Merge pull request #1300 from davotoula/sniff-content-type-from-extension-when-empty-in-response-headers

Sniff content type from extension when empty in response headers
This commit is contained in:
Vitor Pamplona
2025-03-24 17:50:22 -04:00
committed by GitHub
@@ -55,25 +55,25 @@ object MediaSaverToDisk {
onSuccess: () -> Any?, onSuccess: () -> Any?,
onError: (Throwable) -> Any?, onError: (Throwable) -> Any?,
) { ) {
videoUri?.let { theVideoUri -> when {
if (!theVideoUri.startsWith("file")) { videoUri.isNullOrBlank() -> return
downloadAndSave( videoUri.startsWith("file") ->
url = theVideoUri,
mimeType = mimeType,
context = localContext,
forceProxy = forceProxy,
onSuccess = onSuccess,
onError = onError,
)
} else {
save( save(
localFile = theVideoUri.toUri().toFile(), localFile = videoUri.toUri().toFile(),
mimeType = mimeType, mimeType = mimeType,
context = localContext, context = localContext,
onSuccess = onSuccess, onSuccess = onSuccess,
onError = onError, onError = onError,
) )
} else ->
downloadAndSave(
url = videoUri,
mimeType = mimeType,
forceProxy = forceProxy,
context = localContext,
onSuccess = onSuccess,
onError = onError,
)
} }
} }
@@ -120,8 +120,8 @@ object MediaSaverToDisk {
check(response.isSuccessful) check(response.isSuccessful)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentType = response.header("Content-Type") val contentType = response.header("Content-Type") ?: getMimeTypeFromExtension(trimInlineMetaData(url))
checkNotNull(contentType) { "Can't find out the content type" } check(contentType.isNotBlank()) { "Can't find out the content type" }
val realType = val realType =
if (contentType == "application/octet-stream") { if (contentType == "application/octet-stream") {
@@ -240,11 +240,9 @@ object MediaSaverToDisk {
File( File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
PICTURES_SUBDIRECTORY, PICTURES_SUBDIRECTORY,
) ).apply {
if (!exists()) mkdirs()
if (!subdirectory.exists()) { }
subdirectory.mkdirs()
}
val outputFile = File(subdirectory, fileName) val outputFile = File(subdirectory, fileName)