mirror of
https://github.com/gotson/komga.git
synced 2026-05-09 05:10:19 +02:00
refactor: media container extractors are self descriptive
This commit is contained in:
parent
74be1f0fac
commit
cde2756960
6 changed files with 24 additions and 25 deletions
|
|
@ -8,9 +8,7 @@ import org.gotson.komga.domain.model.Media
|
|||
import org.gotson.komga.domain.model.MediaNotReadyException
|
||||
import org.gotson.komga.infrastructure.image.ImageConverter
|
||||
import org.gotson.komga.infrastructure.mediacontainer.ContentDetector
|
||||
import org.gotson.komga.infrastructure.mediacontainer.PdfExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.RarExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.ZipExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.MediaContainerExtractor
|
||||
import org.springframework.stereotype.Service
|
||||
import java.util.*
|
||||
|
||||
|
|
@ -19,17 +17,13 @@ private val logger = KotlinLogging.logger {}
|
|||
@Service
|
||||
class BookAnalyzer(
|
||||
private val contentDetector: ContentDetector,
|
||||
private val zipExtractor: ZipExtractor,
|
||||
private val rarExtractor: RarExtractor,
|
||||
private val pdfExtractor: PdfExtractor,
|
||||
extractors: List<MediaContainerExtractor>,
|
||||
private val imageConverter: ImageConverter
|
||||
) {
|
||||
|
||||
val supportedMediaTypes = mapOf(
|
||||
"application/zip" to zipExtractor,
|
||||
"application/x-rar-compressed" to rarExtractor,
|
||||
"application/pdf" to pdfExtractor
|
||||
)
|
||||
val supportedMediaTypes = extractors
|
||||
.flatMap { e -> e.mediaTypes().map { it to e } }
|
||||
.toMap()
|
||||
|
||||
private val natSortComparator: Comparator<String> = CaseInsensitiveSimpleNaturalComparator.getInstance()
|
||||
|
||||
|
|
@ -41,7 +35,7 @@ class BookAnalyzer(
|
|||
|
||||
val mediaType = contentDetector.detectMediaType(book.path())
|
||||
logger.info { "Detected media type: $mediaType" }
|
||||
if (!supportedMediaTypes.keys.contains(mediaType))
|
||||
if (!supportedMediaTypes.containsKey(mediaType))
|
||||
return Media(mediaType = mediaType, status = Media.Status.UNSUPPORTED, comment = "Media type $mediaType is not supported")
|
||||
|
||||
val entries = try {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ package org.gotson.komga.infrastructure.mediacontainer
|
|||
import org.gotson.komga.domain.model.MediaContainerEntry
|
||||
import java.nio.file.Path
|
||||
|
||||
abstract class MediaContainerExtractor {
|
||||
abstract fun getEntries(path: Path): List<MediaContainerEntry>
|
||||
abstract fun getEntryStream(path: Path, entryName: String): ByteArray
|
||||
interface MediaContainerExtractor {
|
||||
fun mediaTypes(): List<String>
|
||||
fun getEntries(path: Path): List<MediaContainerEntry>
|
||||
fun getEntryStream(path: Path, entryName: String): ByteArray
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,12 +11,14 @@ import java.nio.file.Path
|
|||
import javax.imageio.ImageIO
|
||||
|
||||
@Service
|
||||
class PdfExtractor : MediaContainerExtractor() {
|
||||
class PdfExtractor : MediaContainerExtractor {
|
||||
|
||||
private val mediaType = "image/jpeg"
|
||||
private val imageIOFormat = "jpeg"
|
||||
private val resolution = 1536F
|
||||
|
||||
override fun mediaTypes(): List<String> = listOf("application/pdf")
|
||||
|
||||
override fun getEntries(path: Path): List<MediaContainerEntry> =
|
||||
Files.newInputStream(path).use { inputStream ->
|
||||
PDDocument.load(inputStream).use { pdf ->
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ private val logger = KotlinLogging.logger {}
|
|||
@Service
|
||||
class RarExtractor(
|
||||
private val contentDetector: ContentDetector
|
||||
) : MediaContainerExtractor() {
|
||||
) : MediaContainerExtractor {
|
||||
|
||||
override fun mediaTypes(): List<String> = listOf("application/x-rar-compressed")
|
||||
|
||||
override fun getEntries(path: Path): List<MediaContainerEntry> =
|
||||
Archive(Files.newInputStream(path)).use { rar ->
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ private val logger = KotlinLogging.logger {}
|
|||
@Service
|
||||
class ZipExtractor(
|
||||
private val contentDetector: ContentDetector
|
||||
) : MediaContainerExtractor() {
|
||||
) : MediaContainerExtractor {
|
||||
|
||||
override fun mediaTypes(): List<String> = listOf("application/zip")
|
||||
|
||||
override fun getEntries(path: Path): List<MediaContainerEntry> =
|
||||
ZipFile(path.toFile()).use { zip ->
|
||||
|
|
|
|||
|
|
@ -8,19 +8,17 @@ import org.gotson.komga.domain.model.MediaContainerEntry
|
|||
import org.gotson.komga.domain.model.makeBook
|
||||
import org.gotson.komga.infrastructure.image.ImageConverter
|
||||
import org.gotson.komga.infrastructure.mediacontainer.ContentDetector
|
||||
import org.gotson.komga.infrastructure.mediacontainer.PdfExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.RarExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.ZipExtractor
|
||||
import org.gotson.komga.infrastructure.mediacontainer.MediaContainerExtractor
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class BookAnalyzerTest {
|
||||
private val mockContent = mockk<ContentDetector>()
|
||||
private val mockZip = mockk<ZipExtractor>()
|
||||
private val mockRar = mockk<RarExtractor>()
|
||||
private val mockPDf = mockk<PdfExtractor>()
|
||||
private val mockZip = mockk<MediaContainerExtractor>().also {
|
||||
every { it.mediaTypes() } returns listOf("application/zip")
|
||||
}
|
||||
private val mockImageConverter = mockk<ImageConverter>()
|
||||
|
||||
private val bookAnalyzer = BookAnalyzer(mockContent, mockZip, mockRar, mockPDf, mockImageConverter)
|
||||
private val bookAnalyzer = BookAnalyzer(mockContent, listOf(mockZip), mockImageConverter)
|
||||
|
||||
@Test
|
||||
fun `given book with unordered pages when analyzing then thumbnail should always be the first in natural order`() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue