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 22, 2023
1 parent f4177d4 commit 4fdbc05
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 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 @@ -24,6 +24,7 @@ import suwayomi.tachidesk.manga.impl.download.DownloadManager
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 @@ -41,20 +42,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 @@ -17,6 +17,7 @@ import org.json.JSONArray
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 @@ -191,12 +192,12 @@ object WebInterfaceManager {
}

private fun getLocalVersion(path: String): String {
return File("$path/revision").readText().trim()
return File(path, "revision").readText().trim()
}

private fun doesLocalWebUIExist(path: String): Boolean {
// check if we have webUI installed and is correct version
val webUIRevisionFile = File("$path/revision")
val webUIRevisionFile = File(path, "revision")
return webUIRevisionFile.exists()
}

Expand Down Expand Up @@ -298,7 +299,7 @@ object WebInterfaceManager {
}

val webUIZip = "${WebUI.WEBUI.baseFileName}-$latestCompatibleVersion.zip"
val webUIZipPath = "$tmpDir/$webUIZip"
val webUIZipPath = joinPaths(tmpDir, webUIZip)
val webUIZipFile = File(webUIZipPath)

logger.info { "downloadLatestCompatibleVersion: Downloading WebUI (flavor= ${serverConfig.webUIFlavor}, version \"$latestCompatibleVersion\") zip from the Internet..." }
Expand Down

0 comments on commit 4fdbc05

Please sign in to comment.