NewPipe-app-android/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java

70 lines
2.6 KiB
Java
Raw Normal View History

2017-07-19 16:27:40 +02:00
package org.schabi.newpipe.util;
import android.content.Context;
import android.content.SharedPreferences;
2020-08-27 22:55:57 +02:00
import androidx.preference.PreferenceManager;
2017-07-19 16:27:40 +02:00
import org.schabi.newpipe.R;
import java.util.regex.Pattern;
public final class FilenameUtils {
private static final String CHARSET_MOST_SPECIAL = "[\\n\\r|?*<\":\\\\>/']+";
private static final String CHARSET_ONLY_LETTERS_AND_DIGITS = "[^\\w\\d]+";
private FilenameUtils() { }
2017-07-19 16:27:40 +02:00
/**
* #143 #44 #42 #22: make sure that the filename does not contain illegal chars.
*
2017-07-19 16:27:40 +02:00
* @param context the context to retrieve strings and preferences from
* @param title the title to create a filename from
2017-07-19 16:27:40 +02:00
* @return the filename
*/
public static String createFilename(final Context context, final String title) {
2020-08-16 10:24:58 +02:00
final SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
final String charsetLd = context.getString(R.string.charset_letters_and_digits_value);
final String charsetMs = context.getString(R.string.charset_most_special_value);
final String defaultCharset = context.getString(R.string.default_file_charset_value);
2017-07-19 16:27:40 +02:00
final String replacementChar = sharedPreferences.getString(
context.getString(R.string.settings_file_replacement_character_key), "_");
String selectedCharset = sharedPreferences.getString(
context.getString(R.string.settings_file_charset_key), null);
final String charset;
if (selectedCharset == null || selectedCharset.isEmpty()) {
selectedCharset = defaultCharset;
}
if (selectedCharset.equals(charsetLd)) {
charset = CHARSET_ONLY_LETTERS_AND_DIGITS;
} else if (selectedCharset.equals(charsetMs)) {
charset = CHARSET_MOST_SPECIAL;
} else {
charset = selectedCharset; // Is the user using a custom charset?
}
2020-08-16 10:24:58 +02:00
final Pattern pattern = Pattern.compile(charset);
2017-07-19 16:27:40 +02:00
return createFilename(title, pattern, replacementChar);
}
/**
* Create a valid filename.
*
* @param title the title to create a filename from
2017-07-19 16:27:40 +02:00
* @param invalidCharacters patter matching invalid characters
* @param replacementChar the replacement
2017-07-19 16:27:40 +02:00
* @return the filename
*/
private static String createFilename(final String title, final Pattern invalidCharacters,
final String replacementChar) {
2017-07-19 16:27:40 +02:00
return title.replaceAll(invalidCharacters.pattern(), replacementChar);
}
}