Fix extracting ComicInfo.xml files in local source

Co-authored-by: FooIbar <118464521+FooIbar@users.noreply.github.com>
This commit is contained in:
Secozzi 2024-06-05 14:49:26 +02:00
parent 0424b60b3e
commit b4f0d91481
No known key found for this signature in database
GPG key ID: 71E9C97D8DDC2662
2 changed files with 13 additions and 19 deletions

View file

@ -42,7 +42,6 @@ import tachiyomi.source.local.io.Format
import tachiyomi.source.local.io.manga.LocalMangaSourceFileSystem
import tachiyomi.source.local.metadata.fillMetadata
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
@ -148,8 +147,8 @@ actual class LocalMangaSource(
// Augment manga details based on metadata files
try {
val mangaDir by lazy { fileSystem.getMangaDirectory(manga.url) }
val mangaDirFiles = fileSystem.getFilesInMangaDirectory(manga.url)
val mangaDir = fileSystem.getMangaDirectory(manga.url) ?: error("${manga.url} is not a valid directory")
val mangaDirFiles = mangaDir.listFiles().orEmpty()
val comicInfoFile = mangaDirFiles
.firstOrNull { it.name == COMIC_INFO_FILE }
@ -179,7 +178,7 @@ actual class LocalMangaSource(
// Replace with ComicInfo.xml file
val comicInfo = manga.getComicInfo()
mangaDir
?.createFile(COMIC_INFO_FILE)
.createFile(COMIC_INFO_FILE)
?.openOutputStream()
?.use {
val comicInfoString = xml.encodeToString(ComicInfo.serializer(), comicInfo)
@ -194,14 +193,12 @@ actual class LocalMangaSource(
.filter(ArchiveManga::isSupported)
.toList()
val folderPath = mangaDir?.filePath
val copiedFile = copyComicInfoFileFromArchive(chapterArchives, folderPath)
val copiedFile = copyComicInfoFileFromArchive(chapterArchives, mangaDir)
if (copiedFile != null) {
setMangaDetailsFromComicInfoFile(copiedFile.inputStream(), manga)
setMangaDetailsFromComicInfoFile(copiedFile.openInputStream(), manga)
} else {
// Avoid re-scanning
mangaDir?.createFile(".noxml")
mangaDir.createFile(".noxml")
}
}
}
@ -215,14 +212,14 @@ actual class LocalMangaSource(
return@withIOContext manga
}
private fun copyComicInfoFileFromArchive(chapterArchives: List<UniFile>, folderPath: String?): File? {
private fun copyComicInfoFileFromArchive(chapterArchives: List<UniFile>, folder: UniFile): UniFile? {
for (chapter in chapterArchives) {
when (Format.valueOf(chapter)) {
is Format.Zip -> {
ZipFile(chapter.openReadOnlyChannel(context)).use { zip: ZipFile ->
zip.getEntry(COMIC_INFO_FILE)?.let { comicInfoFile ->
zip.getInputStream(comicInfoFile).buffered().use { stream ->
return copyComicInfoFile(stream, folderPath)
return copyComicInfoFile(stream, folder)
}
}
}
@ -231,7 +228,7 @@ actual class LocalMangaSource(
JunrarArchive(chapter.openInputStream()).use { rar ->
rar.fileHeaders.firstOrNull { it.fileName == COMIC_INFO_FILE }?.let { comicInfoFile ->
rar.getInputStream(comicInfoFile).buffered().use { stream ->
return copyComicInfoFile(stream, folderPath)
return copyComicInfoFile(stream, folder)
}
}
}
@ -242,9 +239,9 @@ actual class LocalMangaSource(
return null
}
private fun copyComicInfoFile(comicInfoFileStream: InputStream, folderPath: String?): File {
return File("$folderPath/$COMIC_INFO_FILE").apply {
outputStream().use { outputStream ->
private fun copyComicInfoFile(comicInfoFileStream: InputStream, folder: UniFile): UniFile? {
return folder.createFile(COMIC_INFO_FILE)?.apply {
openOutputStream().use { outputStream ->
comicInfoFileStream.use { it.copyTo(outputStream) }
}
}

View file

@ -22,9 +22,6 @@ actual class LocalMangaSourceFileSystem(
}
actual fun getFilesInMangaDirectory(name: String): List<UniFile> {
return getBaseDirectory()
?.findFile(name, true)
?.takeIf { it.isDirectory }
?.listFiles().orEmpty().toList()
return getMangaDirectory(name)?.listFiles().orEmpty().toList()
}
}