From 6b1b4c776d2a805e03611cc6e686ff9c603edd57 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 8 Sep 2019 01:40:14 -0700 Subject: Add extension checker --- .../com/pitchedapps/frost/activities/ImageActivity.kt | 18 ++++++++++++++++-- app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index 2e92c814..47b72354 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -112,6 +112,8 @@ class ImageActivity : KauBaseActivity() { private lateinit var dragHelper: ViewDragHelper + private var imgExtension: String = ".jpg" + companion object { /** * Cache folder to store images @@ -120,7 +122,6 @@ class ImageActivity : KauBaseActivity() { private const val IMAGE_FOLDER = "images" private const val TIME_FORMAT = "yyyyMMdd_HHmmss" private const val IMG_TAG = "Frost" - private const val IMG_EXTENSION = ".png" const val PURGE_TIME: Long = 10 * 60 * 1000 // 10 min block private val L = KauLoggerExtension("Image", com.pitchedapps.frost.utils.L) @@ -295,6 +296,17 @@ class ImageActivity : KauBaseActivity() { override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int = top } + private fun getImageExtension(type: String?): String? { + if (type?.startsWith("image/") != true) { + return null + } + return when (type.substring(6)) { + "jpeg", "jpg" -> ".jpg" + "png" -> ".png" + else -> null + } + } + @Throws(IOException::class) private fun createPublicMediaFile(): File { val timeStamp = SimpleDateFormat(TIME_FORMAT, Locale.getDefault()).format(Date()) @@ -303,7 +315,7 @@ class ImageActivity : KauBaseActivity() { Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) val frostDir = File(storageDir, IMG_TAG) if (!frostDir.exists()) frostDir.mkdirs() - return File.createTempFile(imageFileName, IMG_EXTENSION, frostDir) + return File.createTempFile(imageFileName, imgExtension, frostDir) } /** @@ -349,6 +361,8 @@ class ImageActivity : KauBaseActivity() { .call() .execute() + imgExtension = getImageExtension(response.header("Content-Type")) ?: ".jpg" + if (!response.isSuccessful) { throw IOException("Unsuccessful response for image: ${response.peekBody(128).string()}") } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt index dd8cf594..e2fac291 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt @@ -18,6 +18,7 @@ package com.pitchedapps.frost.utils import android.util.Log import ca.allanwang.kau.logging.KauLogger +import ca.allanwang.kau.logging.KauLoggerExtension import com.bugsnag.android.Bugsnag import com.pitchedapps.frost.BuildConfig @@ -78,3 +79,9 @@ object L : KauLogger("Frost", { } } } + +fun KauLoggerExtension.test(message: () -> Any?) { + if (BuildConfig.DEBUG) { + d { "Test1234 ${message()}" } + } +} \ No newline at end of file -- cgit v1.2.3 From 926d0a6e9219852c48f15b526493267a855517e2 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 8 Sep 2019 01:45:02 -0700 Subject: Update extensions --- app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index 47b72354..15efc527 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -301,8 +301,9 @@ class ImageActivity : KauBaseActivity() { return null } return when (type.substring(6)) { - "jpeg", "jpg" -> ".jpg" + "jpeg" -> ".jpg" "png" -> ".png" + "gif" -> ".gif" else -> null } } -- cgit v1.2.3 From ed9dfdd76d90d1f9a4fd52d5f90ad790f671a776 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 8 Sep 2019 01:46:30 -0700 Subject: Check extension after successful response --- app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt index 15efc527..09620a54 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt @@ -362,12 +362,12 @@ class ImageActivity : KauBaseActivity() { .call() .execute() - imgExtension = getImageExtension(response.header("Content-Type")) ?: ".jpg" - if (!response.isSuccessful) { throw IOException("Unsuccessful response for image: ${response.peekBody(128).string()}") } + imgExtension = getImageExtension(response.header("Content-Type")) ?: ".jpg" + val body = response.body() ?: throw IOException("Failed to retrieve image body") file.copyFromInputStream(body.byteStream()) -- cgit v1.2.3