Convert newpipe/util/FilenameUtils.java to kotlin
Co-authored-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
parent
84c646713d
commit
f1b111212d
@ -1,70 +0,0 @@
|
||||
package org.schabi.newpipe.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
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() { }
|
||||
|
||||
/**
|
||||
* #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(final Context context, final String title) {
|
||||
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);
|
||||
|
||||
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?
|
||||
}
|
||||
|
||||
final Pattern pattern = Pattern.compile(charset);
|
||||
|
||||
return createFilename(title, pattern, Matcher.quoteReplacement(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(final String title, final Pattern invalidCharacters,
|
||||
final String replacementChar) {
|
||||
return title.replaceAll(invalidCharacters.pattern(), replacementChar);
|
||||
}
|
||||
}
|
||||
64
app/src/main/java/org/schabi/newpipe/util/FilenameUtils.kt
Normal file
64
app/src/main/java/org/schabi/newpipe/util/FilenameUtils.kt
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2025 NewPipe contributors <https://newpipe.net>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package org.schabi.newpipe.util
|
||||
|
||||
import android.content.Context
|
||||
import androidx.preference.PreferenceManager
|
||||
import org.schabi.newpipe.R
|
||||
import org.schabi.newpipe.ktx.getStringSafe
|
||||
import java.util.regex.Matcher
|
||||
|
||||
object FilenameUtils {
|
||||
private const val CHARSET_MOST_SPECIAL = "[\\n\\r|?*<\":\\\\>/']+"
|
||||
private const val CHARSET_ONLY_LETTERS_AND_DIGITS = "[^\\w\\d]+"
|
||||
|
||||
/**
|
||||
* #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
|
||||
*/
|
||||
@JvmStatic
|
||||
fun createFilename(context: Context, title: String): String {
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
|
||||
val charsetLd = context.getString(R.string.charset_letters_and_digits_value)
|
||||
val charsetMs = context.getString(R.string.charset_most_special_value)
|
||||
val defaultCharset = context.getString(R.string.default_file_charset_value)
|
||||
|
||||
val replacementChar = sharedPreferences.getStringSafe(
|
||||
context.getString(R.string.settings_file_replacement_character_key), "_"
|
||||
)
|
||||
val selectedCharset = sharedPreferences.getStringSafe(
|
||||
context.getString(R.string.settings_file_charset_key), ""
|
||||
).ifEmpty { defaultCharset }
|
||||
|
||||
val charset = when (selectedCharset) {
|
||||
charsetLd -> CHARSET_ONLY_LETTERS_AND_DIGITS
|
||||
charsetMs -> CHARSET_MOST_SPECIAL
|
||||
else -> selectedCharset // Is the user using a custom charset?
|
||||
}
|
||||
|
||||
return createFilename(title, charset, Matcher.quoteReplacement(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 fun createFilename(
|
||||
title: String,
|
||||
invalidCharacters: String,
|
||||
replacementChar: String
|
||||
): String {
|
||||
return title.replace(invalidCharacters.toRegex(), replacementChar)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user