Move tag database downloading to FileChannel based operation

This commit is contained in:
tarsin committed 2023-02-18 12:18:15 +08:00
1 parent ad071e48df
commit 145d5e6302
2 files changed
+28 -12

No files matched your search

@@ -20,10 +20,9 @@ import com.hippo.ehviewer.AppConfig
import com.hippo.ehviewer.EhApplication
import com.hippo.ehviewer.EhApplication.Companion.okHttpClient
import com.hippo.ehviewer.R
import com.hippo.util.ExceptionUtils
import com.hippo.util.HashCodeUtils
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.IOUtils
import com.hippo.yorozuya.copyToFile
import eu.kanade.tachiyomi.util.lang.launchIO
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.flow.Flow
@@ -37,7 +36,6 @@ import org.json.JSONException
import org.json.JSONObject
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
@@ -197,22 +195,21 @@ object EhTagDatabase {
private fun save(client: OkHttpClient, url: String, file: File): Boolean {
val request: Request = Request.Builder().url(url).build()
val call = client.newCall(request)
try {
runCatching {
call.execute().use { response ->
if (!response.isSuccessful) {
return false
}
val body = response.body
body.byteStream()
.use { ins -> FileOutputStream(file).use { os -> IOUtils.copy(ins, os) } }
response.body.use {
it.copyToFile(file)
}
return true
}
} catch (t: Throwable) {
FileUtils.delete(file)
t.printStackTrace()
ExceptionUtils.throwIfFatal(t)
return false
}.onFailure {
file.delete()
it.printStackTrace()
}
return false
}
@OptIn(DelicateCoroutinesApi::class)
@@ -0,0 +1,19 @@
package com.hippo.yorozuya
import okhttp3.ResponseBody
import java.io.File
fun ResponseBody.copyToFile(file: File) {
val os = file.outputStream()
val channel = os.channel
val source = source()
try {
channel.transferFrom(source, 0, contentLength())
} catch (e: Throwable) {
throw e
} finally {
source.close()
channel.close()
os.close()
}
}