Skip to content

Commit

Permalink
fix: regenerate thumbnail if first page is removed as duplicate
Browse files Browse the repository at this point in the history
Closes: #1078
  • Loading branch information
gotson committed Mar 6, 2023
1 parent 13444f8 commit cf2a5a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gotson.komga.application.tasks

import io.micrometer.core.instrument.MeterRegistry
import mu.KotlinLogging
import org.gotson.komga.domain.model.BookAction
import org.gotson.komga.domain.persistence.BookRepository
import org.gotson.komga.domain.persistence.LibraryRepository
import org.gotson.komga.domain.persistence.SeriesRepository
Expand Down Expand Up @@ -149,7 +150,9 @@ class TaskHandler(

is Task.RemoveHashedPages ->
bookRepository.findByIdOrNull(task.bookId)?.let { book ->
bookPageEditor.removeHashedPages(book, task.pages)
if (bookPageEditor.removeHashedPages(book, task.pages) == BookAction.GENERATE_THUMBNAIL) {
taskEmitter.generateBookThumbnail(book, priority = task.priority + 1)
}
} ?: logger.warn { "Cannot execute task $task: Book does not exist" }

is Task.HashBook ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.gotson.komga.domain.model

enum class BookAction {
REFRESH_METADATA,
GENERATE_THUMBNAIL,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import org.apache.commons.io.FilenameUtils
import org.gotson.komga.application.events.EventPublisher
import org.gotson.komga.domain.model.Book
import org.gotson.komga.domain.model.BookAction
import org.gotson.komga.domain.model.BookConversionException
import org.gotson.komga.domain.model.BookPageNumbered
import org.gotson.komga.domain.model.BookWithMedia
Expand Down Expand Up @@ -52,14 +53,18 @@ class BookPageEditor(

private val failedPageRemoval = mutableListOf<String>()

fun removeHashedPages(book: Book, pagesToDelete: Collection<BookPageNumbered>) {
fun removeHashedPages(book: Book, pagesToDelete: Collection<BookPageNumbered>): BookAction? {
// perform various checks
if (failedPageRemoval.contains(book.id))
return logger.info { "Book page removal already failed before, skipping" }
if (failedPageRemoval.contains(book.id)) {
logger.info { "Book page removal already failed before, skipping" }
return null
}

fileSystemScanner.scanFile(book.path)?.let { scannedBook ->
if (scannedBook.fileLastModified.notEquals(book.fileLastModified))
return logger.info { "Book has changed on disk, skipping. Db: ${book.fileLastModified}. Scanned: ${scannedBook.fileLastModified}" }
if (scannedBook.fileLastModified.notEquals(book.fileLastModified)) {
logger.info { "Book has changed on disk, skipping. Db: ${book.fileLastModified}. Scanned: ${scannedBook.fileLastModified}" }
return null
}
} ?: throw FileNotFoundException("File not found: ${book.path}")

val media = mediaRepository.findById(book.id)
Expand All @@ -79,8 +84,10 @@ class BookPageEditor(
candidate.pageNumber == index + 1
} == null
}
if (media.pages.size != (pagesToKeep.size + pagesToDelete.size))
return logger.info { "Should be removing ${pagesToDelete.size} pages from book, but count doesn't add up, skipping" }
if (media.pages.size != (pagesToKeep.size + pagesToDelete.size)) {
logger.info { "Should be removing ${pagesToDelete.size} pages from book, but count doesn't add up, skipping" }
return null
}

logger.info { "Start removal of ${pagesToDelete.size} pages for book: $book" }
logger.debug { "Pages: ${media.pages}" }
Expand Down Expand Up @@ -156,5 +163,7 @@ class BookPageEditor(

pagesToDelete.forEach { historicalEventRepository.insert(HistoricalEvent.DuplicatePageDeleted(book, it)) }
eventPublisher.publishEvent(DomainEvent.BookUpdated(newBook))

return if (pagesToDelete.any { it.pageNumber == 1 }) BookAction.GENERATE_THUMBNAIL else null
}
}

0 comments on commit cf2a5a2

Please sign in to comment.