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("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0")
|
||||||
implementation("com.googlecode.json-simple:json-simple:1.1")
|
implementation("com.googlecode.json-simple:json-simple:1.1")
|
||||||
|
implementation("com.squareup.okhttp3:okhttp:4.9.0")
|
||||||
|
|
||||||
implementation(gradleApi())
|
implementation(gradleApi())
|
||||||
implementation(localGroovy())
|
implementation(localGroovy())
|
||||||
|
@ -79,11 +79,6 @@ object Libs {
|
|||||||
const val mockwebserver: String = "com.squareup.okhttp3:mockwebserver:" +
|
const val mockwebserver: String = "com.squareup.okhttp3:mockwebserver:" +
|
||||||
Versions.com_squareup_okhttp3
|
Versions.com_squareup_okhttp3
|
||||||
|
|
||||||
/**
|
|
||||||
* https://square.github.io/okhttp/
|
|
||||||
*/
|
|
||||||
const val okhttp: String = "com.squareup.okhttp3:okhttp:" + Versions.com_squareup_okhttp3
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://kotlinlang.org/
|
* https://kotlinlang.org/
|
||||||
*/
|
*/
|
||||||
|
@ -185,7 +185,6 @@ class AllProjectConfigurer {
|
|||||||
implementation(Libs.navigation_fragment_ktx)
|
implementation(Libs.navigation_fragment_ktx)
|
||||||
implementation(Libs.navigation_ui_ktx)
|
implementation(Libs.navigation_ui_ktx)
|
||||||
androidTestImplementation(Libs.navigation_testing)
|
androidTestImplementation(Libs.navigation_testing)
|
||||||
implementation(Libs.okhttp)
|
|
||||||
implementation(Libs.logging_interceptor)
|
implementation(Libs.logging_interceptor)
|
||||||
implementation(Libs.retrofit)
|
implementation(Libs.retrofit)
|
||||||
implementation(Libs.adapter_rxjava2)
|
implementation(Libs.adapter_rxjava2)
|
||||||
|
@ -6,10 +6,13 @@ import custom.createPublisher
|
|||||||
import custom.transactionWithCommit
|
import custom.transactionWithCommit
|
||||||
import plugin.KiwixConfigurationPlugin
|
import plugin.KiwixConfigurationPlugin
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.URL
|
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.util.Base64
|
import java.util.Base64
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.Request
|
||||||
|
import okhttp3.ResponseBody
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
android
|
android
|
||||||
@ -51,37 +54,53 @@ fun ProductFlavor.createDownloadTask(file: File): Task {
|
|||||||
doLast {
|
doLast {
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
file.createNewFile()
|
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("\"", "")
|
val urlString = buildConfigFields["ZIM_URL"]!!.value.replace("\"", "")
|
||||||
var secretKey = ""
|
return if (urlString.isAuthenticationUrl) {
|
||||||
val url = if (urlString.isAuthenticationUrl) {
|
Request.Builder()
|
||||||
secretKey = urlString.secretKey
|
.url(URI.create(urlString.removeAuthenticationFromUrl).toURL())
|
||||||
URI.create(urlString.removeAuthenticationFromUrl).toURL()
|
.header(
|
||||||
|
"Authorization",
|
||||||
|
"Basic " +
|
||||||
|
Base64.getEncoder().encodeToString(System.getenv(urlString.secretKey).toByteArray())
|
||||||
|
)
|
||||||
|
.build()
|
||||||
} else {
|
} else {
|
||||||
URI.create(urlString).toURL()
|
Request.Builder()
|
||||||
|
.url(URI.create(urlString).toURL())
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
return url
|
}
|
||||||
.openConnection()
|
|
||||||
.apply {
|
fun writeZimFileData(responseBody: ResponseBody, file: File) {
|
||||||
if (urlString.isAuthenticationUrl) {
|
FileOutputStream(file).use { outputStream ->
|
||||||
setRequestProperty(
|
responseBody.byteStream().use { inputStream ->
|
||||||
"Authorization",
|
val buffer = ByteArray(4096)
|
||||||
"Basic ${Base64.getEncoder().encodeToString(System.getenv(secretKey).toByteArray())}"
|
var bytesRead: Int
|
||||||
)
|
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead)
|
||||||
}
|
}
|
||||||
connect()
|
outputStream.flush()
|
||||||
getInputStream()
|
|
||||||
}.let {
|
|
||||||
it.getHeaderField("Location")?.replace("https", "http") ?: it.url.toString()
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val String.decodeUrl: String
|
val String.decodeUrl: String
|
||||||
|
Loading…
x
Reference in New Issue
Block a user