fixed mimetype detection to prefer metadata-extractor
This commit is contained in:
parent
5258ab721a
commit
c163e21910
3 changed files with 11 additions and 82 deletions
|
@ -37,7 +37,7 @@ import deckers.thibault.aves.utils.MetadataExtractorHelper.getSafeDateMillis
|
|||
import deckers.thibault.aves.utils.MetadataExtractorHelper.getSafeDescription
|
||||
import deckers.thibault.aves.utils.MetadataExtractorHelper.getSafeInt
|
||||
import deckers.thibault.aves.utils.MetadataExtractorHelper.getSafeRational
|
||||
import deckers.thibault.aves.utils.MimeTypes.getMimeTypeForExtension
|
||||
import deckers.thibault.aves.utils.MetadataExtractorHelper.getSafeString
|
||||
import deckers.thibault.aves.utils.MimeTypes.isImage
|
||||
import deckers.thibault.aves.utils.MimeTypes.isSupportedByMetadataExtractor
|
||||
import deckers.thibault.aves.utils.MimeTypes.isVideo
|
||||
|
@ -200,14 +200,11 @@ class MetadataHandler(private val context: Context) : MethodCallHandler {
|
|||
// 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`
|
||||
if (dir.containsTag(FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE)) {
|
||||
val detectedMimeType = dir.getString(FileTypeDirectory.TAG_DETECTED_FILE_MIME_TYPE)
|
||||
if (detectedMimeType != null && detectedMimeType != mimeType) {
|
||||
// file extension is unreliable, but we use it as a tie breaker
|
||||
val extensionMimeType = extension?.toLowerCase(Locale.ROOT)?.let { getMimeTypeForExtension(it) }
|
||||
if (extensionMimeType == null || detectedMimeType == extensionMimeType) {
|
||||
metadataMap[KEY_MIME_TYPE] = detectedMimeType
|
||||
}
|
||||
// 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) {
|
||||
metadataMap[KEY_MIME_TYPE] = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ object MetadataExtractorHelper {
|
|||
if (this.containsTag(tag)) save(this.getDescription(tag))
|
||||
}
|
||||
|
||||
fun Directory.getSafeString(tag: Int, save: (value: String) -> Unit) {
|
||||
if (this.containsTag(tag)) save(this.getString(tag))
|
||||
}
|
||||
|
||||
fun Directory.getSafeBoolean(tag: Int, save: (value: Boolean) -> Unit) {
|
||||
if (this.containsTag(tag)) save(this.getBoolean(tag))
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package deckers.thibault.aves.utils
|
||||
|
||||
import java.util.*
|
||||
|
||||
object MimeTypes {
|
||||
private const val IMAGE = "image"
|
||||
|
||||
|
@ -12,44 +10,20 @@ object MimeTypes {
|
|||
private const val HEIF = "image/heif"
|
||||
private const val ICO = "image/x-icon"
|
||||
private const val JPEG = "image/jpeg"
|
||||
private const val PCX = "image/x-pcx"
|
||||
private const val PNG = "image/png"
|
||||
private const val PSD = "image/x-photoshop" // aka "image/vnd.adobe.photoshop"
|
||||
private const val TIFF = "image/tiff"
|
||||
const val TIFF = "image/tiff"
|
||||
private const val WBMP = "image/vnd.wap.wbmp"
|
||||
const val WEBP = "image/webp"
|
||||
|
||||
// raw raster
|
||||
private const val ARW = "image/x-sony-arw"
|
||||
private const val CR2 = "image/x-canon-cr2"
|
||||
private const val CRW = "image/x-canon-crw"
|
||||
private const val DCR = "image/x-kodak-dcr"
|
||||
private const val DNG = "image/x-adobe-dng"
|
||||
private const val ERF = "image/x-epson-erf"
|
||||
private const val K25 = "image/x-kodak-k25"
|
||||
private const val KDC = "image/x-kodak-kdc"
|
||||
private const val MRW = "image/x-minolta-mrw"
|
||||
private const val NEF = "image/x-nikon-nef"
|
||||
private const val NRW = "image/x-nikon-nrw"
|
||||
private const val ORF = "image/x-olympus-orf"
|
||||
private const val PEF = "image/x-pentax-pef"
|
||||
private const val RAF = "image/x-fuji-raf"
|
||||
private const val RAW = "image/x-panasonic-raw"
|
||||
private const val RW2 = "image/x-panasonic-rw2"
|
||||
private const val SR2 = "image/x-sony-sr2"
|
||||
private const val SRF = "image/x-sony-srf"
|
||||
private const val SRW = "image/x-samsung-srw"
|
||||
private const val X3F = "image/x-sigma-x3f"
|
||||
|
||||
// vector
|
||||
const val SVG = "image/svg+xml"
|
||||
|
||||
private const val VIDEO = "video"
|
||||
|
||||
private const val AVI = "video/avi"
|
||||
private const val MOV = "video/quicktime"
|
||||
private const val MP2T = "video/mp2t"
|
||||
private const val MP4 = "video/mp4"
|
||||
private const val WEBM = "video/webm"
|
||||
|
||||
@JvmStatic
|
||||
|
@ -89,50 +63,4 @@ object MimeTypes {
|
|||
DNG, PNG -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getMimeTypeForExtension(extension: String?): String? = when (extension?.toLowerCase(Locale.ROOT)) {
|
||||
// generic raster
|
||||
".bmp" -> BMP
|
||||
".gif" -> GIF
|
||||
".heic" -> HEIC
|
||||
".heif" -> HEIF
|
||||
".ico" -> ICO
|
||||
".jpg", ".jpeg", ".jpe" -> JPEG
|
||||
".pcx" -> PCX
|
||||
".png" -> PNG
|
||||
".psd" -> PSD
|
||||
".tiff", ".tif" -> TIFF
|
||||
".wbmp" -> WBMP
|
||||
".webp" -> WEBP
|
||||
// raw raster
|
||||
".arw" -> ARW
|
||||
".cr2" -> CR2
|
||||
".crw" -> CRW
|
||||
".dcr" -> DCR
|
||||
".dng" -> DNG
|
||||
".erf" -> ERF
|
||||
".k25" -> K25
|
||||
".kdc" -> KDC
|
||||
".mrw" -> MRW
|
||||
".nef" -> NEF
|
||||
".nrw" -> NRW
|
||||
".orf" -> ORF
|
||||
".pef" -> PEF
|
||||
".raf" -> RAF
|
||||
".raw" -> RAW
|
||||
".rw2" -> RW2
|
||||
".sr2" -> SR2
|
||||
".srf" -> SRF
|
||||
".srw" -> SRW
|
||||
".x3f" -> X3F
|
||||
// vector
|
||||
".svg" -> SVG
|
||||
// video
|
||||
".avi" -> AVI
|
||||
".m2ts" -> MP2T
|
||||
".mov", ".qt" -> MOV
|
||||
".mp4", ".m4a", ".m4p", ".m4b", ".m4r", ".m4v" -> MP4
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue