From a892abb809bede38b002e8674e1727d2e46eea93 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Fri, 5 Jun 2026 16:44:57 -0500 Subject: [PATCH] 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 (cherry picked from commit 9d62588a2739d7eb1f12164d0f2c4ae7d5be4e38) --- .../ExpoReceiveAndroidIntentsModule.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/modules/expo-receive-android-intents/android/src/main/java/xyz/blueskyweb/app/exporeceiveandroidintents/ExpoReceiveAndroidIntentsModule.kt b/modules/expo-receive-android-intents/android/src/main/java/xyz/blueskyweb/app/exporeceiveandroidintents/ExpoReceiveAndroidIntentsModule.kt index 2a365a1e59..af7344216b 100644 --- a/modules/expo-receive-android-intents/android/src/main/java/xyz/blueskyweb/app/exporeceiveandroidintents/ExpoReceiveAndroidIntentsModule.kt +++ b/modules/expo-receive-android-intents/android/src/main/java/xyz/blueskyweb/app/exporeceiveandroidintents/ExpoReceiveAndroidIntentsModule.kt @@ -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,