From b0479d0bd96e894b52176c37428761779075aae4 Mon Sep 17 00:00:00 2001 From: Coffeemakr Date: Wed, 19 Jul 2017 16:27:40 +0200 Subject: [PATCH] Add renaming filename patterns --- .../newpipe/download/DownloadDialog.java | 23 ++--------- .../schabi/newpipe/util/FilenameUtils.java | 39 +++++++++++++++++++ app/src/main/res/values/settings_keys.xml | 18 +++++++++ app/src/main/res/values/strings.xml | 12 ++++++ app/src/main/res/xml/settings.xml | 26 +++++++++++-- 5 files changed, 95 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 08a3528d6..8de10a25d 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -27,6 +27,7 @@ import org.schabi.newpipe.extractor.stream_info.StreamInfo; import org.schabi.newpipe.extractor.stream_info.VideoStream; import org.schabi.newpipe.fragments.detail.SpinnerToolbarAdapter; import org.schabi.newpipe.settings.NewPipeSettings; +import org.schabi.newpipe.util.FilenameUtils; import org.schabi.newpipe.util.PermissionHelper; import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.Utils; @@ -107,7 +108,7 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); nameEditText = ((EditText) view.findViewById(R.id.file_name)); - nameEditText.setText(createFileName(currentInfo.title)); + nameEditText.setText(FilenameUtils.createFilename(getContext(), currentInfo.title)); selectedAudioIndex = Utils.getPreferredAudioFormat(getContext(), currentInfo.audio_streams); streamsSpinner = (Spinner) view.findViewById(R.id.quality_spinner); @@ -252,30 +253,12 @@ public class DownloadDialog extends DialogFragment implements RadioGroup.OnCheck } } - /** - * #143 #44 #42 #22: make sure that the filename does not contain illegal chars. - * This should fix some of the "cannot download" problems. - */ - private String createFileName(String fileName) { - // from http://eng-przemelek.blogspot.de/2009/07/how-to-create-valid-file-name.html - - List forbiddenCharsPatterns = new ArrayList<>(); - forbiddenCharsPatterns.add("[:]+"); // Mac OS, but it looks that also Windows XP - forbiddenCharsPatterns.add("[\\*\"/\\\\\\[\\]\\:\\;\\|\\=\\,]+"); // Windows - forbiddenCharsPatterns.add("[^\\w\\d\\.]+"); // last chance... only latin letters and digits - String nameToTest = fileName; - for (String pattern : forbiddenCharsPatterns) { - nameToTest = nameToTest.replaceAll(pattern, "_"); - } - return nameToTest; - } - private void downloadSelected() { String url, location; String fileName = nameEditText.getText().toString().trim(); - if (fileName.isEmpty()) fileName = createFileName(currentInfo.title); + if (fileName.isEmpty()) fileName = FilenameUtils.createFilename(getContext(), currentInfo.title); boolean isAudio = radioVideoAudioGroup.getCheckedRadioButtonId() == R.id.audio_button; url = isAudio ? currentInfo.audio_streams.get(selectedAudioIndex).url : sortedStreamVideosList.get(selectedVideoIndex).url; diff --git a/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java b/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java new file mode 100644 index 000000000..b874a9eca --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/FilenameUtils.java @@ -0,0 +1,39 @@ +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; + +public class FilenameUtils { + + /** + * #143 #44 #42 #22: make sure that the filename does not contain illegal chars. + * @param context the context to retrieve strings and preferences from + * @param title the title to create a filename from + * @return the filename + */ + public static String createFilename(Context context, String title) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String key = context.getString(R.string.settings_file_charset_key); + final String value = sharedPreferences.getString(key, context.getString(R.string.default_file_charset_value)); + Pattern pattern = Pattern.compile(value); + + final String replacementChar = sharedPreferences.getString(context.getString(R.string.settings_file_replacement_character_key), "_"); + return createFilename(title, pattern, replacementChar); + } + + /** + * Create a valid filename + * @param title the title to create a filename from + * @param invalidCharacters patter matching invalid characters + * @param replacementChar the replacement + * @return the filename + */ + private static String createFilename(String title, Pattern invalidCharacters, String replacementChar) { + return title.replaceAll(invalidCharacters.pattern(), replacementChar); + } +} \ No newline at end of file diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index fc3bb77bc..0de19c568 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -232,4 +232,22 @@ show_age_restricted_content use_tor + + file_rename + file_replacement_character + _ + + + @string/charset_letters_and_digits_value + @string/charset_most_special_characters_value + + + + @string/charset_letters_and_digits + @string/charset_most_special_characters + + + @string/charset_most_special_characters \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c2ad28f02..9634d221c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -190,6 +190,18 @@ + + settings_category_downloads + Download + Allowed characters in filenames + Invalid characters are replaced with this value + Replacement character + + [^\w\d]+ + [\n\r|\\?*<":>/']+ + Letters and digits + Most special characters + About NewPipe Settings diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index cf7e49163..282aba918 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -133,10 +133,10 @@ + + android:key="@string/settings_category_downloads" + android:title="@string/settings_category_downloads_title"> + + + + + + +