mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 23:07:26 -04:00
Fixed URLConnection not adding headers to the request.
* In this update, we've replaced the `URLConnection` with the OkHttp library, and now headers are being correctly passed in the request. As a result, the Zim file is successfully downloading. * To integrate OkHttp into our Gradle project, we've moved the OkHttp dependency to our `buildSrc` folder.
This commit is contained in:
parent
5e810391f0
commit
11c5b3caa2
@ -23,6 +23,7 @@ dependencies {
|
||||
}
|
||||
implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0")
|
||||
implementation("com.googlecode.json-simple:json-simple:1.1")
|
||||
implementation("com.squareup.okhttp3:okhttp:4.9.0")
|
||||
|
||||
implementation(gradleApi())
|
||||
implementation(localGroovy())
|
||||
|
@ -79,11 +79,6 @@ object Libs {
|
||||
const val mockwebserver: String = "com.squareup.okhttp3:mockwebserver:" +
|
||||
Versions.com_squareup_okhttp3
|
||||
|
||||
/**
|
||||
* https://square.github.io/okhttp/
|
||||
*/
|
||||
const val okhttp: String = "com.squareup.okhttp3:okhttp:" + Versions.com_squareup_okhttp3
|
||||
|
||||
/**
|
||||
* https://kotlinlang.org/
|
||||
*/
|
||||
|
@ -185,7 +185,6 @@ class AllProjectConfigurer {
|
||||
implementation(Libs.navigation_fragment_ktx)
|
||||
implementation(Libs.navigation_ui_ktx)
|
||||
androidTestImplementation(Libs.navigation_testing)
|
||||
implementation(Libs.okhttp)
|
||||
implementation(Libs.logging_interceptor)
|
||||
implementation(Libs.retrofit)
|
||||
implementation(Libs.adapter_rxjava2)
|
||||
|
@ -6,10 +6,13 @@ import custom.createPublisher
|
||||
import custom.transactionWithCommit
|
||||
import plugin.KiwixConfigurationPlugin
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
import java.net.URLDecoder
|
||||
import java.util.Locale
|
||||
import java.util.Base64
|
||||
import java.io.FileOutputStream
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.ResponseBody
|
||||
|
||||
plugins {
|
||||
android
|
||||
@ -51,37 +54,53 @@ fun ProductFlavor.createDownloadTask(file: File): Task {
|
||||
doLast {
|
||||
if (!file.exists()) {
|
||||
file.createNewFile()
|
||||
URL(fetchUrl()).openStream().use {
|
||||
it.copyTo(file.outputStream())
|
||||
|
||||
OkHttpClient().newCall(fetchRequest()).execute().use { response ->
|
||||
if (response.isSuccessful) {
|
||||
response.body?.let { responseBody ->
|
||||
writeZimFileData(responseBody, file)
|
||||
}
|
||||
} else {
|
||||
throw RuntimeException(
|
||||
"Download Failed. Error: ${response.message}\n" +
|
||||
" Status Code: ${response.code}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ProductFlavor.fetchUrl(): String {
|
||||
fun ProductFlavor.fetchRequest(): Request {
|
||||
val urlString = buildConfigFields["ZIM_URL"]!!.value.replace("\"", "")
|
||||
var secretKey = ""
|
||||
val url = if (urlString.isAuthenticationUrl) {
|
||||
secretKey = urlString.secretKey
|
||||
URI.create(urlString.removeAuthenticationFromUrl).toURL()
|
||||
return if (urlString.isAuthenticationUrl) {
|
||||
Request.Builder()
|
||||
.url(URI.create(urlString.removeAuthenticationFromUrl).toURL())
|
||||
.header(
|
||||
"Authorization",
|
||||
"Basic " +
|
||||
Base64.getEncoder().encodeToString(System.getenv(urlString.secretKey).toByteArray())
|
||||
)
|
||||
.build()
|
||||
} else {
|
||||
URI.create(urlString).toURL()
|
||||
Request.Builder()
|
||||
.url(URI.create(urlString).toURL())
|
||||
.build()
|
||||
}
|
||||
return url
|
||||
.openConnection()
|
||||
.apply {
|
||||
if (urlString.isAuthenticationUrl) {
|
||||
setRequestProperty(
|
||||
"Authorization",
|
||||
"Basic ${Base64.getEncoder().encodeToString(System.getenv(secretKey).toByteArray())}"
|
||||
)
|
||||
}
|
||||
|
||||
fun writeZimFileData(responseBody: ResponseBody, file: File) {
|
||||
FileOutputStream(file).use { outputStream ->
|
||||
responseBody.byteStream().use { inputStream ->
|
||||
val buffer = ByteArray(4096)
|
||||
var bytesRead: Int
|
||||
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead)
|
||||
}
|
||||
connect()
|
||||
getInputStream()
|
||||
}.let {
|
||||
it.getHeaderField("Location")?.replace("https", "http") ?: it.url.toString()
|
||||
outputStream.flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val String.decodeUrl: String
|
||||
|
Loading…
x
Reference in New Issue
Block a user