diff --git a/ERRORCODES.md b/ERRORCODES.md index d6c8ceb13..5d0e14071 100644 --- a/ERRORCODES.md +++ b/ERRORCODES.md @@ -35,3 +35,4 @@ | ERR_1029 | ComicRack CBL does not contain any Book element | | ERR_1030 | ComicRack CBL has no Name element | | ERR_1031 | ComicRack CBL Book is missing series or number | +| ERR_1032 | EPUB file has wrong media type | diff --git a/komga-webui/src/locales/en.json b/komga-webui/src/locales/en.json index 428bc739e..20de97e9a 100644 --- a/komga-webui/src/locales/en.json +++ b/komga-webui/src/locales/en.json @@ -750,7 +750,8 @@ "ERR_1028": "OpenID Connect login error: no email attribute", "ERR_1029": "ComicRack CBL does not contain any Book element", "ERR_1030": "ComicRack CBL has no Name element", - "ERR_1031": "ComicRack CBL Book is missing series or number" + "ERR_1031": "ComicRack CBL Book is missing series or number", + "ERR_1032": "EPUB file has wrong media type" }, "filter": { "age_rating": "age rating", diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookAnalyzer.kt b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookAnalyzer.kt index 392abe3ba..f429f4b39 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookAnalyzer.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookAnalyzer.kt @@ -30,6 +30,7 @@ import java.io.ByteArrayOutputStream import java.nio.file.AccessDeniedException import java.nio.file.NoSuchFileException import javax.imageio.ImageIO +import kotlin.io.path.extension private val logger = KotlinLogging.logger {} @@ -62,6 +63,11 @@ class BookAnalyzer( 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) + } + when (mediaType.profile) { MediaProfile.DIVINA -> analyzeDivina(book, mediaType, analyzeDimensions) MediaProfile.PDF -> analyzePdf(book, analyzeDimensions) diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookAnalyzerTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookAnalyzerTest.kt index cf05d9575..ed82840ff 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookAnalyzerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookAnalyzerTest.kt @@ -133,6 +133,18 @@ class BookAnalyzerTest( assertThat(media.pages).hasSize(0) } + @Test + fun `given broken epub archive when analyzing then media status is ERROR`() { + val file = ClassPathResource("archives/zip-as-epub.epub") + val book = Book("book", file.url, LocalDateTime.now()) + + val media = bookAnalyzer.analyze(book, false) + + assertThat(media.mediaType).isEqualTo("application/zip") + assertThat(media.status).isEqualTo(Media.Status.ERROR) + assertThat(media.pages).hasSize(0) + } + @Test fun `given book with a single page when hashing then all pages are hashed`() { val book = makeBook("book1") diff --git a/komga/src/test/resources/archives/zip-as-epub.epub b/komga/src/test/resources/archives/zip-as-epub.epub new file mode 100644 index 000000000..bc224d487 Binary files /dev/null and b/komga/src/test/resources/archives/zip-as-epub.epub differ