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 0430a5029..0d618a0c0 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 @@ -111,26 +111,30 @@ object MediaSaverToDisk { withContext(Dispatchers.IO) { check(response.isSuccessful) + val trimmedUrl = trimInlineMetaData(url) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - val contentType = response.header("Content-Type") ?: getMimeTypeFromExtension(trimInlineMetaData(url)) - check(contentType.isNotBlank()) { "Can't find out the content type" } + val headerType = + response + .header("Content-Type") + ?.substringBefore(";") + ?.trim() val realType = - if (contentType == "application/octet-stream") { - mimeType ?: getMimeTypeFromExtension(url) - } else { - contentType - } + headerType?.takeIf(::isMediaMimeType) + ?: mimeType?.takeIf(::isMediaMimeType) + ?: getMimeTypeFromExtension(trimmedUrl).takeIf(::isMediaMimeType) + ?: "" + check(realType.isNotBlank()) { "Can't find out the content type" } saveContentQ( - displayName = File(trimInlineMetaData(url)).nameWithoutExtension, + displayName = File(trimmedUrl).nameWithoutExtension, contentType = realType, contentSource = response.body.source(), contentResolver = context.contentResolver, ) } else { saveContentDefault( - fileName = File(trimInlineMetaData(url)).name, + fileName = File(trimmedUrl).name, contentSource = response.body.source(), context = context, ) @@ -150,6 +154,14 @@ object MediaSaverToDisk { MimeTypeMap.getSingleton().getMimeTypeFromExtension(it).orEmpty() } + private fun isMediaMimeType(type: String): Boolean = + type.isNotBlank() && + ( + type.startsWith("image/", ignoreCase = true) || + type.startsWith("video/", ignoreCase = true) || + type.startsWith("audio/", ignoreCase = true) + ) + @OptIn(ExperimentalUuidApi::class) fun save( localFile: File,