mirror of
https://github.com/gotson/komga.git
synced 2025-12-28 19:39:20 +01:00
refactor: use of path extension api
This commit is contained in:
parent
32ba89419b
commit
3db3181313
9 changed files with 34 additions and 28 deletions
|
|
@ -5,8 +5,8 @@ import com.jakewharton.byteunits.BinaryByteUnit
|
|||
import java.io.Serializable
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.io.path.toPath
|
||||
|
||||
data class Book(
|
||||
val name: String,
|
||||
|
|
@ -24,7 +24,7 @@ data class Book(
|
|||
) : Auditable(), Serializable {
|
||||
|
||||
@delegate:Transient
|
||||
val path: Path by lazy { Paths.get(this.url.toURI()) }
|
||||
val path: Path by lazy { this.url.toURI().toPath() }
|
||||
|
||||
@delegate:Transient
|
||||
val fileSizeHumanReadable: String by lazy { BinaryByteUnit.format(fileSize) }
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import com.github.f4b6a3.tsid.TsidCreator
|
|||
import java.io.Serializable
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.io.path.toPath
|
||||
|
||||
data class Library(
|
||||
val name: String,
|
||||
|
|
@ -30,5 +30,5 @@ data class Library(
|
|||
) : Auditable(), Serializable {
|
||||
|
||||
@delegate:Transient
|
||||
val path: Path by lazy { Paths.get(this.root.toURI()) }
|
||||
val path: Path by lazy { this.root.toURI().toPath() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ import com.github.f4b6a3.tsid.TsidCreator
|
|||
import java.io.Serializable
|
||||
import java.net.URL
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.time.LocalDateTime
|
||||
import kotlin.io.path.toPath
|
||||
|
||||
data class Series(
|
||||
val name: String,
|
||||
|
|
@ -21,5 +21,5 @@ data class Series(
|
|||
) : Auditable(), Serializable {
|
||||
|
||||
@delegate:Transient
|
||||
val path: Path by lazy { Paths.get(this.url.toURI()) }
|
||||
val path: Path by lazy { this.url.toURI().toPath() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import kotlin.io.path.deleteIfExists
|
|||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.io.path.moveTo
|
||||
import kotlin.io.path.name
|
||||
import kotlin.io.path.notExists
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
|
@ -57,8 +58,8 @@ class BookImporter(
|
|||
}
|
||||
|
||||
val destFile = series.path.resolve(
|
||||
if (destinationName != null) Paths.get("$destinationName.${sourceFile.extension}").fileName.toString()
|
||||
else sourceFile.fileName.toString()
|
||||
if (destinationName != null) Paths.get("$destinationName.${sourceFile.extension}").name
|
||||
else sourceFile.name
|
||||
)
|
||||
|
||||
val upgradedBook =
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package org.gotson.komga.domain.service
|
||||
|
||||
import mu.KotlinLogging
|
||||
import org.apache.commons.io.FilenameUtils
|
||||
import org.gotson.komga.domain.model.Book
|
||||
import org.gotson.komga.domain.model.DirectoryNotFoundException
|
||||
import org.gotson.komga.domain.model.ScanResult
|
||||
|
|
@ -23,7 +22,10 @@ import java.nio.file.attribute.FileTime
|
|||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.io.path.name
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
import kotlin.io.path.pathString
|
||||
import kotlin.io.path.readAttributes
|
||||
import kotlin.time.measureTime
|
||||
|
||||
|
|
@ -72,14 +74,14 @@ class FileSystemScanner(
|
|||
object : FileVisitor<Path> {
|
||||
override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||
logger.trace { "preVisit: $dir" }
|
||||
if (dir.fileName?.toString()?.startsWith(".") == true ||
|
||||
if (dir.name.startsWith(".") ||
|
||||
komgaProperties.librariesScanDirectoryExclusions.any { exclude ->
|
||||
dir.toString().contains(exclude, true)
|
||||
dir.pathString.contains(exclude, true)
|
||||
}
|
||||
) return FileVisitResult.SKIP_SUBTREE
|
||||
|
||||
pathToSeries[dir] = Series(
|
||||
name = dir.fileName?.toString() ?: dir.toString(),
|
||||
name = dir.name.ifBlank { dir.pathString },
|
||||
url = dir.toUri().toURL(),
|
||||
fileLastModified = attrs.getUpdatedTime()
|
||||
)
|
||||
|
|
@ -90,8 +92,8 @@ class FileSystemScanner(
|
|||
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||
logger.trace { "visitFile: $file" }
|
||||
if (attrs.isRegularFile) {
|
||||
if (supportedExtensions.contains(FilenameUtils.getExtension(file.fileName.toString()).lowercase()) &&
|
||||
!file.fileName.toString().startsWith(".")
|
||||
if (supportedExtensions.contains(file.extension.lowercase()) &&
|
||||
!file.name.startsWith(".")
|
||||
) {
|
||||
val book = pathToBook(file, attrs)
|
||||
file.parent.let { key ->
|
||||
|
|
@ -174,7 +176,7 @@ class FileSystemScanner(
|
|||
|
||||
private fun pathToBook(path: Path, attrs: BasicFileAttributes): Book =
|
||||
Book(
|
||||
name = FilenameUtils.getBaseName(path.name),
|
||||
name = path.nameWithoutExtension,
|
||||
url = path.toUri().toURL(),
|
||||
fileLastModified = attrs.getUpdatedTime(),
|
||||
fileSize = attrs.size()
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import org.apache.tika.metadata.Metadata
|
|||
import org.springframework.stereotype.Service
|
||||
import java.io.InputStream
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.name
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
|
|
@ -17,7 +18,7 @@ class ContentDetector(
|
|||
|
||||
fun detectMediaType(path: Path): String {
|
||||
val metadata = Metadata().also {
|
||||
it[Metadata.RESOURCE_NAME_KEY] = path.fileName.toString()
|
||||
it[Metadata.RESOURCE_NAME_KEY] = path.name
|
||||
}
|
||||
|
||||
return TikaInputStream.get(path).use {
|
||||
|
|
|
|||
|
|
@ -2,13 +2,13 @@ package org.gotson.komga.infrastructure.mediacontainer
|
|||
|
||||
import mu.KotlinLogging
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile
|
||||
import org.apache.commons.io.FilenameUtils
|
||||
import org.gotson.komga.domain.model.MediaContainerEntry
|
||||
import org.gotson.komga.infrastructure.image.ImageAnalyzer
|
||||
import org.jsoup.Jsoup
|
||||
import org.springframework.stereotype.Service
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.invariantSeparatorsPathString
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ class EpubExtractor(
|
|||
val images = pages
|
||||
.map { opfDir?.resolve(it)?.normalize() ?: Paths.get(it) }
|
||||
.flatMap { pagePath ->
|
||||
val doc = zip.getInputStream(zip.getEntry(pagePath.separatorsToUnix())).use { Jsoup.parse(it, null, "") }
|
||||
val doc = zip.getInputStream(zip.getEntry(pagePath.invariantSeparatorsPathString)).use { Jsoup.parse(it, null, "") }
|
||||
|
||||
val img = doc.getElementsByTag("img")
|
||||
.map { it.attr("src") } // get the src, which can be a relative path
|
||||
|
|
@ -51,9 +51,9 @@ class EpubExtractor(
|
|||
}
|
||||
|
||||
return images.map { image ->
|
||||
val name = image.separatorsToUnix()
|
||||
val name = image.invariantSeparatorsPathString
|
||||
val mediaType = manifest.values.first {
|
||||
it.href == (opfDir?.relativize(image) ?: image).separatorsToUnix()
|
||||
it.href == (opfDir?.relativize(image) ?: image).invariantSeparatorsPathString
|
||||
}.mediaType
|
||||
val dimension = if (contentDetector.isImage(mediaType))
|
||||
zip.getInputStream(zip.getEntry(name)).use { imageAnalyzer.getDimension(it) }
|
||||
|
|
@ -89,8 +89,6 @@ class EpubExtractor(
|
|||
|
||||
fun Path.parentOrEmpty(): Path = parent ?: Paths.get("")
|
||||
|
||||
fun Path.separatorsToUnix(): String = FilenameUtils.separatorsToUnix(this.toString())
|
||||
|
||||
private data class ManifestItem(
|
||||
val id: String,
|
||||
val href: String,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ import org.gotson.komga.infrastructure.sidecar.SidecarBookConsumer
|
|||
import org.gotson.komga.infrastructure.sidecar.SidecarSeriesConsumer
|
||||
import org.springframework.stereotype.Service
|
||||
import java.nio.file.Files
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
import kotlin.streams.asSequence
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
|
@ -27,15 +29,15 @@ class LocalArtworkProvider(
|
|||
fun getBookThumbnails(book: Book): List<ThumbnailBook> {
|
||||
logger.info { "Looking for local thumbnails for book: $book" }
|
||||
val bookPath = book.path
|
||||
val baseName = FilenameUtils.getBaseName(bookPath.toString())
|
||||
val baseName = bookPath.nameWithoutExtension
|
||||
|
||||
val regex = "${Regex.escape(baseName)}(-\\d+)?".toRegex(RegexOption.IGNORE_CASE)
|
||||
|
||||
return Files.list(bookPath.parent).use { dirStream ->
|
||||
dirStream.asSequence()
|
||||
.filter { Files.isRegularFile(it) }
|
||||
.filter { regex.matches(FilenameUtils.getBaseName(it.toString())) }
|
||||
.filter { supportedExtensions.contains(FilenameUtils.getExtension(it.fileName.toString()).lowercase()) }
|
||||
.filter { regex.matches(it.nameWithoutExtension) }
|
||||
.filter { supportedExtensions.contains(it.extension.lowercase()) }
|
||||
.filter { contentDetector.isImage(contentDetector.detectMediaType(it)) }
|
||||
.mapIndexed { index, path ->
|
||||
logger.info { "Found file: $path" }
|
||||
|
|
@ -55,8 +57,8 @@ class LocalArtworkProvider(
|
|||
return Files.list(series.path).use { dirStream ->
|
||||
dirStream.asSequence()
|
||||
.filter { Files.isRegularFile(it) }
|
||||
.filter { supportedSeriesFiles.contains(FilenameUtils.getBaseName(it.toString().lowercase())) }
|
||||
.filter { supportedExtensions.contains(FilenameUtils.getExtension(it.fileName.toString()).lowercase()) }
|
||||
.filter { supportedSeriesFiles.contains(it.nameWithoutExtension.lowercase()) }
|
||||
.filter { supportedExtensions.contains(it.extension.lowercase()) }
|
||||
.filter { contentDetector.isImage(contentDetector.detectMediaType(it)) }
|
||||
.mapIndexed { index, path ->
|
||||
logger.info { "Found file: $path" }
|
||||
|
|
|
|||
|
|
@ -6,9 +6,11 @@ import org.springframework.http.ResponseEntity
|
|||
import java.net.URL
|
||||
import java.nio.file.Paths
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.io.path.pathString
|
||||
import kotlin.io.path.toPath
|
||||
|
||||
fun URL.toFilePath(): String =
|
||||
Paths.get(this.toURI()).toString()
|
||||
this.toURI().toPath().pathString
|
||||
|
||||
fun filePathToUrl(filePath: String): URL =
|
||||
Paths.get(filePath).toUri().toURL()
|
||||
|
|
|
|||
Loading…
Reference in a new issue