Fix zip closing

master
Koitharu 2 years ago
parent e515069b53
commit d28eff7a75
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -103,10 +103,13 @@ class ZipOutput(
} }
val zipEntry = ZipEntry(name) val zipEntry = ZipEntry(name)
putNextEntry(zipEntry) putNextEntry(zipEntry)
try {
fis.copyTo(this) fis.copyTo(this)
} finally {
closeEntry() closeEntry()
} }
} }
}
return true return true
} }
@ -117,8 +120,11 @@ class ZipOutput(
} }
val zipEntry = ZipEntry(name) val zipEntry = ZipEntry(name)
putNextEntry(zipEntry) putNextEntry(zipEntry)
try {
content.byteInputStream().copyTo(this) content.byteInputStream().copyTo(this)
} finally {
closeEntry() closeEntry()
}
return true return true
} }
} }

@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import okhttp3.internal.closeQuietly
import org.koitharu.kotatsu.core.model.isLocal import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.util.ext.deleteAwait import org.koitharu.kotatsu.core.util.ext.deleteAwait
import org.koitharu.kotatsu.core.util.ext.takeIfReadable import org.koitharu.kotatsu.core.util.ext.takeIfReadable
@ -90,7 +91,7 @@ class LocalMangaDirOutput(
override fun close() { override fun close() {
for (output in chaptersOutput.values) { for (output in chaptersOutput.values) {
output.close() output.closeQuietly()
} }
} }
@ -119,10 +120,21 @@ class LocalMangaDirOutput(
} }
private suspend fun ZipOutput.flushAndFinish() = runInterruptible(Dispatchers.IO) { private suspend fun ZipOutput.flushAndFinish() = runInterruptible(Dispatchers.IO) {
val e: Throwable? = try {
finish() finish()
null
} catch (e: Throwable) {
e
} finally {
close() close()
}
if (e == null) {
val resFile = File(file.absolutePath.removeSuffix(SUFFIX_TMP)) val resFile = File(file.absolutePath.removeSuffix(SUFFIX_TMP))
file.renameTo(resFile) file.renameTo(resFile)
} else {
file.delete()
throw e
}
} }
private fun chapterFileName(chapter: IndexedValue<MangaChapter>): String { private fun chapterFileName(chapter: IndexedValue<MangaChapter>): String {

@ -2,8 +2,6 @@ package org.koitharu.kotatsu.local.data.output
import androidx.core.net.toFile import androidx.core.net.toFile
import androidx.core.net.toUri import androidx.core.net.toUri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible
import org.koitharu.kotatsu.core.model.isLocal import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
@ -16,26 +14,14 @@ class LocalMangaUtil(
} }
suspend fun deleteChapters(ids: Set<Long>) { suspend fun deleteChapters(ids: Set<Long>) {
newOutput().use { output -> val file = manga.url.toUri().toFile()
when (output) { if (file.isDirectory) {
is LocalMangaZipOutput -> runInterruptible(Dispatchers.IO) { LocalMangaDirOutput(file, manga).use { output ->
LocalMangaZipOutput.filterChapters(output, ids)
}
is LocalMangaDirOutput -> {
output.deleteChapters(ids) output.deleteChapters(ids)
output.finish() output.finish()
} }
}
}
}
private suspend fun newOutput(): LocalMangaOutput = runInterruptible(Dispatchers.IO) {
val file = manga.url.toUri().toFile()
if (file.isDirectory) {
LocalMangaDirOutput(file, manga)
} else { } else {
LocalMangaZipOutput(file, manga) LocalMangaZipOutput.filterChapters(file, manga, ids)
} }
} }
} }

@ -5,6 +5,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import okhttp3.internal.closeQuietly
import org.koitharu.kotatsu.core.model.isLocal import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.util.ext.deleteAwait import org.koitharu.kotatsu.core.util.ext.deleteAwait
import org.koitharu.kotatsu.core.util.ext.readText import org.koitharu.kotatsu.core.util.ext.readText
@ -52,7 +53,8 @@ class LocalMangaZipOutput(
index.setCoverEntry(name) index.setCoverEntry(name)
} }
override suspend fun addPage(chapter: IndexedValue<MangaChapter>, file: File, pageNumber: Int, ext: String) = mutex.withLock { override suspend fun addPage(chapter: IndexedValue<MangaChapter>, file: File, pageNumber: Int, ext: String) =
mutex.withLock {
val name = buildString { val name = buildString {
append(FILENAME_PATTERN.format(chapter.value.branch.hashCode(), chapter.index + 1, pageNumber)) append(FILENAME_PATTERN.format(chapter.value.branch.hashCode(), chapter.index + 1, pageNumber))
if (ext.isNotEmpty() && ext.length <= 4) { if (ext.isNotEmpty() && ext.length <= 4) {
@ -70,9 +72,10 @@ class LocalMangaZipOutput(
override suspend fun finish() = mutex.withLock { override suspend fun finish() = mutex.withLock {
runInterruptible(Dispatchers.IO) { runInterruptible(Dispatchers.IO) {
output.use { output ->
output.put(ENTRY_NAME_INDEX, index.toString()) output.put(ENTRY_NAME_INDEX, index.toString())
output.finish() output.finish()
output.close() }
} }
rootFile.deleteAwait() rootFile.deleteAwait()
output.file.renameTo(rootFile) output.file.renameTo(rootFile)
@ -115,8 +118,10 @@ class LocalMangaZipOutput(
private const val FILENAME_PATTERN = "%08d_%03d%03d" private const val FILENAME_PATTERN = "%08d_%03d%03d"
@WorkerThread suspend fun filterChapters(file: File, manga: Manga, idsToRemove: Set<Long>) =
fun filterChapters(subject: LocalMangaZipOutput, idsToRemove: Set<Long>) { runInterruptible(Dispatchers.IO) {
val subject = LocalMangaZipOutput(file, manga)
try {
ZipFile(subject.rootFile).use { zip -> ZipFile(subject.rootFile).use { zip ->
val index = MangaIndex(zip.readText(zip.getEntry(ENTRY_NAME_INDEX))) val index = MangaIndex(zip.readText(zip.getEntry(ENTRY_NAME_INDEX)))
idsToRemove.forEach { id -> index.removeChapter(id) } idsToRemove.forEach { id -> index.removeChapter(id) }
@ -151,6 +156,15 @@ class LocalMangaZipOutput(
subject.rootFile.delete() subject.rootFile.delete()
subject.output.file.renameTo(subject.rootFile) subject.output.file.renameTo(subject.rootFile)
} }
} catch (e: Throwable) {
subject.closeQuietly()
try {
subject.output.file.delete()
} catch (e2: Throwable) {
e.addSuppressed(e2)
}
throw e
}
} }
} }
} }

Loading…
Cancel
Save