commit f9611bde7611404911a1219a4ba05d8d0e3821d2
parent e56c5250bb50d43930c51e09f432b2e6405ba592
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Fri, 19 May 2023 01:24:34 +0200

fix(cdndownloader): coroutines

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt | 4+---
Mapp/src/main/kotlin/me/rhunk/snapenhance/util/download/CdnDownloader.kt | 11+++++++----
2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt b/app/src/main/kotlin/me/rhunk/snapenhance/features/impl/downloader/MediaDownloader.kt @@ -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) diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/util/download/CdnDownloader.kt b/app/src/main/kotlin/me/rhunk/snapenhance/util/download/CdnDownloader.kt @@ -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