From e187e5392f3d306a01a5fbde48b39f1db85edfb4 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Thu, 3 Oct 2024 09:30:45 +0200 Subject: [PATCH] refactor "from" method --- .../amethyst/ui/components/MediaCompressor.kt | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 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 be09b28d9..405e0d32b 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 @@ -38,6 +38,8 @@ import id.zelory.compressor.constraint.default import kotlinx.coroutines.CancellationException import java.io.File import java.io.FileOutputStream +import java.io.InputStream +import java.io.OutputStream import java.util.UUID class MediaCompressor { @@ -169,7 +171,7 @@ class MediaCompressor { try { Log.d("MediaCompressor", "Using image compression $mediaQuality") - val tempFile = from(uri, contentType, context) + val tempFile = from(uri, context) val compressedImageFile = Compressor.compress(context, tempFile) { default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality) @@ -185,30 +187,32 @@ class MediaCompressor { } private fun from( - uri: Uri?, - contentType: String?, + uri: Uri, context: Context, ): File { - val extension = contentType?.let { MimeTypeMap.getSingleton().getExtensionFromMimeType(it) } ?: "" - - val inputStream = context.contentResolver.openInputStream(uri!!) - val fileName: String = UUID.randomUUID().toString() + ".$extension" + val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri)) ?: "" + val inputStream = context.contentResolver.openInputStream(uri)!! + val fileName = UUID.randomUUID().toString() + ".$extension" val (name, ext) = splitFileName(fileName) val tempFile = File.createTempFile(name, ext) - inputStream?.use { input -> - FileOutputStream(tempFile).use { output -> - val buffer = ByteArray(1024 * 50) - var read: Int = input.read(buffer) - while (read != -1) { - output.write(buffer, 0, read) - read = input.read(buffer) - } - } - } + + copyStream(inputStream, FileOutputStream(tempFile)) return tempFile } + private fun copyStream( + input: InputStream, + output: OutputStream, + ) { + val buffer = ByteArray(1024 * 50) + var read = input.read(buffer) + while (read != -1) { + output.write(buffer, 0, read) + read = input.read(buffer) + } + } + private fun splitFileName(fileName: String): Pair { val i = fileName.lastIndexOf(".") return if (i != -1) {