feat: handle archives without images

closes #56
This commit is contained in:
Gauthier Roebroeck 2020-01-06 14:56:45 +08:00
parent 98efa9b448
commit 70a2da5321
3 changed files with 38 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package org.gotson.komga.domain.model package org.gotson.komga.domain.model
class MediaNotReadyException : Exception() class MediaNotReadyException : Exception()
class EmptyBookException(val mediaType: String) : Exception()
class UnsupportedMediaTypeException(message: String, val mediaType: String) : Exception(message) class UnsupportedMediaTypeException(message: String, val mediaType: String) : Exception(message)
class DirectoryNotFoundException(message: String) : Exception(message) class DirectoryNotFoundException(message: String) : Exception(message)
class DuplicateNameException(message: String) : Exception(message) class DuplicateNameException(message: String) : Exception(message)

View file

@ -4,6 +4,7 @@ import mu.KotlinLogging
import net.coobird.thumbnailator.Thumbnails import net.coobird.thumbnailator.Thumbnails
import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator
import org.gotson.komga.domain.model.Book import org.gotson.komga.domain.model.Book
import org.gotson.komga.domain.model.EmptyBookException
import org.gotson.komga.domain.model.Media import org.gotson.komga.domain.model.Media
import org.gotson.komga.domain.model.MediaNotReadyException import org.gotson.komga.domain.model.MediaNotReadyException
import org.gotson.komga.domain.model.UnsupportedMediaTypeException import org.gotson.komga.domain.model.UnsupportedMediaTypeException
@ -36,7 +37,10 @@ class BookAnalyzer(
private val thumbnailSize = 300 private val thumbnailSize = 300
private val thumbnailFormat = "jpeg" private val thumbnailFormat = "jpeg"
@Throws(UnsupportedMediaTypeException::class) @Throws(
UnsupportedMediaTypeException::class,
EmptyBookException::class
)
fun analyze(book: Book): Media { fun analyze(book: Book): Media {
logger.info { "Trying to analyze book: $book" } logger.info { "Trying to analyze book: $book" }
@ -47,6 +51,7 @@ class BookAnalyzer(
val pages = supportedMediaTypes.getValue(mediaType).getPagesList(book.path()) val pages = supportedMediaTypes.getValue(mediaType).getPagesList(book.path())
.sortedWith(compareBy(natSortComparator) { it.fileName }) .sortedWith(compareBy(natSortComparator) { it.fileName })
if (pages.isEmpty()) throw EmptyBookException(mediaType)
logger.info { "Book has ${pages.size} pages" } logger.info { "Book has ${pages.size} pages" }
logger.info { "Trying to generate cover for book: $book" } logger.info { "Trying to generate cover for book: $book" }

View file

@ -4,6 +4,7 @@ import mu.KotlinLogging
import org.apache.commons.lang3.time.DurationFormatUtils import org.apache.commons.lang3.time.DurationFormatUtils
import org.gotson.komga.domain.model.Book import org.gotson.komga.domain.model.Book
import org.gotson.komga.domain.model.BookPageContent import org.gotson.komga.domain.model.BookPageContent
import org.gotson.komga.domain.model.EmptyBookException
import org.gotson.komga.domain.model.Media import org.gotson.komga.domain.model.Media
import org.gotson.komga.domain.model.MediaNotReadyException import org.gotson.komga.domain.model.MediaNotReadyException
import org.gotson.komga.domain.model.UnsupportedMediaTypeException import org.gotson.komga.domain.model.UnsupportedMediaTypeException
@ -34,8 +35,11 @@ class BookLifecycle(
try { try {
book.media = bookAnalyzer.analyze(book) book.media = bookAnalyzer.analyze(book)
} catch (ex: UnsupportedMediaTypeException) { } catch (ex: UnsupportedMediaTypeException) {
logger.info(ex) { "Unsupported media type: ${ex.mediaType}. Book: $book" } logger.warn { "Unsupported media type: ${ex.mediaType}. Book: $book" }
book.media = Media(status = Media.Status.UNSUPPORTED, mediaType = ex.mediaType) book.media = Media(status = Media.Status.UNSUPPORTED, mediaType = ex.mediaType)
} catch (ex: EmptyBookException) {
logger.warn { "Book does not contain any images: $book" }
book.media = Media(status = Media.Status.ERROR, mediaType = ex.mediaType)
} catch (ex: Exception) { } catch (ex: Exception) {
logger.error(ex) { "Error while parsing. Book: $book" } logger.error(ex) { "Error while parsing. Book: $book" }
book.media = Media(status = Media.Status.ERROR) book.media = Media(status = Media.Status.ERROR)