-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathFilenameUtils.kt
More file actions
66 lines (57 loc) · 2.3 KB
/
FilenameUtils.kt
File metadata and controls
66 lines (57 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* 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 java.util.regex.Matcher
import org.schabi.newpipe.R
import org.schabi.newpipe.ktx.getStringSafe
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 pattern 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)
}
}