diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MetadataHandler.kt b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MetadataHandler.kt index 227608cce..4b9ec136d 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MetadataHandler.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/channel/calls/MetadataHandler.kt @@ -53,6 +53,7 @@ import deckers.thibault.aves.utils.MimeTypes.isImage import deckers.thibault.aves.utils.MimeTypes.isMultimedia import deckers.thibault.aves.utils.MimeTypes.isSupportedByMetadataExtractor import deckers.thibault.aves.utils.MimeTypes.isVideo +import deckers.thibault.aves.utils.MimeTypes.tiffExtensionPattern import deckers.thibault.aves.utils.StorageUtils import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel @@ -185,12 +186,13 @@ class MetadataHandler(private val context: Context) : MethodCallHandler { private fun getCatalogMetadata(call: MethodCall, result: MethodChannel.Result) { val mimeType = call.argument("mimeType") val uri = call.argument("uri")?.let { Uri.parse(it) } + val path = call.argument("path") if (mimeType == null || uri == null) { result.error("getCatalogMetadata-args", "failed because of missing arguments", null) return } - val metadataMap = HashMap(getCatalogMetadataByMetadataExtractor(uri, mimeType)) + val metadataMap = HashMap(getCatalogMetadataByMetadataExtractor(uri, mimeType, path)) if (isVideo(mimeType)) { metadataMap.putAll(getVideoCatalogMetadataByMediaMetadataRetriever(uri)) } @@ -199,7 +201,7 @@ class MetadataHandler(private val context: Context) : MethodCallHandler { result.success(metadataMap) } - private fun getCatalogMetadataByMetadataExtractor(uri: Uri, mimeType: String): Map { + private fun getCatalogMetadataByMetadataExtractor(uri: Uri, mimeType: String, path: String?): Map { val metadataMap = HashMap() var foundExif = false @@ -212,14 +214,17 @@ class MetadataHandler(private val context: Context) : MethodCallHandler { // File type for (dir in metadata.getDirectoriesOfType(FileTypeDirectory::class.java)) { - // `metadata-extractor` sometimes detect the the wrong mime type (e.g. `pef` file as `tiff`) - // the content resolver / media store sometimes report the wrong mime type (e.g. `png` file as `jpeg`) - // `context.getContentResolver().getType()` sometimes return incorrect value - // `MediaMetadataRetriever.setDataSource()` sometimes fail with `status = 0x80000000` - // file extension is unreliable - // in the end, `metadata-extractor` is the most reliable, unless it reports `tiff` - dir.getSafeString(FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE) { - if (it != MimeTypes.TIFF) { + // * `metadata-extractor` sometimes detect the the wrong mime type (e.g. `pef` file as `tiff`) + // * the content resolver / media store sometimes report the wrong mime type (e.g. `png` file as `jpeg`, `tiff` as `srw`) + // * `context.getContentResolver().getType()` sometimes return incorrect value + // * `MediaMetadataRetriever.setDataSource()` sometimes fail with `status = 0x80000000` + // * file extension is unreliable + // In the end, `metadata-extractor` is the most reliable, except for `tiff` (false positives, false negatives), + // in which case we trust the file extension + if (path?.matches(tiffExtensionPattern) == true) { + metadataMap[KEY_MIME_TYPE] = MimeTypes.TIFF + } else { + dir.getSafeString(FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE) { metadataMap[KEY_MIME_TYPE] = it } } diff --git a/android/app/src/main/kotlin/deckers/thibault/aves/utils/MimeTypes.kt b/android/app/src/main/kotlin/deckers/thibault/aves/utils/MimeTypes.kt index 89c4befcd..924d50b65 100644 --- a/android/app/src/main/kotlin/deckers/thibault/aves/utils/MimeTypes.kt +++ b/android/app/src/main/kotlin/deckers/thibault/aves/utils/MimeTypes.kt @@ -87,4 +87,8 @@ object MimeTypes { DNG, PNG -> true else -> false } + + // extensions + + val tiffExtensionPattern = Regex(".*\\.tiff?", RegexOption.IGNORE_CASE) } diff --git a/lib/services/metadata_service.dart b/lib/services/metadata_service.dart index abf7ea68d..5bbed1ddb 100644 --- a/lib/services/metadata_service.dart +++ b/lib/services/metadata_service.dart @@ -43,6 +43,7 @@ class MetadataService { final result = await platform.invokeMethod('getCatalogMetadata', { 'mimeType': entry.mimeType, 'uri': entry.uri, + 'path': entry.path, }) as Map; result['contentId'] = entry.contentId; return CatalogMetadata.fromMap(result);