fix(cdndownloader): coroutines

This commit is contained in:
rhunk 2023-05-19 01:24:34 +02:00
parent e56c5250bb
commit f9611bde76
2 changed files with 8 additions and 7 deletions

View File

@ -369,7 +369,7 @@ class MediaDownloader : Feature("MediaDownloader", loadParams = FeatureLoadParam
//download the message content
try {
var inputStream: InputStream = CdnDownloader.downloadWithDefaultEndpoints(urlKey) ?: return
var inputStream: InputStream = CdnDownloader.downloadWithDefaultEndpoints(urlKey) ?: throw FileNotFoundException("Unable to get $urlKey from cdn list. Check the logs for more info")
inputStream = EncryptionUtils.decryptInputStreamFromArroyo(
inputStream,
contentType,
@ -436,8 +436,6 @@ class MediaDownloader : Feature("MediaDownloader", loadParams = FeatureLoadParam
return
}
downloadMediaContent(mediaData, mediaData.contentHashCode(), messageAuthor, fileType)
} catch (e: FileNotFoundException) {
context.shortToast("Unable to get $urlKey from cdn list. Check the logs for more info")
} catch (e: Throwable) {
context.shortToast("Failed to download " + e.message)
xposedLog(e)

View File

@ -1,6 +1,8 @@
package me.rhunk.snapenhance.util.download
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import me.rhunk.snapenhance.Constants
@ -19,6 +21,7 @@ object CdnDownloader {
const val CF_ST_CDN_I = "https://cf-st.sc-cdn.net/i/"
const val CF_ST_CDN_J = "https://cf-st.sc-cdn.net/j/"
const val CF_ST_CDN_C = "https://cf-st.sc-cdn.net/c/"
const val CF_ST_CDN_A = "https://cf-st.sc-cdn.net/a/"
const val CF_ST_CDN_AA = "https://cf-st.sc-cdn.net/aa/"
private val keyCache: MutableMap<String, String> = mutableMapOf()
@ -36,17 +39,16 @@ object CdnDownloader {
var inputStream: InputStream? = null
endpoints.forEach {
launch {
launch(Dispatchers.IO) {
val url = it + key
val result = queryRemoteContent(url)
if (result != null) {
queryRemoteContent(url)?.let { result ->
keyCache[key] = url
inputStream = result
jobs.forEach { it.cancel() }
}
}.also { jobs.add(it) }
}
jobs.forEach { it.join() }
jobs.joinAll()
inputStream
}
@ -75,6 +77,7 @@ object CdnDownloader {
CF_ST_CDN_I,
CF_ST_CDN_C,
CF_ST_CDN_J,
CF_ST_CDN_A,
CF_ST_CDN_AA,
CF_ST_CDN_G,
CF_ST_CDN_D