actaully make sure the chapter exists

This commit is contained in:
Aria Moradi
2021-09-17 00:38:15 +04:30
parent 8e6b219eea
commit 4e72a3886f
3 changed files with 26 additions and 7 deletions

View File

@@ -18,8 +18,10 @@ import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import suwayomi.tachidesk.manga.impl.Manga.getManga
import suwayomi.tachidesk.manga.impl.Page.getPageName
import suwayomi.tachidesk.manga.impl.util.GetHttpSource.getHttpSource
import suwayomi.tachidesk.manga.impl.util.lang.awaitSingle
import suwayomi.tachidesk.manga.impl.util.storage.CachedImageResponse
import suwayomi.tachidesk.manga.model.dataclass.ChapterDataClass
import suwayomi.tachidesk.manga.model.table.ChapterMetaTable
import suwayomi.tachidesk.manga.model.table.ChapterTable
@@ -143,7 +145,15 @@ object Chapter {
}.first()
}
return if (!chapterEntry[ChapterTable.isDownloaded]) {
val isReallyDownloaded =
chapterEntry[ChapterTable.isDownloaded] && firstPageExists(mangaId, chapterEntry[ChapterTable.id].value)
return if (!isReallyDownloaded) {
transaction {
ChapterTable.update({ (ChapterTable.chapterIndex eq chapterIndex) and (ChapterTable.manga eq mangaId) }) {
it[isDownloaded] = false
}
}
val mangaEntry = transaction { MangaTable.select { MangaTable.id eq mangaId }.first() }
val source = getHttpSource(mangaEntry[MangaTable.sourceReference])
@@ -210,6 +220,12 @@ object Chapter {
}
}
private fun firstPageExists(mangaId: Int, chapterId: Int): Boolean =
CachedImageResponse.findFileNameStartingWith(
Page.getChapterDir(mangaId, chapterId),
getPageName(0)
) != null
fun modifyChapter(
mangaId: Int,
chapterIndex: Int,

View File

@@ -51,14 +51,14 @@ object Page {
val pageEntry = transaction { PageTable.select { (PageTable.chapter eq chapterId) and (PageTable.index eq index) }.first() }
val tachiPage = Page(
val tachiyomiPage = Page(
pageEntry[PageTable.index],
pageEntry[PageTable.url],
pageEntry[PageTable.imageUrl]
)
if (pageEntry[PageTable.imageUrl] == null) {
val trueImageUrl = getTrueImageUrl(tachiPage, source)
val trueImageUrl = getTrueImageUrl(tachiyomiPage, source)
transaction {
PageTable.update({ (PageTable.chapter eq chapterId) and (PageTable.index eq index) }) {
it[imageUrl] = trueImageUrl
@@ -68,15 +68,18 @@ object Page {
val saveDir = getChapterDir(mangaId, chapterId)
File(saveDir).mkdirs()
val fileName = String.format("%03d", index) // e.g. 001.jpeg
val fileName = getPageName(index) // e.g. 001
return getCachedImageResponse(saveDir, fileName) {
source.fetchImage(tachiPage).awaitSingle()
source.fetchImage(tachiyomiPage).awaitSingle()
}
}
fun getPageName(index: Int): String = String.format("%03d", index)
private val applicationDirs by DI.global.instance<ApplicationDirs>()
private fun getChapterDir(mangaId: Int, chapterId: Int): String {
fun getChapterDir(mangaId: Int, chapterId: Int): String {
val mangaEntry = transaction { MangaTable.select { MangaTable.id eq mangaId }.first() }
val source = getHttpSource(mangaEntry[MangaTable.sourceReference])
val chapterEntry = transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() }

View File

@@ -18,7 +18,7 @@ object CachedImageResponse {
return FileInputStream(path).buffered()
}
private fun findFileNameStartingWith(directoryPath: String, fileName: String): String? {
fun findFileNameStartingWith(directoryPath: String, fileName: String): String? {
val target = "$fileName."
File(directoryPath).listFiles().orEmpty().forEach { file ->
if (file.name.startsWith(target))