Clean up unfinished download files

This commit is contained in:
FooIbar authored and tarsin committed 2023-02-20 15:15:00 +08:00
1 parent 4a6b85003a
commit 16f3f74a90
2 files changed
+13 -3

No files matched your search

@@ -38,6 +38,7 @@ import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.MathUtils
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import moe.tarsin.coroutines.runSuspendCatching
import okhttp3.executeAsync
import okio.buffer
import java.io.File
@@ -197,7 +198,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
return receivedSize
}
findDownloadFileForIndex(index, extension)?.runCatching {
findDownloadFileForIndex(index, extension)?.runSuspendCatching {
openOutputStream().use { outputStream ->
(outputStream as FileOutputStream).channel.use { return doSave(it) == length }
}
@@ -210,7 +211,7 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
if (mMode == SpiderQueen.MODE_READ) {
val key = EhCacheKeyFactory.getImageKey(mGid, index)
var received: Long = 0
runCatching {
runSuspendCatching {
sCache.edit(key) {
metadata.toFile().writeText(extension)
data.toFile().outputStream().use { outputStream ->
@@ -241,7 +241,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
}
private fun stop() {
launchNonCancellable { writeSpiderInfoToLocal() }
launchNonCancellable { runCatching { writeSpiderInfoToLocal() } }
cancel()
}
@@ -570,6 +570,15 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
mSemaphore.withPermit {
doInJob(index, force)
}
}.apply {
invokeOnCompletion {
if (getPageState(index) == STATE_DOWNLOADING) {
Log.d(WORKER_DEBUG_TAG, "Download image cancelled $index")
launchNonCancellable {
mSpiderDen.remove(index)
}
}
}
}
}
}