mirror of
https://github.com/Suwayomi/Tachidesk.git
synced 2025-12-10 06:42:07 +01:00
Add fetch chapter pages (#576)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package suwayomi.tachidesk.graphql.mutations
|
||||
|
||||
import eu.kanade.tachiyomi.source.model.SChapter
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.batchInsert
|
||||
import org.jetbrains.exposed.sql.deleteWhere
|
||||
import org.jetbrains.exposed.sql.select
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
@@ -9,8 +11,12 @@ import org.jetbrains.exposed.sql.update
|
||||
import suwayomi.tachidesk.graphql.types.ChapterMetaType
|
||||
import suwayomi.tachidesk.graphql.types.ChapterType
|
||||
import suwayomi.tachidesk.manga.impl.Chapter
|
||||
import suwayomi.tachidesk.manga.impl.util.lang.awaitSingle
|
||||
import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource.getCatalogueSourceOrNull
|
||||
import suwayomi.tachidesk.manga.model.table.ChapterMetaTable
|
||||
import suwayomi.tachidesk.manga.model.table.ChapterTable
|
||||
import suwayomi.tachidesk.manga.model.table.MangaTable
|
||||
import suwayomi.tachidesk.manga.model.table.PageTable
|
||||
import suwayomi.tachidesk.server.JavalinSetup.future
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.CompletableFuture
|
||||
@@ -183,4 +189,57 @@ class ChapterMutation {
|
||||
|
||||
return DeleteChapterMetaPayload(clientMutationId, meta, chapter)
|
||||
}
|
||||
|
||||
data class FetchChapterPagesInput(
|
||||
val clientMutationId: String? = null,
|
||||
val chapterId: Int
|
||||
)
|
||||
data class FetchChapterPagesPayload(
|
||||
val clientMutationId: String?,
|
||||
val pages: List<String>,
|
||||
val chapter: ChapterType
|
||||
)
|
||||
fun fetchChapterPages(
|
||||
input: FetchChapterPagesInput
|
||||
): CompletableFuture<FetchChapterPagesPayload> {
|
||||
val (clientMutationId, chapterId) = input
|
||||
|
||||
val chapter = transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() }
|
||||
val manga = transaction { MangaTable.select { MangaTable.id eq chapter[ChapterTable.manga] }.first() }
|
||||
val source = getCatalogueSourceOrNull(manga[MangaTable.sourceReference])!!
|
||||
|
||||
return future {
|
||||
source.fetchPageList(
|
||||
SChapter.create().apply {
|
||||
url = chapter[ChapterTable.url]
|
||||
name = chapter[ChapterTable.name]
|
||||
}
|
||||
).awaitSingle()
|
||||
}.thenApply { pageList ->
|
||||
transaction {
|
||||
PageTable.deleteWhere { PageTable.chapter eq chapterId }
|
||||
PageTable.batchInsert(pageList) { page ->
|
||||
this[PageTable.index] = page.index
|
||||
this[PageTable.url] = page.url
|
||||
this[PageTable.imageUrl] = page.imageUrl
|
||||
this[PageTable.chapter] = chapterId
|
||||
}
|
||||
ChapterTable.update({ ChapterTable.id eq chapterId }) {
|
||||
it[ChapterTable.pageCount] = pageList.size
|
||||
}
|
||||
}
|
||||
|
||||
val mangaId = manga[MangaTable.id].value
|
||||
val chapterIndex = chapter[ChapterTable.sourceOrder]
|
||||
FetchChapterPagesPayload(
|
||||
clientMutationId = clientMutationId,
|
||||
pages = List(pageList.size) { index ->
|
||||
"/api/v1/manga/$mangaId/chapter/$chapterIndex/page/$index"
|
||||
},
|
||||
chapter = ChapterType(
|
||||
transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() }
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +83,6 @@ private class ChapterForDownload(
|
||||
|
||||
private fun updateDatabasePages(pageList: List<Page>) {
|
||||
val chapterId = chapterEntry[ChapterTable.id].value
|
||||
val chapterIndex = chapterEntry[ChapterTable.sourceOrder]
|
||||
val mangaId = chapterEntry[ChapterTable.manga].value
|
||||
|
||||
transaction {
|
||||
pageList.forEach { page ->
|
||||
@@ -108,7 +106,7 @@ private class ChapterForDownload(
|
||||
}
|
||||
}
|
||||
|
||||
updatePageCount(pageList, mangaId, chapterIndex)
|
||||
updatePageCount(pageList, chapterId)
|
||||
|
||||
// chapter was updated
|
||||
chapterEntry = freshChapterEntry()
|
||||
@@ -116,13 +114,12 @@ private class ChapterForDownload(
|
||||
|
||||
private fun updatePageCount(
|
||||
pageList: List<Page>,
|
||||
mangaId: Int,
|
||||
chapterIndex: Int
|
||||
chapterId: Int
|
||||
) {
|
||||
val pageCount = pageList.count()
|
||||
|
||||
transaction {
|
||||
ChapterTable.update({ (ChapterTable.manga eq mangaId) and (ChapterTable.sourceOrder eq chapterIndex) }) {
|
||||
ChapterTable.update({ ChapterTable.id eq chapterId }) {
|
||||
it[ChapterTable.pageCount] = pageCount
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user