mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2026-01-21 11:12:36 +01:00
Fix path to Preference file if it contains a invalid path character (#750)
* Fix path to shared preference files if it contains a invalid character * Lint
This commit is contained in:
@@ -19,6 +19,7 @@ import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.builtins.SetSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import mu.KotlinLogging
|
||||
import xyz.nulldev.androidcompat.util.SafePath
|
||||
import xyz.nulldev.ts.config.ApplicationRootDir
|
||||
import java.util.Properties
|
||||
import kotlin.io.path.Path
|
||||
@@ -34,7 +35,12 @@ class JavaSharedPreferences(key: String) : SharedPreferences {
|
||||
private val logger = KotlinLogging.logger {}
|
||||
}
|
||||
|
||||
private val file = Path(ApplicationRootDir, "settings", "$key.xml")
|
||||
private val file =
|
||||
Path(
|
||||
ApplicationRootDir,
|
||||
"settings",
|
||||
"${SafePath.buildValidFilename(key)}.xml",
|
||||
)
|
||||
private val properties =
|
||||
Properties().also { properties ->
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package xyz.nulldev.androidcompat.util
|
||||
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// adopted from: https://github.com/tachiyomiorg/tachiyomi/blob/4cefbce7c34e724b409b6ba127f3c6c5c346ad8d/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt
|
||||
object SafePath {
|
||||
/**
|
||||
* Mutate the given filename to make it valid for a FAT filesystem,
|
||||
* replacing any invalid characters with "_". This method doesn't allow hidden files (starting
|
||||
* with a dot), but you can manually add it later.
|
||||
*/
|
||||
fun buildValidFilename(origName: String): String {
|
||||
val name = origName.trim('.', ' ')
|
||||
if (name.isEmpty()) {
|
||||
return "(invalid)"
|
||||
}
|
||||
val sb = StringBuilder(name.length)
|
||||
name.forEach { c ->
|
||||
if (isValidFatFilenameChar(c)) {
|
||||
sb.append(c)
|
||||
} else {
|
||||
sb.append('_')
|
||||
}
|
||||
}
|
||||
// Even though vfat allows 255 UCS-2 chars, we might eventually write to
|
||||
// ext4 through a FUSE layer, so use that limit minus 15 reserved characters.
|
||||
return sb.toString().take(240)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given character is a valid filename character, false otherwise.
|
||||
*/
|
||||
private fun isValidFatFilenameChar(c: Char): Boolean {
|
||||
if (0x00.toChar() <= c && c <= 0x1f.toChar()) {
|
||||
return false
|
||||
}
|
||||
return when (c) {
|
||||
'"', '*', '/', ':', '<', '>', '?', '\\', '|', 0x7f.toChar() -> false
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user