diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/model/Exceptions.kt b/komga/src/main/kotlin/org/gotson/komga/domain/model/Exceptions.kt index a9613c45d..a1538e15d 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/model/Exceptions.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/model/Exceptions.kt @@ -24,3 +24,4 @@ class PathContainedInPath(message: String, code: String = "") : CodedException(m class UserEmailAlreadyExistsException(message: String, code: String = "") : CodedException(message, code) class BookConversionException(message: String) : Exception(message) class ComicRackListException(message: String, code: String = "") : CodedException(message, code) +class EntryNotFoundException(message: String) : Exception(message) diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/epub/EpubExtractor.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/epub/EpubExtractor.kt index d61299bb3..0eeed14e0 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/epub/EpubExtractor.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/epub/EpubExtractor.kt @@ -4,6 +4,7 @@ import mu.KotlinLogging import org.apache.commons.compress.archivers.ArchiveEntry import org.apache.commons.compress.archivers.zip.ZipFile import org.gotson.komga.domain.model.BookPage +import org.gotson.komga.domain.model.EntryNotFoundException import org.gotson.komga.domain.model.EpubTocEntry import org.gotson.komga.domain.model.MediaFile import org.gotson.komga.domain.model.R2Locator @@ -33,7 +34,8 @@ class EpubExtractor( */ fun getEntryStream(path: Path, entryName: String): ByteArray = ZipFile(path.toFile()).use { zip -> - zip.getInputStream(zip.getEntry(entryName)).use { it.readBytes() } + zip.getEntry(entryName)?.let { entry -> zip.getInputStream(entry).use { it.readBytes() } } + ?: throw EntryNotFoundException("Entry does not exist: $entryName") } fun isEpub(path: Path): Boolean = @@ -173,17 +175,17 @@ class EpubExtractor( readingOrder.map { R2Locator( href = it.fileName, - type = it.mediaType!!, + type = it.mediaType ?: "application/octet-stream", locations = R2Locator.Location(progression = 0F, position = startPosition++), ) } } else { readingOrder.flatMap { file -> - val positionCount = maxOf(1, ceil(file.fileSize!! / 1024.0).roundToInt()) + val positionCount = maxOf(1, ceil((file.fileSize ?: 0) / 1024.0).roundToInt()) (0 until positionCount).map { p -> R2Locator( href = file.fileName, - type = file.mediaType!!, + type = file.mediaType ?: "application/octet-stream", locations = R2Locator.Location(progression = p.toFloat() / positionCount, position = startPosition++), ) } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt index 274c6effb..28fdcef4a 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/rest/BookController.kt @@ -19,6 +19,7 @@ import org.gotson.komga.domain.model.BookSearchWithReadProgress import org.gotson.komga.domain.model.BookWithMedia import org.gotson.komga.domain.model.Dimension import org.gotson.komga.domain.model.DomainEvent +import org.gotson.komga.domain.model.EntryNotFoundException import org.gotson.komga.domain.model.ImageConversionException import org.gotson.komga.domain.model.KomgaUser import org.gotson.komga.domain.model.MarkSelectedPreference @@ -701,7 +702,11 @@ class BookController( if (!isFont) principal!!.user.checkContentRestriction(book) val res = media.files.firstOrNull { it.fileName == resourceName } ?: throw ResponseStatusException(HttpStatus.NOT_FOUND) - val bytes = bookAnalyzer.getFileContent(BookWithMedia(book, media), resourceName) + val bytes = try { + bookAnalyzer.getFileContent(BookWithMedia(book, media), resourceName) + } catch (e: EntryNotFoundException) { + throw ResponseStatusException(HttpStatus.NOT_FOUND) + } return ResponseEntity.ok() .headers(