#135 safer entry fetch

This commit is contained in:
Thibault Deckers 2021-11-30 12:04:18 +09:00
parent 42aad255f1
commit 5d61d28838
3 changed files with 13 additions and 11 deletions

View file

@ -636,16 +636,16 @@ class MetadataFetchHandler(private val context: Context) : MethodCallHandler {
return
}
val saveExposureTime: (value: Rational) -> Unit = {
val saveExposureTime = fun(value: Rational) {
// `TAG_EXPOSURE_TIME` as a string is sometimes a ratio, sometimes a decimal
// so we explicitly request it as a rational (e.g. 1/100, 1/14, 71428571/1000000000, 4000/1000, 2000000000/500000000)
// and process it to make sure the numerator is `1` when the ratio value is less than 1
val num = it.numerator
val denom = it.denominator
val num = value.numerator
val denom = value.denominator
metadataMap[KEY_EXPOSURE_TIME] = when {
num >= denom -> "${it.toSimpleString(true)}"
num >= denom -> "${value.toSimpleString(true)}"
num != 1L && num != 0L -> Rational(1, (denom / num.toDouble()).roundToLong()).toString()
else -> it.toString()
else -> value.toString()
}
}

View file

@ -223,7 +223,9 @@ object ExifInterfaceHelper {
val dirs = DirType.values().map { Pair(it, it.createDirectory()) }.toMap()
// exclude Exif directory when it only includes image size
val isUselessExif: (Map<String, String>) -> Boolean = { it.size == 2 && it.containsKey("Image Height") && it.containsKey("Image Width") }
val isUselessExif = fun(it: Map<String, String>): Boolean {
return it.size == 2 && it.containsKey("Image Height") && it.containsKey("Image Width")
}
return HashMap<String, Map<String, String>>().apply {
put("Exif", describeDir(exif, dirs, baseTags).takeUnless(isUselessExif) ?: hashMapOf())

View file

@ -45,15 +45,15 @@ class MediaStoreImageProvider : ImageProvider() {
fetchFrom(context, isModified, handleNewEntry, VIDEO_CONTENT_URI, VIDEO_PROJECTION)
}
// the provided URI can point to the wrong media collection,
// e.g. a GIF image with the URI `content://media/external/video/media/[ID]`
// so the effective entry URI may not match the provided URI
override fun fetchSingle(context: Context, uri: Uri, sourceMimeType: String?, callback: ImageOpCallback) {
var found = false
val fetched = arrayListOf<FieldMap>()
val id = uri.tryParseId()
val onSuccess = fun(entry: FieldMap) {
entry["uri"] = uri.toString()
fetched.add(entry)
}
val alwaysValid = { _: Int, _: Int -> true }
val alwaysValid: NewEntryChecker = fun(_: Int, _: Int): Boolean = true
val onSuccess: NewEntryHandler = fun(entry: FieldMap) { fetched.add(entry) }
if (id != null) {
if (!found && (sourceMimeType == null || isImage(sourceMimeType))) {
val contentUri = ContentUris.withAppendedId(IMAGE_CONTENT_URI, id)