Extension mismatch fix
Temp file leak fix
This commit is contained in:
+23
-3
@@ -110,14 +110,33 @@ class MetadataStripper {
|
|||||||
uri: Uri,
|
uri: Uri,
|
||||||
context: Context,
|
context: Context,
|
||||||
): StrippingResult {
|
): StrippingResult {
|
||||||
|
var tempFile: File? = null
|
||||||
return try {
|
return try {
|
||||||
val tempFile = File.createTempFile("stripped_", ".jpg", context.cacheDir)
|
val mimeType = context.contentResolver.getType(uri) ?: ""
|
||||||
|
val extension =
|
||||||
|
when {
|
||||||
|
mimeType.endsWith("jpeg", ignoreCase = true) ||
|
||||||
|
mimeType.endsWith("jpg", ignoreCase = true) -> ".jpg"
|
||||||
|
|
||||||
context.contentResolver.openInputStream(uri)?.use { input ->
|
mimeType.endsWith("png", ignoreCase = true) -> ".png"
|
||||||
|
|
||||||
|
mimeType.endsWith("webp", ignoreCase = true) -> ".webp"
|
||||||
|
|
||||||
|
else -> ".tmp"
|
||||||
|
}
|
||||||
|
tempFile = File.createTempFile("stripped_", extension, context.cacheDir)
|
||||||
|
|
||||||
|
val inputStream =
|
||||||
|
context.contentResolver.openInputStream(uri)
|
||||||
|
?: run {
|
||||||
|
tempFile.delete()
|
||||||
|
return StrippingResult(uri, false)
|
||||||
|
}
|
||||||
|
inputStream.use { input ->
|
||||||
tempFile.outputStream().use { output ->
|
tempFile.outputStream().use { output ->
|
||||||
input.copyTo(output)
|
input.copyTo(output)
|
||||||
}
|
}
|
||||||
} ?: return StrippingResult(uri, false)
|
}
|
||||||
|
|
||||||
val exif = ExifInterface(tempFile.absolutePath)
|
val exif = ExifInterface(tempFile.absolutePath)
|
||||||
for (tag in SENSITIVE_EXIF_TAGS) {
|
for (tag in SENSITIVE_EXIF_TAGS) {
|
||||||
@@ -129,6 +148,7 @@ class MetadataStripper {
|
|||||||
StrippingResult(tempFile.toUri(), true)
|
StrippingResult(tempFile.toUri(), true)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e is CancellationException) throw e
|
if (e is CancellationException) throw e
|
||||||
|
tempFile?.delete()
|
||||||
Log.d("MetadataStripper", "Failed to strip image metadata: ${e.message}")
|
Log.d("MetadataStripper", "Failed to strip image metadata: ${e.message}")
|
||||||
StrippingResult(uri, false)
|
StrippingResult(uri, false)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user