Remove the try-catch wrapping in MangaDex. (#18716)

This commit is contained in:
Alessandro Jean 2023-10-25 16:05:10 -03:00 committed by GitHub
parent 5b79fd2229
commit ae81ee5ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 36 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'MangaDex' extName = 'MangaDex'
pkgNameSuffix = 'all.mangadex' pkgNameSuffix = 'all.mangadex'
extClass = '.MangaDexFactory' extClass = '.MangaDexFactory'
extVersionCode = 191 extVersionCode = 192
isNsfw = true isNsfw = true
} }

View File

@ -12,6 +12,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response import okhttp3.Response
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -37,21 +38,7 @@ class MdAtHomeReportInterceptor(
Log.e("MangaDex", "Connecting to MD@Home node at $url") Log.e("MangaDex", "Connecting to MD@Home node at $url")
val result = ImageReportDto( val reportRequest = mdAtHomeReportRequest(response)
url = url,
success = response.isSuccessful,
bytes = response.peekBody(Long.MAX_VALUE).bytes().size,
cached = response.headers["X-Cache"] == "HIT",
duration = response.receivedResponseAtMillis - response.sentRequestAtMillis,
)
val payload = json.encodeToString(result)
val reportRequest = POST(
url = MDConstants.atHomePostUrl,
headers = headers,
body = payload.toRequestBody(JSON_MEDIA_TYPE),
)
// Execute the report endpoint network call asynchronously to avoid blocking // Execute the report endpoint network call asynchronously to avoid blocking
// the reader from showing the image once it's fully loaded if the report call // the reader from showing the image once it's fully loaded if the report call
@ -66,8 +53,12 @@ class MdAtHomeReportInterceptor(
Log.e("MangaDex", "Error connecting to MD@Home node, fallback to uploads server") Log.e("MangaDex", "Error connecting to MD@Home node, fallback to uploads server")
val imagePath = originalRequest.url.pathSegments
.dropWhile { it != "data" && it != "data-saver" }
.joinToString("/")
val fallbackUrl = MDConstants.cdnUrl.toHttpUrl().newBuilder() val fallbackUrl = MDConstants.cdnUrl.toHttpUrl().newBuilder()
.addPathSegments(originalRequest.url.pathSegments.dropWhile{ it != "data" && it != "data-saver" }.joinToString("/")) .addPathSegments(imagePath)
.build() .build()
val fallbackRequest = originalRequest.newBuilder() val fallbackRequest = originalRequest.newBuilder()
@ -78,6 +69,24 @@ class MdAtHomeReportInterceptor(
return chain.proceed(fallbackRequest) return chain.proceed(fallbackRequest)
} }
private fun mdAtHomeReportRequest(response: Response): Request {
val result = ImageReportDto(
url = response.request.url.toString(),
success = response.isSuccessful,
bytes = response.peekBody(Long.MAX_VALUE).bytes().size,
cached = response.headers["X-Cache"] == "HIT",
duration = response.receivedResponseAtMillis - response.sentRequestAtMillis,
)
val payload = json.encodeToString(result)
return POST(
url = MDConstants.atHomePostUrl,
headers = headers,
body = payload.toRequestBody(JSON_MEDIA_TYPE),
)
}
companion object { companion object {
private val JSON_MEDIA_TYPE = "application/json".toMediaType() private val JSON_MEDIA_TYPE = "application/json".toMediaType()
private val MD_AT_HOME_URL_REGEX = private val MD_AT_HOME_URL_REGEX =

View File

@ -19,29 +19,22 @@ class MdUserAgentInterceptor(
MDConstants.defaultUserAgent, MDConstants.defaultUserAgent,
) )
private fun getUserAgent(): String? {
return preferences.customUserAgent
}
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
try { val originalRequest = chain.request()
val originalRequest = chain.request()
val newUserAgent = getUserAgent() ?: return chain.proceed(originalRequest) val newUserAgent = preferences.customUserAgent
?: return chain.proceed(originalRequest)
val originalHeaders = originalRequest.headers val originalHeaders = originalRequest.headers
val modifiedHeaders = originalHeaders.newBuilder() val modifiedHeaders = originalHeaders.newBuilder()
.set("User-Agent", newUserAgent) .set("User-Agent", newUserAgent)
.build() .build()
return chain.proceed( val modifiedRequest = originalRequest.newBuilder()
originalRequest.newBuilder() .headers(modifiedHeaders)
.headers(modifiedHeaders) .build()
.build(),
) return chain.proceed(modifiedRequest)
} catch (e: Exception) {
throw IOException("MdUserAgentInterceptor failed with error: ${e.message}")
}
} }
} }