From 31b1bca8c43c79a8f4c0c937449c5f1102c5bd2b Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 2 Jul 2023 23:44:44 +0200 Subject: [PATCH] Build paths instead of manually creating the path --- .../fileProvider/impl/FolderProvider.kt | 3 ++- .../tachidesk/manga/impl/util/DirName.kt | 15 ++++++----- .../manga/impl/util/storage/SafePath.kt | 6 +++++ .../suwayomi/tachidesk/server/ServerSetup.kt | 26 +++++++++---------- .../server/util/WebInterfaceManager.kt | 5 ++-- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/fileProvider/impl/FolderProvider.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/fileProvider/impl/FolderProvider.kt index 7ae5702320..e14724f416 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/fileProvider/impl/FolderProvider.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/fileProvider/impl/FolderProvider.kt @@ -13,6 +13,7 @@ import suwayomi.tachidesk.manga.impl.download.fileProvider.ChaptersFilesProvider import suwayomi.tachidesk.manga.impl.download.model.DownloadChapter import suwayomi.tachidesk.manga.impl.util.getChapterDownloadPath import suwayomi.tachidesk.manga.impl.util.storage.ImageResponse +import suwayomi.tachidesk.manga.impl.util.storage.SafePath.joinPaths import java.io.File import java.io.FileInputStream import java.io.InputStream @@ -60,7 +61,7 @@ class FolderProvider(mangaId: Int, chapterId: Int) : ChaptersFilesProvider(manga } .launchIn(scope) }.first.use { image -> - val filePath = "$chapterDir/$fileName" + val filePath = joinPaths(chapterDir, fileName) ImageResponse.saveImage(filePath, image) } } finally { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/DirName.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/DirName.kt index cc15edb5ff..0ea080f9ba 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/DirName.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/DirName.kt @@ -15,6 +15,7 @@ import org.kodein.di.conf.global import org.kodein.di.instance import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource import suwayomi.tachidesk.manga.impl.util.storage.SafePath +import suwayomi.tachidesk.manga.impl.util.storage.SafePath.joinPaths import suwayomi.tachidesk.manga.model.table.ChapterTable import suwayomi.tachidesk.manga.model.table.MangaTable import suwayomi.tachidesk.server.ApplicationDirs @@ -28,7 +29,7 @@ private fun getMangaDir(mangaId: Int): String { val sourceDir = SafePath.buildValidFilename(source.toString()) val mangaDir = SafePath.buildValidFilename(mangaEntry[MangaTable.title]) - return "$sourceDir/$mangaDir" + return joinPaths(sourceDir, mangaDir) } private fun getChapterDir(mangaId: Int, chapterId: Int): String { @@ -41,15 +42,15 @@ private fun getChapterDir(mangaId: Int, chapterId: Int): String { } ) - return getMangaDir(mangaId) + "/$chapterDir" + return joinPaths(getMangaDir(mangaId), chapterDir) } fun getThumbnailDownloadPath(mangaId: Int): String { - return applicationDirs.thumbnailDownloadsRoot + "/$mangaId" + return joinPaths(applicationDirs.thumbnailDownloadsRoot, mangaId.toString()) } fun getChapterDownloadPath(mangaId: Int, chapterId: Int): String { - return applicationDirs.mangaDownloadsRoot + "/" + getChapterDir(mangaId, chapterId) + return joinPaths(applicationDirs.mangaDownloadsRoot, getChapterDir(mangaId, chapterId)) } fun getChapterCbzPath(mangaId: Int, chapterId: Int): String { @@ -57,7 +58,7 @@ fun getChapterCbzPath(mangaId: Int, chapterId: Int): String { } fun getChapterCachePath(mangaId: Int, chapterId: Int): String { - return applicationDirs.tempMangaCacheRoot + "/" + getChapterDir(mangaId, chapterId) + return joinPaths(applicationDirs.tempMangaCacheRoot, getChapterDir(mangaId, chapterId)) } /** return value says if rename/move was successful */ @@ -70,8 +71,8 @@ fun updateMangaDownloadDir(mangaId: Int, newTitle: String): Boolean { val newMangaDir = SafePath.buildValidFilename(newTitle) - val oldDir = "${applicationDirs.downloadsRoot}/$sourceDir/$mangaDir" - val newDir = "${applicationDirs.downloadsRoot}/$sourceDir/$newMangaDir" + val oldDir = joinPaths(applicationDirs.downloadsRoot, sourceDir, mangaDir) + val newDir = joinPaths(applicationDirs.downloadsRoot, sourceDir, newMangaDir) val oldDirFile = File(oldDir) val newDirFile = File(newDir) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/SafePath.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/SafePath.kt index 96a37b2e8f..6dd6b804ff 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/SafePath.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/storage/SafePath.kt @@ -1,5 +1,7 @@ package suwayomi.tachidesk.manga.impl.util.storage +import java.io.File + /* * Copyright (C) Contributors to the Suwayomi project * @@ -9,6 +11,10 @@ package suwayomi.tachidesk.manga.impl.util.storage // adopted from: https://github.com/tachiyomiorg/tachiyomi/blob/4cefbce7c34e724b409b6ba127f3c6c5c346ad8d/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt object SafePath { + fun joinPaths(vararg paths: String): String { + return paths.reduce { parent, child -> File(parent, child).toString() } + } + /** * 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 diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt index ecc2a65530..360ebf87d4 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt @@ -22,6 +22,7 @@ import suwayomi.tachidesk.manga.impl.backup.proto.ProtoBackupExport import suwayomi.tachidesk.manga.impl.update.IUpdater import suwayomi.tachidesk.manga.impl.update.Updater import suwayomi.tachidesk.manga.impl.util.lang.renameTo +import suwayomi.tachidesk.manga.impl.util.storage.SafePath.joinPaths import suwayomi.tachidesk.server.database.databaseUp import suwayomi.tachidesk.server.util.AppMutex.handleAppMutex import suwayomi.tachidesk.server.util.SystemTray.systemTray @@ -38,20 +39,19 @@ private val logger = KotlinLogging.logger {} class ApplicationDirs( val dataRoot: String = ApplicationRootDir, - val tempRoot: String = "${System.getProperty("java.io.tmpdir")}/Tachidesk" + tempRoot: String = joinPaths(System.getProperty("java.io.tmpdir"), "Tachidesk") ) { - val cacheRoot = System.getProperty("java.io.tmpdir") + "/tachidesk" - val extensionsRoot = "$dataRoot/extensions" - val downloadsRoot = serverConfig.downloadsPath.ifBlank { "$dataRoot/downloads" } - val localMangaRoot = "$dataRoot/local" - val webUIRoot = "$dataRoot/webUI" - val automatedBackupRoot = serverConfig.backupPath.ifBlank { "$dataRoot/backups" } - - val tempThumbnailCacheRoot = "$tempRoot/thumbnails" - val tempMangaCacheRoot = "$tempRoot/manga-cache" - - val thumbnailDownloadsRoot = "$downloadsRoot/thumbnails" - val mangaDownloadsRoot = "$downloadsRoot/mangas" + val extensionsRoot = joinPaths(dataRoot, "extensions") + val downloadsRoot = serverConfig.downloadsPath.ifBlank { joinPaths(dataRoot, "downloads") } + val localMangaRoot = joinPaths(dataRoot, "local") + val webUIRoot = joinPaths(dataRoot, "webUI") + val automatedBackupRoot = serverConfig.backupPath.ifBlank { joinPaths(dataRoot, "backups") } + + val tempThumbnailCacheRoot = joinPaths(tempRoot, "thumbnails") + val tempMangaCacheRoot = joinPaths(tempRoot, "manga-cache") + + val thumbnailDownloadsRoot = joinPaths(downloadsRoot, "thumbnails") + val mangaDownloadsRoot = joinPaths(downloadsRoot, "mangas") } val serverConfig: ServerConfig by lazy { GlobalConfigManager.module() } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt index 52ba7dbf4c..309b0271d2 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/util/WebInterfaceManager.kt @@ -13,6 +13,7 @@ import net.lingala.zip4j.ZipFile import org.kodein.di.DI import org.kodein.di.conf.global import org.kodein.di.instance +import suwayomi.tachidesk.manga.impl.util.storage.SafePath.joinPaths import suwayomi.tachidesk.server.ApplicationDirs import suwayomi.tachidesk.server.BuildConfig import suwayomi.tachidesk.server.serverConfig @@ -62,7 +63,7 @@ fun setupWebInterface() { /** Make sure a valid copy of WebUI is available */ fun setupWebUI() { // check if we have webUI installed and is correct version - val webUIRevisionFile = File(applicationDirs.webUIRoot + "/revision") + val webUIRevisionFile = File(applicationDirs.webUIRoot, "revision") if (webUIRevisionFile.exists() && webUIRevisionFile.readText().trim() == BuildConfig.WEBUI_TAG) { logger.info { "WebUI Static files exists and is the correct revision" } logger.info { "Verifying WebUI Static files..." } @@ -71,7 +72,7 @@ fun setupWebUI() { File(applicationDirs.webUIRoot).deleteRecursively() val webUIZip = "Tachidesk-WebUI-${BuildConfig.WEBUI_TAG}.zip" - val webUIZipPath = "$tmpDir/$webUIZip" + val webUIZipPath = joinPaths(tmpDir, webUIZip) val webUIZipFile = File(webUIZipPath) // try with resources first