Fix local manga operations

This commit is contained in:
Koitharu
2023-03-30 18:11:43 +03:00
parent 056538a341
commit bd27eb9f59
5 changed files with 32 additions and 37 deletions

View File

@@ -39,8 +39,7 @@ class ExceptionResolver private constructor(
sourceAuthContract = fragment.registerForActivityResult(SourceAuthActivity.Contract(), this)
}
override fun onActivityResult(result: TaggedActivityResult?) {
result ?: return
override fun onActivityResult(result: TaggedActivityResult) {
continuations.remove(result.tag)?.resume(result.isSuccess)
}

View File

@@ -34,9 +34,10 @@ class LocalMangaDirOutput(
}
}
runInterruptible(Dispatchers.IO) {
file.copyTo(File(rootFile, name))
file.copyTo(File(rootFile, name), overwrite = true)
}
index.setCoverEntry(name)
flushIndex()
}
override suspend fun addPage(chapter: MangaChapter, file: File, pageNumber: Int, ext: String) {
@@ -59,12 +60,11 @@ class LocalMangaDirOutput(
override suspend fun flushChapter(chapter: MangaChapter) {
val output = chaptersOutput.remove(chapter) ?: return
output.flushAndFinish()
flushIndex()
}
override suspend fun finish() {
runInterruptible(Dispatchers.IO) {
File(rootFile, ENTRY_NAME_INDEX).writeText(index.toString())
}
flushIndex()
for (output in chaptersOutput.values) {
output.flushAndFinish()
}
@@ -103,6 +103,10 @@ class LocalMangaDirOutput(
return "${chapter.number}_${chapter.name.toFileNameSafe()}".take(18) + ".cbz"
}
private suspend fun flushIndex() = runInterruptible(Dispatchers.IO) {
File(rootFile, ENTRY_NAME_INDEX).writeText(index.toString())
}
companion object {
private const val FILENAME_PATTERN = "%08d_%03d%03d"

View File

@@ -12,11 +12,9 @@ import kotlinx.coroutines.runInterruptible
import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.local.data.LocalManga
import org.koitharu.kotatsu.local.data.LocalStorageManager
import org.koitharu.kotatsu.local.data.MangaIndex
import org.koitharu.kotatsu.local.data.TempFileFilter
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
import org.koitharu.kotatsu.local.data.output.LocalMangaDirOutput
import org.koitharu.kotatsu.local.data.output.LocalMangaOutput
import org.koitharu.kotatsu.local.data.output.LocalMangaZipOutput
import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaChapter
@@ -27,10 +25,9 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.utils.AlphanumComparator
import org.koitharu.kotatsu.utils.CompositeMutex
import org.koitharu.kotatsu.utils.ext.deleteAwait
import org.koitharu.kotatsu.utils.ext.readText
import org.koitharu.kotatsu.utils.ext.printStackTraceDebug
import org.koitharu.kotatsu.utils.ext.runCatchingCancellable
import java.io.File
import java.util.zip.ZipFile
import javax.inject.Inject
import javax.inject.Singleton
@@ -114,16 +111,11 @@ class LocalMangaRepository @Inject constructor(private val storageManager: Local
}
suspend fun getRemoteManga(localManga: Manga): Manga? {
val file = runCatching {
Uri.parse(localManga.url).toFile()
}.getOrNull() ?: return null
return runInterruptible(Dispatchers.IO) {
ZipFile(file).use { zip ->
val entry = zip.getEntry(LocalMangaOutput.ENTRY_NAME_INDEX)
val index = entry?.let(zip::readText)?.let(::MangaIndex)
index?.getMangaInfo()
}
}
return runCatchingCancellable {
LocalMangaInput.of(localManga).getMangaInfo()
}.onFailure {
it.printStackTraceDebug()
}.getOrNull()
}
suspend fun findSavedManga(remoteManga: Manga): LocalManga? {

View File

@@ -47,7 +47,7 @@ fun File.getStorageName(context: Context): String = runCatching {
fun Uri.toFileOrNull() = if (scheme == "file") path?.let(::File) else null
suspend fun File.deleteAwait() = withContext(Dispatchers.IO) {
delete()
delete() || deleteRecursively()
}
fun ContentResolver.resolveName(uri: Uri): String? {