Skip to content

Commit

Permalink
Build paths instead of manually creating the path
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda committed Jul 13, 2023
1 parent d9e3ff6 commit 31b1bca
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -41,23 +42,23 @@ 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 {
return getChapterDownloadPath(mangaId, chapterId) + ".cbz"
}

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 */
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package suwayomi.tachidesk.manga.impl.util.storage

import java.io.File

/*
* Copyright (C) Contributors to the Suwayomi project
*
Expand All @@ -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
Expand Down
26 changes: 13 additions & 13 deletions server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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..." }
Expand All @@ -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
Expand Down

0 comments on commit 31b1bca

Please sign in to comment.