Introduce CronetRequest.copyToChannel
This commit is contained in:
1 parent
ead2300d90
commit
0265ddc6e1
3 files changed
+14
-12
No files matched your search
@@ -12,7 +12,7 @@ import coil.fetch.FetchResult
|
||||
import coil.fetch.Fetcher
|
||||
import coil.fetch.SourceResult
|
||||
import coil.request.Options
|
||||
import com.hippo.ehviewer.cronet.awaitBodyFully
|
||||
import com.hippo.ehviewer.cronet.copyToChannel
|
||||
import com.hippo.ehviewer.cronet.cronetRequest
|
||||
import com.hippo.ehviewer.cronet.execute
|
||||
import java.io.RandomAccessFile
|
||||
@@ -30,11 +30,8 @@ class CronetHttpUriFetcher(private val data: String, private val options: Option
|
||||
disableCache()
|
||||
}.execute {
|
||||
val success = diskCache.suspendEdit(diskCacheKey) {
|
||||
RandomAccessFile(data.toFile(), "rw").use { f ->
|
||||
val chan = f.channel
|
||||
awaitBodyFully { buffer ->
|
||||
chan.write(buffer)
|
||||
}
|
||||
RandomAccessFile(data.toFile(), "rw").use {
|
||||
copyToChannel(it.channel)
|
||||
}
|
||||
}
|
||||
check(success)
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.chromium.net.UrlRequest
|
||||
import org.chromium.net.UrlResponseInfo
|
||||
import splitties.init.appCtx
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
@@ -98,6 +99,11 @@ suspend inline fun CronetRequest.awaitBodyFully(crossinline callback: (ByteBuffe
|
||||
}
|
||||
}
|
||||
|
||||
suspend inline fun CronetRequest.copyToChannel(chan: FileChannel, crossinline listener: ((Int) -> Unit) = {}) = awaitBodyFully {
|
||||
val bytes = chan.write(it)
|
||||
listener(bytes)
|
||||
}
|
||||
|
||||
suspend inline fun CronetRequest.execute(crossinline callback: suspend CronetRequest.(UrlResponseInfo) -> Unit) = coroutineScope {
|
||||
onResponse = { launch { callback(it) } }
|
||||
suspendCancellableCoroutine { cont ->
|
||||
|
||||
@@ -26,7 +26,7 @@ import com.hippo.ehviewer.client.executeNonCache
|
||||
import com.hippo.ehviewer.client.getImageKey
|
||||
import com.hippo.ehviewer.coil.read
|
||||
import com.hippo.ehviewer.coil.suspendEdit
|
||||
import com.hippo.ehviewer.cronet.awaitBodyFully
|
||||
import com.hippo.ehviewer.cronet.copyToChannel
|
||||
import com.hippo.ehviewer.cronet.cronetRequest
|
||||
import com.hippo.ehviewer.cronet.execute
|
||||
import com.hippo.ehviewer.download.downloadLocation
|
||||
@@ -141,11 +141,10 @@ class SpiderDen(mGalleryInfo: GalleryInfo) {
|
||||
val type = headers["Content-Type"]?.first()?.toMediaType()?.subtype ?: "jpg"
|
||||
val length = headers["Content-Length"]!!.first().toLong()
|
||||
saveResponseMeta(index, type, length) { file ->
|
||||
file.openOutputStream().use {
|
||||
val chan = it.channel
|
||||
awaitBodyFully { buffer ->
|
||||
val read = chan.write(buffer)
|
||||
notifyProgress(length, info.receivedByteCount, read)
|
||||
file.openOutputStream().use { os ->
|
||||
val chan = os.channel
|
||||
copyToChannel(chan) {
|
||||
notifyProgress(length, info.receivedByteCount, it)
|
||||
}.also { size -> if (chan.size() != size) chan.truncate(size) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user