fix: soft delete after deleting files instead of triggering a scan

This commit is contained in:
Gauthier Roebroeck 2022-02-08 15:07:53 +08:00
parent b53fbc7217
commit dabe398274
3 changed files with 19 additions and 19 deletions

View file

@ -141,14 +141,12 @@ class TaskHandler(
is Task.DeleteBook -> {
bookRepository.findByIdOrNull(task.bookId)?.let { book ->
bookLifecycle.deleteBookFiles(book)
taskReceiver.scanLibrary(book.libraryId, task.priority)
}
}
is Task.DeleteSeries -> {
seriesRepository.findByIdOrNull(task.seriesId)?.let { series ->
seriesLifecycle.deleteSeriesFiles(series)
taskReceiver.scanLibrary(series.libraryId, task.priority)
}
}
}

View file

@ -13,7 +13,6 @@ import org.gotson.komga.domain.model.Media
import org.gotson.komga.domain.model.MediaNotReadyException
import org.gotson.komga.domain.model.ReadProgress
import org.gotson.komga.domain.model.ThumbnailBook
import org.gotson.komga.domain.model.withCode
import org.gotson.komga.domain.persistence.BookMetadataRepository
import org.gotson.komga.domain.persistence.BookRepository
import org.gotson.komga.domain.persistence.LibraryRepository
@ -27,9 +26,7 @@ import org.gotson.komga.infrastructure.image.ImageType
import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionTemplate
import java.io.File
import java.io.FileNotFoundException
import java.time.LocalDateTime
import kotlin.io.path.deleteExisting
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
import kotlin.io.path.isWritable
@ -323,17 +320,21 @@ class BookLifecycle(
}
fun deleteBookFiles(book: Book) {
if (book.path.notExists() || !book.path.isWritable())
throw FileNotFoundException("File is not accessible : ${book.path}").withCode("ERR_1018")
if (book.path.notExists()) return logger.info { "Cannot delete book file, path does not exist: ${book.path}" }
if (!book.path.isWritable()) return logger.info { "Cannot delete book file, path is not writable: ${book.path}" }
val thumbnails = thumbnailBookRepository.findAllByBookIdAndType(book.id, ThumbnailBook.Type.SIDECAR)
.mapNotNull { it.url?.toURI()?.toPath() }
.filter { it.exists() && it.isWritable() }
book.path.deleteIfExists()
thumbnails.forEach { it.deleteIfExists() }
if (book.path.deleteIfExists()) logger.info { "Deleted file: ${book.path}" }
thumbnails.forEach {
if (it.deleteIfExists()) logger.info { "Deleted file: $it" }
}
if (book.path.parent.listDirectoryEntries().isEmpty())
book.path.parent.deleteExisting()
if (book.path.parent.deleteIfExists()) logger.info { "Deleted directory: ${book.path.parent}" }
softDeleteMany(listOf(book))
}
}

View file

@ -18,7 +18,6 @@ import org.gotson.komga.domain.model.ReadProgress
import org.gotson.komga.domain.model.Series
import org.gotson.komga.domain.model.SeriesMetadata
import org.gotson.komga.domain.model.ThumbnailSeries
import org.gotson.komga.domain.model.withCode
import org.gotson.komga.domain.persistence.BookMetadataAggregationRepository
import org.gotson.komga.domain.persistence.BookMetadataRepository
import org.gotson.komga.domain.persistence.BookRepository
@ -33,8 +32,6 @@ import org.gotson.komga.infrastructure.language.stripAccents
import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionTemplate
import java.io.File
import java.io.FileNotFoundException
import java.nio.file.Path
import java.time.LocalDateTime
import kotlin.io.path.deleteIfExists
import kotlin.io.path.exists
@ -96,8 +93,8 @@ class SeriesLifecycle(
book, metadata,
metadata.copy(
number = if (!metadata.numberLock) (index + 1).toString() else metadata.number,
numberSort = if (!metadata.numberSortLock) (index + 1).toFloat() else metadata.numberSort
)
numberSort = if (!metadata.numberSortLock) (index + 1).toFloat() else metadata.numberSort,
),
)
}
bookMetadataRepository.update(oldToNew.map { it.third })
@ -294,8 +291,8 @@ class SeriesLifecycle(
}
fun deleteSeriesFiles(series: Series) {
if (series.path.notExists() || !series.path.isWritable())
throw FileNotFoundException("File is not accessible : ${series.path}").withCode("ERR_1018")
if (series.path.notExists()) return logger.info { "Cannot delete series folder, path does not exist: ${series.path}" }
if (!series.path.isWritable()) return logger.info { "Cannot delete series folder, path is not writable: ${series.path}" }
val thumbnails = thumbnailsSeriesRepository.findAllBySeriesIdIdAndType(series.id, ThumbnailSeries.Type.SIDECAR)
.mapNotNull { it.url?.toURI()?.toPath() }
@ -303,10 +300,14 @@ class SeriesLifecycle(
bookRepository.findAllBySeriesId(series.id)
.forEach { bookLifecycle.deleteBookFiles(it) }
thumbnails.forEach(Path::deleteIfExists)
thumbnails.forEach {
if (it.deleteIfExists()) logger.info { "Deleted file: $it" }
}
if (series.path.exists() && series.path.listDirectoryEntries().isEmpty())
series.path.deleteIfExists()
if (series.path.deleteIfExists()) logger.info { "Deleted directory: ${series.path}" }
softDeleteMany(listOf(series))
}
private fun thumbnailsHouseKeeping(seriesId: String) {