DownloadPaths.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/download/DownloadPaths.kt
1package eu.friedcutlet.yokan.services.download23import eu.friedcutlet.yokan.database.DbItemType4import eu.friedcutlet.yokan.domain.Manga5import eu.friedcutlet.yokan.services.files.LocalFileService6import eu.friedcutlet.yokan.services.files.downloadItemTypeFolder78fun downloadStorageRoot(configuredRoot: String?, files: LocalFileService): String =9 (configuredRoot?.takeIf { it.isNotBlank() } ?: files.downloadRoot).trimEnd('/', '\\')1011fun downloadStorageRootCandidates(configuredRoot: String?, files: LocalFileService): List<String> {12 val root = downloadStorageRoot(configuredRoot, files)13 return listOf(root, "$root/downloads").distinct()14}1516/** Chapter folder a download writes to: `<storageRoot>/<type>/<source>/<manga>/<scanlator_><chapter>`. */17internal fun downloadChapterLocation(18 storageRoot: String,19 itemType: DbItemType,20 sourceLabel: String,21 mangaName: String,22 scanlator: String,23 chapterName: String,24): String {25 val source = sanitizeDownloadName(sourceLabel.ifBlank { "Source" })26 val manga = sanitizeDownloadName(mangaName.ifBlank { "Unknown" })27 val scanlatorPrefix = if (scanlator.isNotBlank()) "${sanitizeDownloadName(scanlator)}_" else ""28 val chapter = sanitizeDownloadName(chapterName.ifBlank { "Chapter" })29 return "$storageRoot/${downloadItemTypeFolder(itemType)}/$source/$manga/$scanlatorPrefix$chapter"30}3132/** The source folder label of a chapter download: "Name (LANG)", else the title's own source name. */33internal fun downloadSourceLabel(sourceName: String?, sourceLang: String?, titleSource: String): String =34 sourceName?.let { "$it (${sourceLang.orEmpty().uppercase()})" } ?: titleSource3536internal fun sanitizeDownloadName(value: String): String = value.trim().replace(UnsafeFileNameChars, "_").take(120)37 .let { if (it.isBlank() || it == "." || it == "..") "_" else it }3839private val UnsafeFileNameChars = Regex("[\\\\/:*?\"<>|\\u0000]")40