Skip to content

Commit

Permalink
Separate cache dir from download dir
Browse files Browse the repository at this point in the history
  • Loading branch information
akabhirav committed Feb 9, 2023
1 parent 406cb46 commit 25b7426
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object Page {
File(chapterDir).mkdirs()
val fileName = getPageName(index)

return getImageResponse(chapterDir, fileName, useCache) {
return getImageResponse(mangaId, chapterId, fileName, useCache) {
source.fetchImage(tachiyomiPage).awaitSingle()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package suwayomi.tachidesk.manga.impl.util
* 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/. */

import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.kodein.di.DI
Expand All @@ -21,17 +22,16 @@ import java.io.File

private val applicationDirs by DI.global.instance<ApplicationDirs>()

fun getMangaDir(mangaId: Int): String {
val mangaEntry = transaction { MangaTable.select { MangaTable.id eq mangaId }.first() }
fun getMangaDir(mangaId: Int, cache: Boolean = false): String {
val mangaEntry = getMangaEntry(mangaId)
val source = GetCatalogueSource.getCatalogueSourceOrStub(mangaEntry[MangaTable.sourceReference])

val sourceDir = source.toString()
val mangaDir = SafePath.buildValidFilename(mangaEntry[MangaTable.title])

return "${applicationDirs.mangaDownloadsRoot}/$sourceDir/$mangaDir"
return (if (cache) applicationDirs.cacheRoot else applicationDirs.mangaDownloadsRoot) + "/$sourceDir/$mangaDir"
}

fun getChapterDir(mangaId: Int, chapterId: Int): String {
fun getChapterDir(mangaId: Int, chapterId: Int, cache: Boolean = false): String {
val chapterEntry = transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() }

val chapterDir = SafePath.buildValidFilename(
Expand All @@ -41,12 +41,12 @@ fun getChapterDir(mangaId: Int, chapterId: Int): String {
}
)

return getMangaDir(mangaId) + "/$chapterDir"
return getMangaDir(mangaId, cache) + "/$chapterDir"
}

/** return value says if rename/move was successful */
fun updateMangaDownloadDir(mangaId: Int, newTitle: String): Boolean {
val mangaEntry = transaction { MangaTable.select { MangaTable.id eq mangaId }.first() }
val mangaEntry = getMangaEntry(mangaId)
val source = GetCatalogueSource.getCatalogueSourceOrStub(mangaEntry[MangaTable.sourceReference])

val sourceDir = source.toString()
Expand All @@ -66,3 +66,7 @@ fun updateMangaDownloadDir(mangaId: Int, newTitle: String): Boolean {
true
}
}

private fun getMangaEntry(mangaId: Int): ResultRow {
return transaction { MangaTable.select { MangaTable.id eq mangaId }.first() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package suwayomi.tachidesk.manga.impl.util.storage

import okhttp3.Response
import okhttp3.internal.closeQuietly
import suwayomi.tachidesk.manga.impl.util.getChapterDir
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
Expand Down Expand Up @@ -70,7 +71,7 @@ object ImageResponse {
}
}

suspend fun getNoCacheImageResponse(fetcher: suspend () -> Response): Pair<InputStream, String> {
private suspend fun getNoCacheImageResponse(fetcher: suspend () -> Response): Pair<InputStream, String> {
val response = fetcher()

if (response.code == 200) {
Expand All @@ -88,7 +89,16 @@ object ImageResponse {
}
}

suspend fun getImageResponse(saveDir: String, fileName: String, useCache: Boolean = false, fetcher: suspend () -> Response): Pair<InputStream, String> {
suspend fun getImageResponse(mangaId: Int, chapterId: Int, fileName: String, useCache: Boolean, fetcher: suspend () -> Response): Pair<InputStream, String> {
var saveDir = ""
if (useCache) {
saveDir = getChapterDir(mangaId, chapterId, true)
File(saveDir).mkdir()
}
return getImageResponse(saveDir, fileName, useCache, fetcher)
}

suspend fun getImageResponse(saveDir: String, fileName: String, useCache: Boolean, fetcher: suspend () -> Response): Pair<InputStream, String> {
return if (useCache) {
getCachedImageResponse(saveDir, fileName, fetcher)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private val logger = KotlinLogging.logger {}
class ApplicationDirs(
val dataRoot: String = ApplicationRootDir
) {
val cacheRoot = System.getProperty("java.io.tmpdir") + "/tachidesk"
val extensionsRoot = "$dataRoot/extensions"
val thumbnailsRoot = "$dataRoot/thumbnails"
val mangaDownloadsRoot = serverConfig.downloadsPath.ifBlank { "$dataRoot/downloads" }
Expand Down

0 comments on commit 25b7426

Please sign in to comment.