2017-07-19 16:27:40 +02:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public final class FilenameUtils {
|
2019-04-09 23:38:34 +02:00
|
|
|
private static final String CHARSET_MOST_SPECIAL = "[\\n\\r|?*<\":\\\\>/']+";
|
|
|
|
private static final String CHARSET_ONLY_LETTERS_AND_DIGITS = "[^\\w\\d]+";
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
private FilenameUtils() { }
|
|
|
|
|
2017-07-19 16:27:40 +02:00
|
|
|
/**
|
|
|
|
* #143 #44 #42 #22: make sure that the filename does not contain illegal chars.
|
2020-03-31 19:20:15 +02:00
|
|
|
*
|
2017-07-19 16:27:40 +02:00
|
|
|
* @param context the context to retrieve strings and preferences from
|
2020-03-31 19:20:15 +02:00
|
|
|
* @param title the title to create a filename from
|
2017-07-19 16:27:40 +02:00
|
|
|
* @return the filename
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static String createFilename(final Context context, final String title) {
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context);
|
2019-04-09 23:38:34 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
final String charsetLd = context.getString(R.string.charset_letters_and_digits_value);
|
|
|
|
final String charsetMs = context.getString(R.string.charset_most_special_value);
|
2019-04-09 23:38:34 +02:00
|
|
|
final String defaultCharset = context.getString(R.string.default_file_charset_value);
|
2017-07-19 16:27:40 +02:00
|
|
|
|
2020-03-31 19:20:15 +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);
|
2019-04-09 23:38:34 +02:00
|
|
|
|
|
|
|
final String charset;
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (selectedCharset == null || selectedCharset.isEmpty()) {
|
|
|
|
selectedCharset = defaultCharset;
|
|
|
|
}
|
2019-04-09 23:38:34 +02:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (selectedCharset.equals(charsetLd)) {
|
2019-04-09 23:38:34 +02:00
|
|
|
charset = CHARSET_ONLY_LETTERS_AND_DIGITS;
|
2020-03-31 19:20:15 +02:00
|
|
|
} else if (selectedCharset.equals(charsetMs)) {
|
2019-04-09 23:38:34 +02:00
|
|
|
charset = CHARSET_MOST_SPECIAL;
|
|
|
|
} else {
|
2020-03-31 19:20:15 +02:00
|
|
|
charset = selectedCharset; // Is the user using a custom charset?
|
2019-04-09 23:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Pattern pattern = Pattern.compile(charset);
|
|
|
|
|
2017-07-19 16:27:40 +02:00
|
|
|
return createFilename(title, pattern, replacementChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-31 19:20:15 +02:00
|
|
|
* 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
|
2020-03-31 19:20:15 +02:00
|
|
|
* @param replacementChar the replacement
|
2017-07-19 16:27:40 +02:00
|
|
|
* @return the filename
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|