From 2ccff8cdde39348538a86e9e88c9e1f134520706 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 4 Feb 2024 15:27:46 +0800 Subject: [PATCH] Fix extracting ComicInfo.xml files in local source (#325) --- .../tachiyomi/source/local/LocalSource.kt | 27 +++++++++---------- .../source/local/io/LocalSourceFileSystem.kt | 5 +--- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt index 10aa1e4640..b1ae25236f 100644 --- a/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt +++ b/source-local/src/androidMain/kotlin/tachiyomi/source/local/LocalSource.kt @@ -41,7 +41,6 @@ import tachiyomi.source.local.io.Format import tachiyomi.source.local.io.LocalSourceFileSystem 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 kotlin.time.Duration.Companion.days @@ -145,8 +144,8 @@ actual class LocalSource( // 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 } @@ -176,7 +175,7 @@ actual class LocalSource( // 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) @@ -191,14 +190,12 @@ actual class LocalSource( .filter(Archive::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") } } } @@ -209,14 +206,14 @@ actual class LocalSource( return@withIOContext manga } - private fun copyComicInfoFileFromArchive(chapterArchives: List, folderPath: String?): File? { + private fun copyComicInfoFileFromArchive(chapterArchives: List, 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) } } } @@ -225,7 +222,7 @@ actual class LocalSource( 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) } } } @@ -236,9 +233,9 @@ actual class LocalSource( 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) } } } diff --git a/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt b/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt index ad95b39ce7..402df7e006 100644 --- a/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt +++ b/source-local/src/androidMain/kotlin/tachiyomi/source/local/io/LocalSourceFileSystem.kt @@ -22,9 +22,6 @@ actual class LocalSourceFileSystem( } actual fun getFilesInMangaDirectory(name: String): List { - return getBaseDirectory() - ?.findFile(name, true) - ?.takeIf { it.isDirectory } - ?.listFiles().orEmpty().toList() + return getMangaDirectory(name)?.listFiles().orEmpty().toList() } }