feat(komga): relax epub validity controls

Komga will check if a mimetype file is present if the detected media type is application/zip
This commit is contained in:
Gauthier Roebroeck 2023-12-14 16:39:20 +08:00
parent e856d4fb21
commit fad93ada4b
3 changed files with 23 additions and 3 deletions

View file

@ -0,0 +1,9 @@
update media
set STATUS = 'OUTDATED'
where MEDIA_TYPE = 'application/epub+zip';
update media
set STATUS = 'OUTDATED'
where BOOK_ID in (select ID
from BOOK
where URL like '%.epub' collate NOCASE);

View file

@ -59,14 +59,17 @@ class BookAnalyzer(
fun analyze(book: Book, analyzeDimensions: Boolean): Media {
logger.info { "Trying to analyze book: $book" }
return try {
val mediaType = contentDetector.detectMediaType(book.path).let {
var mediaType = contentDetector.detectMediaType(book.path).let {
logger.info { "Detected media type: $it" }
MediaType.fromMediaType(it) ?: return Media(mediaType = it, status = Media.Status.UNSUPPORTED, comment = "ERR_1001", bookId = book.id)
}
if (book.path.extension.lowercase() == "epub" && mediaType != MediaType.EPUB) {
logger.warn { "Epub file detected as zip, file is probably broken: ${book.path}" }
return Media(mediaType = mediaType.type, status = Media.Status.ERROR, comment = "ERR_1032", bookId = book.id)
if (epubExtractor.isEpub(book.path)) mediaType = MediaType.EPUB
else {
logger.warn { "Epub file is malformed, file is probably broken: ${book.path}" }
return Media(mediaType = mediaType.type, status = Media.Status.ERROR, comment = "ERR_1032", bookId = book.id)
}
}
when (mediaType.profile) {
@ -208,6 +211,7 @@ class BookAnalyzer(
MediaProfile.EPUB ->
if (book.media.epubDivinaCompatible) epubExtractor.getEntryStream(book.book.path, book.media.pages[number - 1].fileName)
else throw MediaUnsupportedException("Epub profile does not support getting page content")
null -> throw MediaNotReadyException()
}
}

View file

@ -36,6 +36,13 @@ class EpubExtractor(
zip.getInputStream(zip.getEntry(entryName)).use { it.readBytes() }
}
fun isEpub(path: Path): Boolean =
try {
getEntryStream(path, "mimetype").decodeToString() == "application/epub+zip"
} catch (e: Exception) {
false
}
/**
* Retrieves the book cover along with its mediaType from the epub 2/3 manifest
*/