From afe7437e0a0f6c315d383e0b6133b13a461c92af Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 7 Jan 2018 04:44:18 -0500 Subject: Enhancement/debug (#605) * Finalize debugger * Add video logging --- .../pitchedapps/frost/debugger/OfflineWebsite.kt | 25 ++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/debugger') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt b/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt index 434f1bae..791e6a69 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/debugger/OfflineWebsite.kt @@ -6,6 +6,8 @@ import com.pitchedapps.frost.facebook.USER_AGENT_BASIC import com.pitchedapps.frost.facebook.get import com.pitchedapps.frost.facebook.requests.call import com.pitchedapps.frost.facebook.requests.zip +import com.pitchedapps.frost.utils.createFreshDir +import com.pitchedapps.frost.utils.createFreshFile import com.pitchedapps.frost.utils.frostJsoup import okhttp3.Request import okhttp3.ResponseBody @@ -71,24 +73,19 @@ class OfflineWebsite(private val url: String, reset() L.v { "Saving $url to ${baseDir.absolutePath}" } - if (baseDir.exists() && !baseDir.deleteRecursively()) { - L.e { "Could not clean directory" } - return callback(false) - } - if (!baseDir.mkdirs()) { + if (!baseDir.exists() && !baseDir.mkdirs()) { L.e { "Could not make directory" } return callback(false) } - if (!mainFile.createNewFile()) { L.e { "Could not create ${mainFile.absolutePath}" } return callback(false) } - if (!assetDir.mkdirs()) { + if (!assetDir.createFreshDir()) { L.e { "Could not create ${assetDir.absolutePath}" } return callback(false) } @@ -149,7 +146,7 @@ class OfflineWebsite(private val url: String, fun zip(name: String): Boolean { try { val zip = File(baseDir, "$name.zip") - if (zip.exists() && (!zip.delete() || !zip.createNewFile())) { + if (!zip.createFreshFile()) { L.e { "Failed to create zip at ${zip.absolutePath}" } return false } @@ -157,6 +154,7 @@ class OfflineWebsite(private val url: String, ZipOutputStream(FileOutputStream(zip)).use { out -> fun File.zip(name: String = this.name) { + if (!isFile) return inputStream().use { file -> out.putNextEntry(ZipEntry(name)) file.copyTo(out) @@ -165,13 +163,14 @@ class OfflineWebsite(private val url: String, delete() } - mainFile.zip() + baseDir.listFiles({ _, n -> n != "$name.zip" }).forEach { it.zip() } assetDir.listFiles().forEach { it.zip("assets/${it.name}") } } return true } catch (e: Exception) { + L.e { "Zip failed: ${e.message}" } return false } } @@ -275,11 +274,16 @@ class OfflineWebsite(private val url: String, val index = atomicInt.getAndIncrement() + + var newUrl = "a${index}_$candidate" + /** * This is primarily for zipping up and sending via emails * As .js files typically aren't allowed, we'll simply make everything txt files */ - val newUrl = "a${index}_$candidate.txt" + if (newUrl.endsWith(".js")) + newUrl = "$newUrl.txt" + urlMapper.put(this, newUrl) return newUrl } @@ -296,7 +300,6 @@ class OfflineWebsite(private val url: String, atomicInt.set(0) fileQueue.clear() cssQueue.clear() - baseDir.deleteRecursively() } fun cancel() { -- cgit v1.2.3