From bcd636b46e1ca173c770ff3a3d9d747f2e29c4ca Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Mon, 17 Mar 2025 19:24:15 +0000 Subject: [PATCH 1/4] sniff content type from extension when empty in response --- .../com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt index 5e7583c6f4..78308d2c41 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt @@ -120,8 +120,8 @@ object MediaSaverToDisk { check(response.isSuccessful) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val contentType = response.header("Content-Type") - checkNotNull(contentType) { "Can't find out the content type" } + val contentType = response.header("Content-Type") ?: getMimeTypeFromExtension(url) + check(contentType.isNotBlank()) { "Can't find out the content type" } val realType = if (contentType == "application/octet-stream") { From 93ed81dedb5beb92814f38595b3ebda1ed5f4320 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Tue, 18 Mar 2025 12:14:57 +0000 Subject: [PATCH 2/4] cleaner code saveDownloadingIfNeeded: refactor to use when and remove unnecessary null-safe calls --- .../amethyst/ui/actions/MediaSaverToDisk.kt | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt index 78308d2c41..95d7232c12 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt @@ -55,25 +55,25 @@ object MediaSaverToDisk { onSuccess: () -> Any?, onError: (Throwable) -> Any?, ) { - videoUri?.let { theVideoUri -> - if (!theVideoUri.startsWith("file")) { - downloadAndSave( - url = theVideoUri, - mimeType = mimeType, - context = localContext, - forceProxy = forceProxy, - onSuccess = onSuccess, - onError = onError, - ) - } else { + when { + videoUri.isNullOrBlank() -> return + videoUri.startsWith("file") -> save( - localFile = theVideoUri.toUri().toFile(), + localFile = videoUri.toUri().toFile(), mimeType = mimeType, context = localContext, onSuccess = onSuccess, onError = onError, ) - } + else -> + downloadAndSave( + url = videoUri, + mimeType = mimeType, + forceProxy = forceProxy, + context = localContext, + onSuccess = onSuccess, + onError = onError, + ) } } From 2f29a21d72f45495753f156390e8713ef907641a Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Tue, 18 Mar 2025 12:29:03 +0000 Subject: [PATCH 3/4] cleaner code saveContentDefault: use apply for clarity --- .../vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt index 95d7232c12..4f2aa90b28 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt @@ -240,11 +240,9 @@ object MediaSaverToDisk { File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), PICTURES_SUBDIRECTORY, - ) - - if (!subdirectory.exists()) { - subdirectory.mkdirs() - } + ).apply { + if (!exists()) mkdirs() + } val outputFile = File(subdirectory, fileName) From cdf2b10fd240f2221e8bebf1800411438764549d Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Tue, 18 Mar 2025 12:55:31 +0000 Subject: [PATCH 4/4] trim inline meta data to fix sniffing (required for DM messages) --- .../com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt index 4f2aa90b28..d79dd754ce 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/MediaSaverToDisk.kt @@ -120,7 +120,7 @@ object MediaSaverToDisk { check(response.isSuccessful) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val contentType = response.header("Content-Type") ?: getMimeTypeFromExtension(url) + val contentType = response.header("Content-Type") ?: getMimeTypeFromExtension(trimInlineMetaData(url)) check(contentType.isNotBlank()) { "Can't find out the content type" } val realType =