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

This commit is contained in:
anenasa 2024-09-17 13:32:08 +08:00 committed by Draff
parent 3ab425a549
commit a0715ec24a
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
2 changed files with 21 additions and 2 deletions

View File

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

View File

@ -22,9 +22,12 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.decodeFromStream
import okhttp3.FormBody import okhttp3.FormBody
import okhttp3.Headers import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody.Companion.asResponseBody
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -36,7 +39,6 @@ class Happymh : HttpSource(), ConfigurableSource {
override val lang: String = "zh" override val lang: String = "zh"
override val supportsLatest: Boolean = true override val supportsLatest: Boolean = true
override val baseUrl: String = "https://m.happymh.com" override val baseUrl: String = "https://m.happymh.com"
override val client: OkHttpClient = network.cloudflareClient
private val json: Json by injectLazy() private val json: Json by injectLazy()
private val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000) 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 { override fun headersBuilder(): Headers.Builder {
val builder = super.headersBuilder() val builder = super.headersBuilder()
val userAgent = preferences.getString(PREF_KEY_CUSTOM_UA, "")!! val userAgent = preferences.getString(PREF_KEY_CUSTOM_UA, "")!!