From 6294c65ad65c3591b8dd62b6e50f1eb588004df5 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Wed, 2 Oct 2024 18:41:46 +0200 Subject: [PATCH] Create private functions for image vs video compression and when statement to select on content type --- .../amethyst/ui/components/MediaCompressor.kt | 212 ++++++++++-------- 1 file changed, 118 insertions(+), 94 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/MediaCompressor.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/MediaCompressor.kt index 3e0148945..d50abd954 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/MediaCompressor.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/MediaCompressor.kt @@ -58,102 +58,126 @@ class MediaCompressor { checkNotInMainThread() - if (contentType?.startsWith("video", true) == true) { - val videoQuality = - when (mediaQuality) { - CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW - CompressorQuality.LOW -> VideoQuality.LOW - CompressorQuality.MEDIUM -> VideoQuality.MEDIUM - CompressorQuality.HIGH -> VideoQuality.HIGH - CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH - else -> VideoQuality.MEDIUM // dodgy - } - Log.d("MediaCompressor", "Using video compression $mediaQuality") - VideoCompressor.start( - // => This is required - context = applicationContext, - // => Source can be provided as content uris - uris = listOf(uri), - isStreamable = false, - // THIS STORAGE - // sharedStorageConfiguration = SharedStorageConfiguration( - // saveAt = SaveLocation.movies, // => default is movies - // videoName = "compressed_video" // => required name - // ), - // OR AND NOT BOTH - appSpecificStorageConfiguration = AppSpecificStorageConfiguration(), - configureWith = - Configuration( - quality = videoQuality, - // => required name - videoNames = listOf(UUID.randomUUID().toString()), - ), - listener = - object : CompressionListener { - override fun onProgress( - index: Int, - percent: Float, - ) { - } + // branch into compression based on content type + when { + contentType?.startsWith("video", ignoreCase = true) == true + -> compressVideo(uri, contentType, applicationContext, onReady, onError, mediaQuality) + contentType?.startsWith("image", ignoreCase = true) == true && !contentType.contains("gif") && !contentType.contains("svg") + -> compressImage(uri, contentType, applicationContext, onReady, onError, mediaQuality) + else -> onReady(uri, contentType, null) + } + } - override fun onStart(index: Int) { - // Compression start - } - - override fun onSuccess( - index: Int, - size: Long, - path: String?, - ) { - if (path != null) { - Log.d("MediaCompressor", "Video compression success. Compressed size [$size]") - onReady(Uri.fromFile(File(path)), contentType, size) - } else { - Log.d("MediaCompressor", "Video compression successful, but returned null path") - onError(R.string.compression_returned_null) - } - } - - override fun onFailure( - index: Int, - failureMessage: String, - ) { - // keeps going with original video - Log.d("MediaCompressor", "Video compression failed: $failureMessage") - onReady(uri, contentType, null) - } - - override fun onCancelled(index: Int) { - onError(R.string.compression_cancelled) - } - }, - ) - } else if (contentType?.startsWith("image", true) == true && !contentType.contains("gif") && !contentType.contains("svg")) { - val imageQuality = - when (mediaQuality) { - CompressorQuality.VERY_LOW -> 40 - CompressorQuality.LOW -> 50 - CompressorQuality.MEDIUM -> 60 - CompressorQuality.HIGH -> 80 - CompressorQuality.VERY_HIGH -> 90 - else -> 60 - } - try { - Log.d("MediaCompressor", "Using image compression $mediaQuality") - val tempFile = from(uri, contentType, applicationContext) - val compressedImageFile = - Compressor.compress(applicationContext, tempFile) { - default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality) - } - Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]") - onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length()) - } catch (e: Exception) { - Log.d("MediaCompressor", "Image compression failed: ${e.message}") - if (e is CancellationException) throw e - e.printStackTrace() - onReady(uri, contentType, null) + private fun compressVideo( + uri: Uri, + contentType: String?, + applicationContext: Context, + onReady: (Uri, String?, Long?) -> Unit, + onError: (Int) -> Unit, + mediaQuality: CompressorQuality, + ) { + val videoQuality = + when (mediaQuality) { + CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW + CompressorQuality.LOW -> VideoQuality.LOW + CompressorQuality.MEDIUM -> VideoQuality.MEDIUM + CompressorQuality.HIGH -> VideoQuality.HIGH + CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH + else -> VideoQuality.MEDIUM } - } else { + + Log.d("MediaCompressor", "Using video compression $mediaQuality") + + VideoCompressor.start( + // => This is required + context = applicationContext, + // => Source can be provided as content uris + uris = listOf(uri), + isStreamable = false, + // THIS STORAGE + // sharedStorageConfiguration = SharedStorageConfiguration( + // saveAt = SaveLocation.movies, // => default is movies + // videoName = "compressed_video" // => required name + // ), + // OR AND NOT BOTH + appSpecificStorageConfiguration = AppSpecificStorageConfiguration(), + configureWith = + Configuration( + quality = videoQuality, + // => required name + videoNames = listOf(UUID.randomUUID().toString()), + ), + listener = + object : CompressionListener { + override fun onProgress( + index: Int, + percent: Float, + ) { + } + + override fun onStart(index: Int) {} + + override fun onSuccess( + index: Int, + size: Long, + path: String?, + ) { + if (path != null) { + Log.d("MediaCompressor", "Video compression success. Compressed size [$size]") + onReady(Uri.fromFile(File(path)), contentType, size) + } else { + Log.d("MediaCompressor", "Video compression successful, but returned null path") + onError(R.string.compression_returned_null) + } + } + + override fun onFailure( + index: Int, + failureMessage: String, + ) { + Log.d("MediaCompressor", "Video compression failed: $failureMessage") + // keeps going with original video + onReady(uri, contentType, null) + } + + override fun onCancelled(index: Int) { + onError(R.string.compression_cancelled) + } + }, + ) + } + + private suspend fun compressImage( + uri: Uri, + contentType: String?, + context: Context, + onReady: (Uri, String?, Long?) -> Unit, + onError: (Int) -> Unit, + mediaQuality: CompressorQuality, + ) { + val imageQuality = + when (mediaQuality) { + CompressorQuality.VERY_LOW -> 40 + CompressorQuality.LOW -> 50 + CompressorQuality.MEDIUM -> 60 + CompressorQuality.HIGH -> 80 + CompressorQuality.VERY_HIGH -> 90 + else -> 60 + } + + try { + Log.d("MediaCompressor", "Using image compression $mediaQuality") + val tempFile = from(uri, contentType, context) + val compressedImageFile = + Compressor.compress(context, tempFile) { + default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality) + } + Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]") + onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length()) + } catch (e: Exception) { + Log.d("MediaCompressor", "Image compression failed: ${e.message}") + if (e is CancellationException) throw e + e.printStackTrace() onReady(uri, contentType, null) } }