refactor: move exception handling inside BookAnalyzer.kt

This commit is contained in:
Gauthier Roebroeck 2021-04-19 17:14:10 +08:00
parent 25d6272e0f
commit 34f77a83fc
4 changed files with 33 additions and 34 deletions

View file

@ -12,6 +12,7 @@ import org.gotson.komga.infrastructure.image.ImageConverter
import org.gotson.komga.infrastructure.mediacontainer.ContentDetector
import org.gotson.komga.infrastructure.mediacontainer.MediaContainerExtractor
import org.springframework.stereotype.Service
import java.nio.file.AccessDeniedException
private val logger = KotlinLogging.logger {}
@ -31,20 +32,20 @@ class BookAnalyzer(
fun analyze(book: Book): Media {
logger.info { "Trying to analyze book: $book" }
try {
val mediaType = contentDetector.detectMediaType(book.path())
logger.info { "Detected media type: $mediaType" }
if (!supportedMediaTypes.containsKey(mediaType))
return Media(mediaType = mediaType, status = Media.Status.UNSUPPORTED, comment = "ERR_1001", bookId = book.id)
val mediaType = contentDetector.detectMediaType(book.path())
logger.info { "Detected media type: $mediaType" }
if (!supportedMediaTypes.containsKey(mediaType))
return Media(mediaType = mediaType, status = Media.Status.UNSUPPORTED, comment = "ERR_1001")
val entries = try {
supportedMediaTypes.getValue(mediaType).getEntries(book.path())
} catch (ex: MediaUnsupportedException) {
return Media(mediaType = mediaType, status = Media.Status.UNSUPPORTED, comment = ex.code)
} catch (ex: Exception) {
logger.error(ex) { "Error while analyzing book: $book" }
return Media(mediaType = mediaType, status = Media.Status.ERROR, comment = "ERR_1008")
}
val entries = try {
supportedMediaTypes.getValue(mediaType).getEntries(book.path())
} catch (ex: MediaUnsupportedException) {
return Media(mediaType = mediaType, status = Media.Status.UNSUPPORTED, comment = ex.code, bookId = book.id)
} catch (ex: Exception) {
logger.error(ex) { "Error while analyzing book: $book" }
return Media(mediaType = mediaType, status = Media.Status.ERROR, comment = "ERR_1008", bookId = book.id)
}
val (pages, others) = entries
.partition { entry ->
@ -62,15 +63,22 @@ class BookAnalyzer(
.ifEmpty { null }
?.joinToString(prefix = "ERR_1007 [", postfix = "]") { it }
if (pages.isEmpty()) {
logger.warn { "Book $book does not contain any pages" }
return Media(mediaType = mediaType, status = Media.Status.ERROR, comment = "ERR_1006")
}
logger.info { "Book has ${pages.size} pages" }
if (pages.isEmpty()) {
logger.warn { "Book $book does not contain any pages" }
return Media(mediaType = mediaType, status = Media.Status.ERROR, comment = "ERR_1006", bookId = book.id)
}
logger.info { "Book has ${pages.size} pages" }
val files = others.map { it.name }
return Media(mediaType = mediaType, status = Media.Status.READY, pages = pages, files = files, comment = entriesErrorSummary)
return Media(mediaType = mediaType, status = Media.Status.READY, pages = pages, 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)
} catch (ex: Exception) {
logger.error(ex) { "Error while analyzing book: $book" }
return Media(status = Media.Status.ERROR, comment = "ERR_1005", bookId = book.id)
}
}
@Throws(MediaNotReadyException::class)

View file

@ -20,7 +20,6 @@ import org.gotson.komga.infrastructure.image.ImageConverter
import org.gotson.komga.infrastructure.image.ImageType
import org.springframework.stereotype.Service
import java.io.File
import java.nio.file.AccessDeniedException
import java.nio.file.Files
import java.nio.file.Paths
@ -40,15 +39,7 @@ class BookLifecycle(
fun analyzeAndPersist(book: Book): Boolean {
logger.info { "Analyze and persist book: $book" }
val media = try {
bookAnalyzer.analyze(book)
} catch (ade: AccessDeniedException) {
logger.error(ade) { "Error while analyzing book: $book" }
Media(status = Media.Status.ERROR, comment = "ERR_1000")
} catch (ex: Exception) {
logger.error(ex) { "Error while analyzing book: $book" }
Media(status = Media.Status.ERROR, comment = "ERR_1005")
}.copy(bookId = book.id)
val media = bookAnalyzer.analyze(book)
// if the number of pages has changed, delete all read progress for that book
mediaRepository.findById(book.id).let { previous ->

View file

@ -35,7 +35,7 @@ class BookLifecycleTest(
@Autowired private val seriesLifecycle: SeriesLifecycle,
@Autowired private val readProgressRepository: ReadProgressRepository,
@Autowired private val mediaRepository: MediaRepository,
@Autowired private val userRepository: KomgaUserRepository
@Autowired private val userRepository: KomgaUserRepository,
) {
@MockkBean
@ -90,7 +90,7 @@ class BookLifecycleTest(
assertThat(readProgressRepository.findAll()).hasSize(2)
// when
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")))
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")), bookId = book.id)
bookLifecycle.analyzeAndPersist(book)
// then
@ -123,7 +123,7 @@ class BookLifecycleTest(
assertThat(readProgressRepository.findAll()).hasSize(2)
// when
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = (1..10).map { BookPage("$it", "image/jpeg") })
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = (1..10).map { BookPage("$it", "image/jpeg") }, bookId = book.id)
bookLifecycle.analyzeAndPersist(book)
// then

View file

@ -200,7 +200,7 @@ class LibraryContentLifecycleTest(
)
libraryContentLifecycle.scanRootFolder(library)
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")))
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")), bookId = book1.id)
bookRepository.findAll().map { bookLifecycle.analyzeAndPersist(it) }
// when
@ -236,7 +236,7 @@ class LibraryContentLifecycleTest(
)
libraryContentLifecycle.scanRootFolder(library)
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")))
every { mockAnalyzer.analyze(any()) } returns Media(status = Media.Status.READY, mediaType = "application/zip", pages = mutableListOf(makeBookPage("1.jpg"), makeBookPage("2.jpg")), bookId = book1.id)
bookRepository.findAll().map { bookLifecycle.analyzeAndPersist(it) }
// when