From a64e68d646855b0cd282edf6523bda2277ccaa99 Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Tue, 28 Jul 2020 17:47:18 +0800 Subject: [PATCH] refactor: use files instead of streams for rar files --- komga/build.gradle.kts | 2 +- .../mediacontainer/RarExtractor.kt | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/komga/build.gradle.kts b/komga/build.gradle.kts index 7e305296d..1edc7795e 100644 --- a/komga/build.gradle.kts +++ b/komga/build.gradle.kts @@ -77,7 +77,7 @@ dependencies { implementation("org.apache.tika:tika-core:1.24.1") implementation("org.apache.commons:commons-compress:1.20") - implementation("com.github.junrar:junrar:4.0.0") + implementation("com.github.junrar:junrar:7.2.0") implementation("org.apache.pdfbox:pdfbox:2.0.19") implementation("net.grey-panther:natural-comparator:1.1") implementation("org.jsoup:jsoup:1.13.1") diff --git a/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/RarExtractor.kt b/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/RarExtractor.kt index 8d7ae092e..8681b922c 100644 --- a/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/RarExtractor.kt +++ b/komga/src/main/kotlin/org/gotson/komga/infrastructure/mediacontainer/RarExtractor.kt @@ -6,9 +6,8 @@ import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator import org.gotson.komga.domain.model.MediaContainerEntry import org.gotson.komga.domain.model.MediaUnsupportedException import org.springframework.stereotype.Service -import java.nio.file.Files import java.nio.file.Path -import java.util.* +import java.util.Comparator private val logger = KotlinLogging.logger {} @@ -22,26 +21,26 @@ class RarExtractor( override fun mediaTypes(): List = listOf("application/x-rar-compressed", "application/x-rar-compressed; version=4") override fun getEntries(path: Path): List = - Archive(Files.newInputStream(path)).use { rar -> - if (rar.mainHeader.isEncrypted) throw MediaUnsupportedException("Encrypted RAR archives are not supported") + Archive(path.toFile()).use { rar -> + if (rar.isPasswordProtected) throw MediaUnsupportedException("Encrypted RAR archives are not supported") if (rar.mainHeader.isSolid) throw MediaUnsupportedException("Solid RAR archives are not supported") if (rar.mainHeader.isMultiVolume) throw MediaUnsupportedException("Multi-Volume RAR archives are not supported") rar.fileHeaders .filter { !it.isDirectory } .map { try { - MediaContainerEntry(name = it.fileNameString, mediaType = contentDetector.detectMediaType(rar.getInputStream(it))) + MediaContainerEntry(name = it.fileName, mediaType = contentDetector.detectMediaType(rar.getInputStream(it))) } catch (e: Exception) { - logger.warn(e) { "Could not analyze entry: ${it.fileNameString}" } - MediaContainerEntry(name = it.fileNameString, comment = e.message) + logger.warn(e) { "Could not analyze entry: ${it.fileName}" } + MediaContainerEntry(name = it.fileName, comment = e.message) } } .sortedWith(compareBy(natSortComparator) { it.name }) } override fun getEntryStream(path: Path, entryName: String): ByteArray = - Archive(Files.newInputStream(path)).use { rar -> - val header = rar.fileHeaders.find { it.fileNameString == entryName } - rar.getInputStream(header).readBytes() + Archive(path.toFile()).use { rar -> + val header = rar.fileHeaders.find { it.fileName == entryName } + rar.getInputStream(header).readBytes() } }