refactor: add more logs to koreader sync controller

This commit is contained in:
Gauthier Roebroeck 2025-09-16 11:38:00 +08:00
parent d9657587c4
commit 2f9b4e75d2

View file

@ -1,5 +1,6 @@
package org.gotson.komga.interfaces.api.kosync package org.gotson.komga.interfaces.api.kosync
import io.github.oshai.kotlinlogging.KotlinLogging
import org.gotson.komga.domain.model.MediaExtensionEpub import org.gotson.komga.domain.model.MediaExtensionEpub
import org.gotson.komga.domain.model.MediaProfile import org.gotson.komga.domain.model.MediaProfile
import org.gotson.komga.domain.model.R2Device import org.gotson.komga.domain.model.R2Device
@ -25,6 +26,8 @@ import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
import java.time.ZonedDateTime import java.time.ZonedDateTime
private val logger = KotlinLogging.logger {}
@RestController @RestController
@RequestMapping("/koreader", produces = ["application/vnd.koreader.v1+json"]) @RequestMapping("/koreader", produces = ["application/vnd.koreader.v1+json"])
class KoreaderSyncController( class KoreaderSyncController(
@ -48,8 +51,14 @@ class KoreaderSyncController(
@PathVariable bookHash: String, @PathVariable bookHash: String,
): DocumentProgressDto { ): DocumentProgressDto {
val books = bookRepository.findAllByHashKoreader(bookHash) val books = bookRepository.findAllByHashKoreader(bookHash)
if (books.isEmpty()) throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book not found") if (books.isEmpty()) {
if (books.size > 1) throw ResponseStatusException(HttpStatus.CONFLICT, "More than 1 book found with the same hash") logger.debug { "No book found with KOReader hash: $bookHash" }
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book not found")
}
if (books.size > 1) {
logger.debug { "No unique book found with KOReader hash: $bookHash. Found ${books.size} books with the same hash." }
throw ResponseStatusException(HttpStatus.CONFLICT, "More than 1 book found with the same hash")
}
val book = books.first() val book = books.first()
val media = mediaRepository.findById(book.id) val media = mediaRepository.findById(book.id)
@ -69,7 +78,10 @@ class KoreaderSyncController(
when (media.profile) { when (media.profile) {
MediaProfile.DIVINA, MediaProfile.PDF -> readProgress.page.toString() MediaProfile.DIVINA, MediaProfile.PDF -> readProgress.page.toString()
MediaProfile.EPUB -> { MediaProfile.EPUB -> {
val extension = mediaRepository.findExtensionByIdOrNull(book.id) as? MediaExtensionEpub ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Epub extension not found") val extension =
mediaRepository.findExtensionByIdOrNull(book.id) as? MediaExtensionEpub
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Epub extension not found")
.also { logger.error { "Epub extension not found for book ${book.id}. Book should be re-analyzed." } }
// convert the href to its index for KOReader // convert the href to its index for KOReader
val resourceIndex = val resourceIndex =
@ -81,6 +93,7 @@ class KoreaderSyncController(
// return a progress string that points to the beginning of the resource // return a progress string that points to the beginning of the resource
"/body/DocFragment[${resourceIndex + 1}].0" "/body/DocFragment[${resourceIndex + 1}].0"
} }
null -> throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book has no media profile") null -> throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book has no media profile")
} }
@ -99,8 +112,14 @@ class KoreaderSyncController(
@RequestBody koreaderProgress: DocumentProgressDto, @RequestBody koreaderProgress: DocumentProgressDto,
) { ) {
val books = bookRepository.findAllByHashKoreader(koreaderProgress.document) val books = bookRepository.findAllByHashKoreader(koreaderProgress.document)
if (books.isEmpty()) throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book not found") if (books.isEmpty()) {
if (books.size > 1) throw ResponseStatusException(HttpStatus.CONFLICT, "More than 1 book found with the same hash") logger.debug { "No book found with KOReader hash: ${koreaderProgress.document}" }
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Book not found")
}
if (books.size > 1) {
logger.debug { "No unique book found with KOReader hash: ${koreaderProgress.document}. Found ${books.size} books with the same hash." }
throw ResponseStatusException(HttpStatus.CONFLICT, "More than 1 book found with the same hash")
}
val book = books.first() val book = books.first()
val media = mediaRepository.findById(book.id) val media = mediaRepository.findById(book.id)
@ -139,8 +158,12 @@ class KoreaderSyncController(
?.value ?.value
?.toIntOrNull() ?.toIntOrNull()
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not get Epub resource index from progress: ${koreaderProgress.progress}") ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Could not get Epub resource index from progress: ${koreaderProgress.progress}")
.also { logger.error { "Could not get Epub resource index from progress: ${koreaderProgress.progress}" } }
val extension = mediaRepository.findExtensionByIdOrNull(book.id) as? MediaExtensionEpub ?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Epub extension not found") val extension =
mediaRepository.findExtensionByIdOrNull(book.id) as? MediaExtensionEpub
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Epub extension not found")
.also { logger.error { "Epub extension not found for book ${book.id}. Book should be re-analyzed." } }
// get the href from the index provided by KOReader // get the href from the index provided by KOReader
val href = val href =