Happymh: Fix Content-Type of images (#5090)

This commit is contained in:
anenasa 2024-09-17 13:32:08 +08:00 committed by GitHub
parent 111057f898
commit cd871beab7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'Happymh'
extClass = '.Happymh'
extVersionCode = 11
extVersionCode = 12
}
apply from: "$rootDir/common.gradle"

View File

@ -22,9 +22,12 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.asResponseBody
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
@ -36,7 +39,6 @@ class Happymh : HttpSource(), ConfigurableSource {
override val lang: String = "zh"
override val supportsLatest: Boolean = true
override val baseUrl: String = "https://m.happymh.com"
override val client: OkHttpClient = network.cloudflareClient
private val json: Json by injectLazy()
private val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
@ -50,6 +52,23 @@ class Happymh : HttpSource(), ConfigurableSource {
}
}
private val rewriteOctetStream: Interceptor = Interceptor { chain ->
val originalResponse: Response = chain.proceed(chain.request())
if (originalResponse.headers("Content-Type").contains("application/octet-stream") && originalResponse.request.url.toString().contains(".jpg")) {
val orgBody = originalResponse.body.source()
val newBody = orgBody.asResponseBody("image/jpeg".toMediaType())
originalResponse.newBuilder()
.body(newBody)
.build()
} else {
originalResponse
}
}
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.addInterceptor(rewriteOctetStream)
.build()
override fun headersBuilder(): Headers.Builder {
val builder = super.headersBuilder()
val userAgent = preferences.getString(PREF_KEY_CUSTOM_UA, "")!!