Fallback to another favicon on error

This commit is contained in:
Koitharu
2023-08-11 12:30:47 +03:00
parent f4f84099cc
commit 788c7b862a

View File

@@ -20,7 +20,9 @@ import okhttp3.Response
import okhttp3.ResponseBody import okhttp3.ResponseBody
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import okio.Closeable import okio.Closeable
import okio.IOException
import okio.buffer import okio.buffer
import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException
import org.koitharu.kotatsu.core.model.MangaSource import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
@@ -49,22 +51,32 @@ class FaviconFetcher(
override suspend fun fetch(): FetchResult { override suspend fun fetch(): FetchResult {
getCached(options)?.let { return it } getCached(options)?.let { return it }
val repo = mangaRepositoryFactory.create(mangaSource) as RemoteMangaRepository val repo = mangaRepositoryFactory.create(mangaSource) as RemoteMangaRepository
val favicons = repo.getFavicons()
val sizePx = maxOf( val sizePx = maxOf(
options.size.width.pxOrElse { FALLBACK_SIZE }, options.size.width.pxOrElse { FALLBACK_SIZE },
options.size.height.pxOrElse { FALLBACK_SIZE }, options.size.height.pxOrElse { FALLBACK_SIZE },
) )
val icon = checkNotNull(favicons.find(sizePx)) { "No favicons found" } var favicons = repo.getFavicons()
val response = loadIcon(icon.url, mangaSource) while (favicons.isNotEmpty()) {
val responseBody = response.requireBody() val icon = favicons.find(sizePx) ?: throwNSEE()
val source = writeToDiskCache(responseBody)?.toImageSource()?.also { val response = try {
response.closeQuietly() loadIcon(icon.url, mangaSource)
} ?: responseBody.toImageSource(response) } catch (e: CloudFlareProtectedException) {
return SourceResult( throw e
source = source, } catch (e: IOException) {
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(icon.type), favicons -= icon
dataSource = response.toDataSource(), continue
) }
val responseBody = response.requireBody()
val source = writeToDiskCache(responseBody)?.toImageSource()?.also {
response.closeQuietly()
} ?: responseBody.toImageSource(response)
return SourceResult(
source = source,
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(icon.type),
dataSource = response.toDataSource(),
)
}
throwNSEE()
} }
private suspend fun loadIcon(url: String, source: MangaSource): Response { private suspend fun loadIcon(url: String, source: MangaSource): Response {
@@ -143,6 +155,8 @@ class FaviconFetcher(
append(height.toString()) append(height.toString())
} }
private fun throwNSEE(): Nothing = throw NoSuchElementException("No favicons found")
class Factory( class Factory(
context: Context, context: Context,
private val okHttpClient: OkHttpClient, private val okHttpClient: OkHttpClient,