diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/PageHashRepository.kt b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/PageHashRepository.kt index 08bd7931a90..d7cb81103e8 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/persistence/PageHashRepository.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/persistence/PageHashRepository.kt @@ -1,5 +1,6 @@ package org.gotson.komga.domain.persistence +import org.gotson.komga.domain.model.BookPageNumbered import org.gotson.komga.domain.model.PageHash import org.gotson.komga.domain.model.PageHashKnown import org.gotson.komga.domain.model.PageHashMatch @@ -13,6 +14,7 @@ interface PageHashRepository { fun findAllUnknown(pageable: Pageable): Page fun findMatchesByHash(pageHash: PageHash, libraryId: String?, pageable: Pageable): Page + fun findMatchesByKnownHashAction(actions: List?, libraryId: String?): Map> fun getKnownThumbnail(pageHash: PageHash): ByteArray? diff --git a/komga/src/main/kotlin/org/gotson/komga/domain/service/PageHashLifecycle.kt b/komga/src/main/kotlin/org/gotson/komga/domain/service/PageHashLifecycle.kt index c3001d92b01..ab20058c1f9 100644 --- a/komga/src/main/kotlin/org/gotson/komga/domain/service/PageHashLifecycle.kt +++ b/komga/src/main/kotlin/org/gotson/komga/domain/service/PageHashLifecycle.kt @@ -46,28 +46,8 @@ class PageHashLifecycle( return bookLifecycle.getBookPage(book, match.pageNumber, resizeTo = resizeTo) } - fun getBookPagesToDeleteAutomatically(library: Library): Map> { - val hashesAutoDelete = pageHashRepository.findAllKnown(listOf(PageHashKnown.Action.DELETE_AUTO), Pageable.unpaged()).content - - return hashesAutoDelete.map { hash -> - pageHashRepository.findMatchesByHash(hash, library.id, Pageable.unpaged()).content - .groupBy( - { it.bookId }, - { - BookPageNumbered( - fileName = it.fileName, - mediaType = hash.mediaType, - fileHash = hash.hash, - fileSize = hash.size, - pageNumber = it.pageNumber, - ) - }, - ) - }.flatMap { it.entries } - .groupBy({ it.key }, { it.value }) - .mapValues { it.value.flatten() } - .filter { it.value.isNotEmpty() } - } + fun getBookPagesToDeleteAutomatically(library: Library): Map> = + pageHashRepository.findMatchesByKnownHashAction(listOf(PageHashKnown.Action.DELETE_AUTO), library.id) fun createOrUpdate(pageHash: PageHashKnown) { if (pageHash.action == PageHashKnown.Action.DELETE_AUTO && pageHash.size == null) throw IllegalArgumentException("cannot create PageHash without size and Action.DELETE_AUTO") diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/PageHashDao.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/PageHashDao.kt index 2b76afebea6..0145e6fe7bf 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/PageHashDao.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/jooq/PageHashDao.kt @@ -1,5 +1,6 @@ package org.gotson.komga.infrastructure.jooq +import org.gotson.komga.domain.model.BookPageNumbered import org.gotson.komga.domain.model.PageHash import org.gotson.komga.domain.model.PageHashKnown import org.gotson.komga.domain.model.PageHashMatch @@ -164,6 +165,30 @@ class PageHashDao( ) } + override fun findMatchesByKnownHashAction(actions: List?, libraryId: String?): Map> = + dsl.select(p.BOOK_ID, p.FILE_NAME, p.NUMBER, p.FILE_HASH, p.MEDIA_TYPE, p.FILE_SIZE) + .from(p) + .innerJoin(ph).on( + p.FILE_HASH.eq(ph.HASH) + .and(p.MEDIA_TYPE.eq(ph.MEDIA_TYPE)) + .and( + p.FILE_SIZE.isNull.and(ph.SIZE.isNull).or(p.FILE_SIZE.isNotNull.and(ph.SIZE.isNotNull).and(p.FILE_SIZE.eq(ph.SIZE))) + ) + ) + .apply { libraryId?.let { innerJoin(b).on(b.ID.eq(p.BOOK_ID)) } } + .where(ph.ACTION.`in`(actions)) + .apply { libraryId?.let { and(b.LIBRARY_ID.eq(it)) } } + .fetch { + it.value1() to BookPageNumbered( + fileName = it.value2(), + pageNumber = it.value3() + 1, + fileHash = it.value4(), + mediaType = it.value5(), + fileSize = it.value6(), + ) + }.groupingBy { it.first } + .fold(emptyList()) { acc, (_, new) -> acc + new } + override fun getKnownThumbnail(pageHash: PageHash): ByteArray? = dsl.select(pht.THUMBNAIL) .from(pht)