Enhance download cancellation in blocking io tasks #90
This commit is contained in:
@@ -145,7 +145,7 @@ class DownloadManager(
|
|||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
val response = call.clone().await()
|
val response = call.clone().await()
|
||||||
withContext(Dispatchers.IO) {
|
runInterruptible(Dispatchers.IO) {
|
||||||
file.outputStream().use { out ->
|
file.outputStream().use { out ->
|
||||||
checkNotNull(response.body).byteStream().copyTo(out)
|
checkNotNull(response.body).byteStream().copyTo(out)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class MangaZip(val file: File) {
|
|||||||
return writableCbz.flush()
|
return writableCbz.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addCover(file: File, ext: String) {
|
suspend fun addCover(file: File, ext: String) {
|
||||||
val name = buildString {
|
val name = buildString {
|
||||||
append(FILENAME_PATTERN.format(0, 0))
|
append(FILENAME_PATTERN.format(0, 0))
|
||||||
if (ext.isNotEmpty() && ext.length <= 4) {
|
if (ext.isNotEmpty() && ext.length <= 4) {
|
||||||
@@ -39,11 +39,11 @@ class MangaZip(val file: File) {
|
|||||||
append(ext)
|
append(ext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writableCbz[name] = file
|
writableCbz.put(name, file)
|
||||||
index.setCoverEntry(name)
|
index.setCoverEntry(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addPage(chapter: MangaChapter, file: File, pageNumber: Int, ext: String) {
|
suspend fun addPage(chapter: MangaChapter, file: File, pageNumber: Int, ext: String) {
|
||||||
val name = buildString {
|
val name = buildString {
|
||||||
append(FILENAME_PATTERN.format(chapter.number, pageNumber))
|
append(FILENAME_PATTERN.format(chapter.number, pageNumber))
|
||||||
if (ext.isNotEmpty() && ext.length <= 4) {
|
if (ext.isNotEmpty() && ext.length <= 4) {
|
||||||
@@ -51,7 +51,7 @@ class MangaZip(val file: File) {
|
|||||||
append(ext)
|
append(ext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writableCbz[name] = file
|
writableCbz.put(name, file)
|
||||||
index.addChapter(chapter)
|
index.addChapter(chapter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package org.koitharu.kotatsu.local.data
|
package org.koitharu.kotatsu.local.data
|
||||||
|
|
||||||
import androidx.annotation.CheckResult
|
import androidx.annotation.CheckResult
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileInputStream
|
import java.io.FileInputStream
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
@@ -27,11 +26,13 @@ class WritableCbzFile(private val file: File) {
|
|||||||
}
|
}
|
||||||
ZipInputStream(FileInputStream(file)).use { zip ->
|
ZipInputStream(FileInputStream(file)).use { zip ->
|
||||||
var entry = zip.nextEntry
|
var entry = zip.nextEntry
|
||||||
while (entry != null) {
|
while (entry != null && currentCoroutineContext().isActive) {
|
||||||
val target = File(dir.path + File.separator + entry.name)
|
val target = File(dir.path + File.separator + entry.name)
|
||||||
target.parentFile?.mkdirs()
|
runInterruptible {
|
||||||
target.outputStream().use { out ->
|
target.parentFile?.mkdirs()
|
||||||
zip.copyTo(out)
|
target.outputStream().use { out ->
|
||||||
|
zip.copyTo(out)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
zip.closeEntry()
|
zip.closeEntry()
|
||||||
entry = zip.nextEntry
|
entry = zip.nextEntry
|
||||||
@@ -51,11 +52,13 @@ class WritableCbzFile(private val file: File) {
|
|||||||
tempFile.delete()
|
tempFile.delete()
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ZipOutputStream(FileOutputStream(tempFile)).use { zip ->
|
runInterruptible {
|
||||||
dir.listFiles()?.forEach {
|
ZipOutputStream(FileOutputStream(tempFile)).use { zip ->
|
||||||
zipFile(it, it.name, zip)
|
dir.listFiles()?.forEach {
|
||||||
|
zipFile(it, it.name, zip)
|
||||||
|
}
|
||||||
|
zip.flush()
|
||||||
}
|
}
|
||||||
zip.flush()
|
|
||||||
}
|
}
|
||||||
tempFile.renameTo(file)
|
tempFile.renameTo(file)
|
||||||
} finally {
|
} finally {
|
||||||
@@ -67,29 +70,26 @@ class WritableCbzFile(private val file: File) {
|
|||||||
|
|
||||||
operator fun get(name: String) = File(dir, name)
|
operator fun get(name: String) = File(dir, name)
|
||||||
|
|
||||||
operator fun set(name: String, file: File) {
|
suspend fun put(name: String, file: File) = runInterruptible(Dispatchers.IO) {
|
||||||
file.copyTo(this[name], overwrite = true)
|
file.copyTo(this[name], overwrite = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
private fun zipFile(fileToZip: File, fileName: String, zipOut: ZipOutputStream) {
|
||||||
|
if (fileToZip.isDirectory) {
|
||||||
private fun zipFile(fileToZip: File, fileName: String, zipOut: ZipOutputStream) {
|
if (fileName.endsWith("/")) {
|
||||||
if (fileToZip.isDirectory) {
|
zipOut.putNextEntry(ZipEntry(fileName))
|
||||||
if (fileName.endsWith("/")) {
|
|
||||||
zipOut.putNextEntry(ZipEntry(fileName))
|
|
||||||
} else {
|
|
||||||
zipOut.putNextEntry(ZipEntry("$fileName/"))
|
|
||||||
}
|
|
||||||
zipOut.closeEntry()
|
|
||||||
fileToZip.listFiles()?.forEach { childFile ->
|
|
||||||
zipFile(childFile, "$fileName/${childFile.name}", zipOut)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
FileInputStream(fileToZip).use { fis ->
|
zipOut.putNextEntry(ZipEntry("$fileName/"))
|
||||||
val zipEntry = ZipEntry(fileName)
|
}
|
||||||
zipOut.putNextEntry(zipEntry)
|
zipOut.closeEntry()
|
||||||
fis.copyTo(zipOut)
|
fileToZip.listFiles()?.forEach { childFile ->
|
||||||
}
|
zipFile(childFile, "$fileName/${childFile.name}", zipOut)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
FileInputStream(fileToZip).use { fis ->
|
||||||
|
val zipEntry = ZipEntry(fileName)
|
||||||
|
zipOut.putNextEntry(zipEntry)
|
||||||
|
fis.copyTo(zipOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user