Commit 2fc8dac7 authored by Anon's avatar Anon
Browse files

Unpack download archive.

parent d1eb0d0e
......@@ -22,16 +22,25 @@
package de.ccc.events.badge.card10.hatchery
import android.content.DialogInterface
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import de.ccc.events.badge.card10.R
import de.ccc.events.badge.card10.common.LoadingDialog
import kotlinx.android.synthetic.main.app_detail_fragment.*
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
import java.io.File
private const val TAG = "AppDetailFragment"
class AppDetailFragment : Fragment() {
private lateinit var app: App
......@@ -54,28 +63,71 @@ class AppDetailFragment : Fragment() {
label_description.text = app.description
button_download.setOnClickListener {
ReleaseDownload(app).execute()
val ctx = activity ?: throw java.lang.IllegalStateException()
val loadingDialog = LoadingDialog()
loadingDialog.show(fragmentManager, "loading")
val errorDialog =
AlertDialog.Builder(ctx).setMessage(R.string.hatchery_error_generic)
.setPositiveButton(
R.string.dialog_action_ok
) { dialog, _ -> dialog.dismiss() }
.create()
ReleaseDownload(app, ctx.cacheDir, loadingDialog, errorDialog).execute()
}
}
private class ReleaseDownload(private val app: App) : AsyncTask<Void, Void, String?>() {
private class ReleaseDownload(
private val app: App,
private val cacheDir: File,
private val loadingDialog: LoadingDialog,
private val errorDialog: AlertDialog
) : AsyncTask<Void, Void, List<String>?>() {
override fun doInBackground(vararg p0: Void?): String? {
override fun doInBackground(vararg p0: Void?): List<String>? {
return try {
cacheDir.deleteRecursively()
cacheDir.mkdir()
val inputStream = HatcheryClient().openDownloadStream(app)
val file = File.createTempFile(app.slug, ".tar.gz")
val file = File.createTempFile(app.slug, ".tar.gz", cacheDir)
val outputStream = file.outputStream()
inputStream.copyTo(outputStream)
file.absolutePath
val unpackedFiles = mutableListOf<String>()
val tarStream = TarArchiveInputStream(GzipCompressorInputStream(file.inputStream()))
while (true) {
val entry = tarStream.nextTarEntry ?: break
if (entry.isDirectory) {
continue
}
// TODO: A bit hacky. Maybe there is a better way?
val targetFile = File(cacheDir, entry.name)
targetFile.parentFile?.mkdirs()
targetFile.createNewFile()
Log.d(TAG, "Extracting ${entry.name} to ${targetFile.absolutePath}")
tarStream.copyTo(targetFile.outputStream())
unpackedFiles.add(entry.name)
}
unpackedFiles
} catch (e: Exception) {
null
}
}
override fun onPostExecute(result: String?) {
// TODO: Handle result
override fun onPostExecute(result: List<String>?) {
if (result == null) {
loadingDialog.dismiss()
errorDialog.show()
return
}
loadingDialog.dismiss()
}
}
}
......@@ -16,6 +16,8 @@
<string name="file_transfer_label_selected_file">Selected file:</string>
<string name="loading_dialog_loading">Loading</string>
<string name="dialog_action_ok">OK</string>
<string name="dialog_action_cancel">Cancel</string>
<string name="hatchery_error_generic">Something went wrong</string>
......
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