Skip to content

Commit

Permalink
fix(komga): empty generated thumbnails would be saved in DB
Browse files Browse the repository at this point in the history
Closes: #1338
  • Loading branch information
gotson committed Dec 13, 2023
1 parent 77b8a32 commit 15920b7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
delete
from THUMBNAIL_BOOK
where TYPE = 'GENERATED'
and THUMBNAIL is null;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open class CodedException : Exception {
fun Exception.withCode(code: String) = CodedException(this, code)

class MediaNotReadyException : Exception()
class NoThumbnailFoundException : Exception()
class MediaUnsupportedException(message: String, code: String = "") : CodedException(message, code)
class ImageConversionException(message: String, code: String = "") : CodedException(message, code)
class DirectoryNotFoundException(message: String, code: String = "") : CodedException(message, code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gotson.komga.domain.model.MediaNotReadyException
import org.gotson.komga.domain.model.MediaProfile
import org.gotson.komga.domain.model.MediaType
import org.gotson.komga.domain.model.MediaUnsupportedException
import org.gotson.komga.domain.model.NoThumbnailFoundException
import org.gotson.komga.domain.model.ThumbnailBook
import org.gotson.komga.domain.model.TypedBytes
import org.gotson.komga.infrastructure.configuration.KomgaSettingsProvider
Expand Down Expand Up @@ -143,7 +144,10 @@ class BookAnalyzer(
return Media(status = Media.Status.READY, pages = pages)
}

@Throws(MediaNotReadyException::class)
@Throws(
MediaNotReadyException::class,
NoThumbnailFoundException::class,
)
fun generateThumbnail(book: BookWithMedia): ThumbnailBook {
logger.info { "Generate thumbnail for book: $book" }

Expand All @@ -152,22 +156,17 @@ class BookAnalyzer(
throw MediaNotReadyException()
}

val thumbnail = try {
getPoster(book)?.let { cover ->
imageConverter.resizeImageToByteArray(cover.bytes, thumbnailType, komgaSettingsProvider.thumbnailSize.maxEdge)
}
} catch (ex: Exception) {
logger.warn(ex) { "Could not generate thumbnail for book: $book" }
null
}
val thumbnail = getPoster(book)?.let { cover ->
imageConverter.resizeImageToByteArray(cover.bytes, thumbnailType, komgaSettingsProvider.thumbnailSize.maxEdge)
} ?: throw NoThumbnailFoundException()

return ThumbnailBook(
thumbnail = thumbnail,
type = ThumbnailBook.Type.GENERATED,
bookId = book.book.id,
mediaType = thumbnailType.mediaType,
dimension = thumbnail?.let { imageAnalyzer.getDimension(it.inputStream()) } ?: Dimension(0, 0),
fileSize = thumbnail?.size?.toLong() ?: 0,
dimension = imageAnalyzer.getDimension(thumbnail.inputStream()) ?: Dimension(0, 0),
fileSize = thumbnail.size.toLong(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.gotson.komga.domain.model.Media
import org.gotson.komga.domain.model.MediaExtensionEpub
import org.gotson.komga.domain.model.MediaNotReadyException
import org.gotson.komga.domain.model.MediaProfile
import org.gotson.komga.domain.model.NoThumbnailFoundException
import org.gotson.komga.domain.model.R2Progression
import org.gotson.komga.domain.model.ReadProgress
import org.gotson.komga.domain.model.ThumbnailBook
Expand Down Expand Up @@ -121,6 +122,8 @@ class BookLifecycle(
logger.info { "Generate thumbnail and persist for book: $book" }
try {
addThumbnailForBook(bookAnalyzer.generateThumbnail(BookWithMedia(book, mediaRepository.findById(book.id))), MarkSelectedPreference.IF_NONE_OR_GENERATED)
} catch (ex: NoThumbnailFoundException) {
logger.error { "Error while creating thumbnail" }
} catch (ex: Exception) {
logger.error(ex) { "Error while creating thumbnail" }
}
Expand Down

0 comments on commit 15920b7

Please sign in to comment.