aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-09-08 01:40:14 -0700
committerAllan Wang <me@allanwang.ca>2019-09-08 01:40:14 -0700
commit6b1b4c776d2a805e03611cc6e686ff9c603edd57 (patch)
treef971b47eed8584dea26fdc601b98e2d56998a2ec /app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt
parent81c1a249b847121f9552b4b309dbe8c730de1c14 (diff)
downloadfrost-6b1b4c776d2a805e03611cc6e686ff9c603edd57.tar.gz
frost-6b1b4c776d2a805e03611cc6e686ff9c603edd57.tar.bz2
frost-6b1b4c776d2a805e03611cc6e686ff9c603edd57.zip
Add extension checker
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/ImageActivity.kt18
1 files changed, 16 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()}")
}