From 21e3e7a2690b84f3eb8adc0b61f94ef9e26fd34a Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Tue, 14 Nov 2023 16:14:28 +0800 Subject: [PATCH] refactor(komga): make Media.pageCount explicit --- .../kotlin/org/gotson/komga/domain/model/Media.kt | 1 + .../org/gotson/komga/domain/service/BookAnalyzer.kt | 12 ++++++------ .../org/gotson/komga/domain/service/BookLifecycle.kt | 6 +++--- .../komga/infrastructure/jooq/main/MediaDao.kt | 5 +++-- .../metadata/barcode/IsbnBarcodeProvider.kt | 2 +- .../gotson/komga/interfaces/api/dto/WepPubHelper.kt | 2 +- .../komga/interfaces/api/opds/v1/OpdsController.kt | 4 ++-- .../gotson/komga/domain/service/BookImporterTest.kt | 1 + .../gotson/komga/domain/service/BookLifecycleTest.kt | 2 ++ .../komga/interfaces/api/rest/BookControllerTest.kt | 3 +++ .../interfaces/api/rest/SeriesControllerTest.kt | 1 + 11 files changed, 24 insertions(+), 15 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/model/Media.kt b/komga/src/main/kotlin/org/gotson/komga/domain/model/Media.kt index 58a1b9e9..f124db32 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/model/Media.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/model/Media.kt @@ -6,6 +6,7 @@ data class Media( val status: Status = Status.UNKNOWN, val mediaType: String? = null, val pages: List = emptyList(), + val pageCount: Int = pages.size, val files: List = emptyList(), val comment: String? = null, val bookId: String = "", 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 b2fb3a8b..0715391c 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 @@ -84,7 +84,7 @@ class BookAnalyzer( val files = others.map { it.name } - return Media(mediaType = mediaType, status = Media.Status.READY, pages = pages, files = files, comment = entriesErrorSummary, bookId = book.id) + return Media(mediaType = mediaType, status = Media.Status.READY, pages = pages, pageCount = pages.size, files = files, comment = entriesErrorSummary, bookId = book.id) } catch (ade: AccessDeniedException) { logger.error(ade) { "Error while analyzing book: $book" } return Media(status = Media.Status.ERROR, comment = "ERR_1000", bookId = book.id) @@ -150,8 +150,8 @@ class BookAnalyzer( throw MediaNotReadyException() } - if (number > book.media.pages.size || number <= 0) { - logger.error { "Page number #$number is out of bounds. Book has ${book.media.pages.size} pages" } + if (number > book.media.pageCount || number <= 0) { + logger.error { "Page number #$number is out of bounds. Book has ${book.media.pageCount} pages" } throw IndexOutOfBoundsException("Page $number does not exist") } @@ -170,8 +170,8 @@ class BookAnalyzer( throw MediaNotReadyException() } - if (number > book.media.pages.size || number <= 0) { - logger.error { "Page number #$number is out of bounds. Book has ${book.media.pages.size} pages" } + if (number > book.media.pageCount || number <= 0) { + logger.error { "Page number #$number is out of bounds. Book has ${book.media.pageCount} pages" } throw IndexOutOfBoundsException("Page $number does not exist") } @@ -203,7 +203,7 @@ class BookAnalyzer( */ fun hashPages(book: BookWithMedia): Media { val hashedPages = book.media.pages.mapIndexed { index, bookPage -> - if (bookPage.fileHash.isBlank() && (index < pageHashing || index >= (book.media.pages.size - pageHashing))) { + if (bookPage.fileHash.isBlank() && (index < pageHashing || index >= (book.media.pageCount - pageHashing))) { val content = getPageContent(book, index + 1) val hash = hashPage(bookPage, content) bookPage.copy(fileHash = hash) diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookLifecycle.kt b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookLifecycle.kt index 3824902b..55dde7f5 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/service/BookLifecycle.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/service/BookLifecycle.kt @@ -69,9 +69,9 @@ class BookLifecycle( transactionTemplate.executeWithoutResult { // if the number of pages has changed, delete all read progress for that book mediaRepository.findById(book.id).let { previous -> - if (previous.status == Media.Status.OUTDATED && previous.pages.size != media.pages.size) { + if (previous.status == Media.Status.OUTDATED && previous.pageCount != media.pageCount) { val adjustedProgress = readProgressRepository.findAllByBookId(book.id) - .map { it.copy(page = if (it.completed) media.pages.size else 1) } + .map { it.copy(page = if (it.completed) media.pageCount else 1) } if (adjustedProgress.isNotEmpty()) { logger.info { "Number of pages differ, adjust read progress for book" } readProgressRepository.save(adjustedProgress) @@ -345,7 +345,7 @@ class BookLifecycle( fun markReadProgressCompleted(bookId: String, user: KomgaUser) { val media = mediaRepository.findById(bookId) - val progress = ReadProgress(bookId, user.id, media.pages.size, true) + val progress = ReadProgress(bookId, user.id, media.pageCount, true) readProgressRepository.save(progress) eventPublisher.publishEvent(DomainEvent.ReadProgressChanged(progress)) } diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/main/MediaDao.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/main/MediaDao.kt index 29637f1e..a91a8b3a 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/main/MediaDao.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/main/MediaDao.kt @@ -114,7 +114,7 @@ class MediaDao( it.status, it.mediaType, it.comment, - it.pages.size, + it.pageCount, ) } }.execute() @@ -189,7 +189,7 @@ class MediaDao( .set(m.STATUS, media.status.toString()) .set(m.MEDIA_TYPE, media.mediaType) .set(m.COMMENT, media.comment) - .set(m.PAGE_COUNT, media.pages.size) + .set(m.PAGE_COUNT, media.pageCount) .set(m.LAST_MODIFIED_DATE, LocalDateTime.now(ZoneId.of("Z"))) .where(m.BOOK_ID.eq(media.bookId)) .execute() @@ -229,6 +229,7 @@ class MediaDao( status = Media.Status.valueOf(status), mediaType = mediaType, pages = pages, + pageCount = pageCount, files = files, comment = comment, bookId = bookId, diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/metadata/barcode/IsbnBarcodeProvider.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/metadata/barcode/IsbnBarcodeProvider.kt index 72da63e3..5d8fafe6 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/metadata/barcode/IsbnBarcodeProvider.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/metadata/barcode/IsbnBarcodeProvider.kt @@ -39,7 +39,7 @@ class IsbnBarcodeProvider( setOf(BookMetadataPatchCapability.ISBN) override fun getBookMetadataFromBook(book: BookWithMedia): BookMetadataPatch? { - val pagesToTry = (1..book.media.pages.size).toList().let { + val pagesToTry = (1..book.media.pageCount).toList().let { (it.takeLast(PAGES_LAST).reversed() + it.take(PAGES_FIRST)).distinct() } diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/dto/WepPubHelper.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/dto/WepPubHelper.kt index 2fd86af6..b9eba90d 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/dto/WepPubHelper.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/dto/WepPubHelper.kt @@ -92,7 +92,7 @@ fun BookDto.toManifestPdf(media: Media, seriesMetadata: SeriesMetadata): WPPubli metadata = it.metadata .withSeriesMetadata(seriesMetadata) .copy(conformsTo = PROFILE_PDF), - readingOrder = List(media.pages.size) { index: Int -> + readingOrder = List(media.pageCount) { index: Int -> WPLinkDto( href = uriBuilder.cloneBuilder().path("books/$id/pages/${index + 1}/raw").toUriString(), type = PDF.type, diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/opds/v1/OpdsController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/opds/v1/OpdsController.kt index 5d4d607c..873401eb 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/api/opds/v1/OpdsController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/api/opds/v1/OpdsController.kt @@ -684,9 +684,9 @@ class OpdsController( val mediaTypes = media.pages.map { it.mediaType }.distinct() val opdsLinkPageStreaming = if (mediaTypes.size == 1 && mediaTypes.first() in opdsPseSupportedFormats) { - OpdsLinkPageStreaming(mediaTypes.first(), uriBuilder("books/$id/pages/").toUriString() + "{pageNumber}", media.pages.size, readProgress?.page, readProgress?.readDate) + OpdsLinkPageStreaming(mediaTypes.first(), uriBuilder("books/$id/pages/").toUriString() + "{pageNumber}", media.pageCount, readProgress?.page, readProgress?.readDate) } else { - OpdsLinkPageStreaming("image/jpeg", uriBuilder("books/$id/pages/").toUriString() + "{pageNumber}?convert=jpeg", media.pages.size, readProgress?.page, readProgress?.readDate) + OpdsLinkPageStreaming("image/jpeg", uriBuilder("books/$id/pages/").toUriString() + "{pageNumber}?convert=jpeg", media.pageCount, readProgress?.page, readProgress?.readDate) } return OpdsEntryAcquisition( diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt index 09c7cf5d..a030a0ac 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookImporterTest.kt @@ -448,6 +448,7 @@ class BookImporterTest( media.copy( status = Media.Status.READY, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } diff --git a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookLifecycleTest.kt b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookLifecycleTest.kt index c153e4e4..8815fd36 100644 --- a/komga/src/test/kotlin/org/gotson/komga/domain/service/BookLifecycleTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/domain/service/BookLifecycleTest.kt @@ -90,6 +90,7 @@ class BookLifecycleTest( media.copy( status = Media.Status.OUTDATED, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } @@ -130,6 +131,7 @@ class BookLifecycleTest( media.copy( status = Media.Status.OUTDATED, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } diff --git a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/BookControllerTest.kt b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/BookControllerTest.kt index 02c18939..473cfad7 100644 --- a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/BookControllerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/BookControllerTest.kt @@ -1195,6 +1195,7 @@ class BookControllerTest( media.copy( status = Media.Status.READY, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } @@ -1237,6 +1238,7 @@ class BookControllerTest( media.copy( status = Media.Status.READY, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } @@ -1279,6 +1281,7 @@ class BookControllerTest( media.copy( status = Media.Status.READY, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) } diff --git a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/SeriesControllerTest.kt b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/SeriesControllerTest.kt index 61372791..381fe3f7 100644 --- a/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/SeriesControllerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/interfaces/api/rest/SeriesControllerTest.kt @@ -1067,6 +1067,7 @@ class SeriesControllerTest( media.copy( status = Media.Status.READY, pages = (1..10).map { BookPage("$it", "image/jpeg") }, + pageCount = 10, ), ) }