clean up orphan temp files on android share failure paths

createFile writes to the cache dir before the copy/compress attempt, so
the bail-out paths left a temp file behind. Delete the file on each
failure path (no input stream, copy/write throws, getVideoInfo returns
null). Also wraps the image write in a use {} block so the stream is
closed if compress throws. The success path still keeps the file, since
its path is passed to the compose intent downstream.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
(cherry picked from commit 9d62588a2739d7eb1f12164d0f2c4ae7d5be4e38)
This commit is contained in:
Eric Bailey
2026-06-05 16:44:57 -05:00
parent c808a10577
commit a892abb809
@@ -164,15 +164,25 @@ class ExpoReceiveAndroidIntentsModule : Module() {
// app, since this runs synchronously on the module init path.
try {
FileOutputStream(file).use { out ->
val input = appContext.currentActivity?.contentResolver?.openInputStream(uri) ?: return
val input =
appContext.currentActivity?.contentResolver?.openInputStream(uri)
?: run {
file.delete()
return
}
input.use { it.copyTo(out) }
}
} catch (e: Exception) {
Log.w(TAG, "Failed to copy shared video to cache", e)
file.delete()
return
}
val info = getVideoInfo(uri) ?: return
val info =
getVideoInfo(uri) ?: run {
file.delete()
return
}
val encodedText = text?.let { URLEncoder.encode(it, "UTF-8") }
@@ -198,10 +208,16 @@ class ExpoReceiveAndroidIntentsModule : Module() {
// We have to save this so that we can access it later when uploading the image.
// createTempFile will automatically place a unique string between "img" and "temp.jpeg"
val file = createFile("jpeg")
val out = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
out.flush()
out.close()
try {
FileOutputStream(file).use { out ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
out.flush()
}
} catch (e: Exception) {
Log.w(TAG, "Failed to write shared image to cache", e)
file.delete()
return null
}
return mapOf(
"width" to bitmap.width,