Skip to content

Commit

Permalink
Fix Cannot Download EPG URL which is not end with .gz but redirected …
Browse files Browse the repository at this point in the history
…to gz- resource. #174
  • Loading branch information
oxyroid committed Jul 20, 2024
1 parent 4daa580 commit 35a8f50
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/com/m3u/core/util/basic/LetIf.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.m3u.core.util.basic

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

@OptIn(ExperimentalContracts::class)
inline fun <T> T.letIf(condition: Boolean, block: (T) -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return if (condition) block(this) else this
}
3 changes: 1 addition & 2 deletions data/src/main/java/com/m3u/data/api/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ internal object ApiModule {
@Singleton
@OkhttpClient(false)
fun provideOkhttpClient(
logger: Logger,
@Logger.MessageImpl messager: Logger
logger: Logger
): OkHttpClient {
return OkHttpClient.Builder()
.authenticator(Authenticator.JAVA_NET_AUTHENTICATOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.m3u.core.architecture.logger.Logger
import com.m3u.core.architecture.logger.Profiles
import com.m3u.core.architecture.logger.install
import com.m3u.core.architecture.logger.post
import com.m3u.core.util.basic.letIf
import com.m3u.data.api.OkhttpClient
import com.m3u.data.database.dao.PlaylistDao
import com.m3u.data.database.dao.ProgrammeDao
Expand All @@ -29,7 +30,6 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.supervisorScope
import kotlinx.datetime.Clock
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import java.util.zip.GZIPInputStream
Expand Down Expand Up @@ -126,26 +126,23 @@ internal class ProgrammeRepositoryImpl @Inject constructor(
}

private fun downloadProgrammes(epgUrl: String): Flow<EpgProgramme> = channelFlow {
val isGzip = epgUrl
.toHttpUrlOrNull()
?.pathSegments
?.lastOrNull()
val request = Request.Builder()
.url(epgUrl)
.build()

val response = okHttpClient.newCall(request).execute()
val url = response.request.url

val isGzip = url
.pathSegments
.lastOrNull()
?.endsWith(".gz", true)
?: false

okHttpClient.newCall(
Request.Builder()
.url(epgUrl)
.build()
)
.execute()
response
.body
?.byteStream()
?.let {
if (isGzip) {
GZIPInputStream(it).buffered()
} else it
}
?.letIf(isGzip) { GZIPInputStream(it).buffered() }
?.use { input ->
epgParser
.readProgrammes(input)
Expand Down

0 comments on commit 35a8f50

Please sign in to comment.