Commit eec5e7ba authored by Anon's avatar Anon
Browse files

Download app archive to temporary file.

parent 12d8ad1b
......@@ -22,6 +22,7 @@
package de.ccc.events.badge.card10.hatchery
import android.os.AsyncTask
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
......@@ -29,7 +30,7 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.R
import kotlinx.android.synthetic.main.app_detail_fragment.*
import java.lang.IllegalStateException
import java.io.File
class AppDetailFragment : Fragment() {
......@@ -51,5 +52,30 @@ class AppDetailFragment : Fragment() {
label_content_size.text = getString(R.string.app_detail_content_size, app.size_of_content)
label_description.text = app.description
button_download.setOnClickListener {
ReleaseDownload(app).execute()
}
}
private class ReleaseDownload(private val app: App) : AsyncTask<Void, Void, String?>() {
override fun doInBackground(vararg p0: Void?): String? {
return try {
val inputStream = HatcheryClient().openDownloadStream(app)
val file = File.createTempFile(app.slug, ".tar.gz")
val outputStream = file.outputStream()
inputStream.copyTo(outputStream)
file.absolutePath
} catch (e: Exception) {
null
}
}
override fun onPostExecute(result: String?) {
// TODO: Handle result
}
}
}
......@@ -29,6 +29,8 @@ import okhttp3.Request
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.io.InputStream
private const val TAG = "HatcheryClient"
......@@ -81,4 +83,57 @@ class HatcheryClient {
return resultList
}
fun getReleaseUrl(app: App): String {
val client = OkHttpClient()
val request = Request.Builder()
.url("$HATCHERY_BASE_URL/eggs/get/${app.slug}/json")
.build()
val response: Response
try {
response = client.newCall(request).execute()
} catch (e: Exception) {
throw HatcheryClientException(0)
}
if (response.code != 200) {
throw HatcheryClientException(response.code)
}
val body = response.body?.string() ?: ""
try {
val responseJson = JSONObject(body)
val version = responseJson.getJSONObject("info").getString("version")
return responseJson.getJSONObject("releases")
.getJSONArray(version).getJSONObject(0).getString("url")
} catch (e: JSONException) {
Log.e(TAG, "Error parsing JSON: ${e.message}")
throw HatcheryClientException(0)
}
}
fun openDownloadStream(app: App): InputStream {
val releaseUrl = getReleaseUrl(app)
val client = OkHttpClient()
val request = Request.Builder()
.url(releaseUrl)
.build()
val response: Response
try {
response = client.newCall(request).execute()
} catch (e: Exception) {
throw HatcheryClientException(0)
}
if (response.code != 200) {
throw HatcheryClientException(response.code)
}
return response.body?.byteStream() ?: throw HatcheryClientException(0)
}
}
......@@ -35,8 +35,7 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/button_download"
android:text="@string/app_detail_button_download"
android:enabled="false"/>
android:text="@string/app_detail_button_download"/>
</androidx.constraintlayout.widget.ConstraintLayout>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment