From 1581e13bd62dabe45d17584ee22f1c851c7e39e2 Mon Sep 17 00:00:00 2001 From: Sai Gokula Krishnan Date: Tue, 29 Sep 2026 12:30:35 +0530 Subject: [PATCH] fix(share): preserve Android attachments with duplicate filenames --- .../main/kotlin/com/dartnative/share/DNShareBridge.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/dartnative_share/android/src/main/kotlin/com/dartnative/share/DNShareBridge.kt b/plugins/dartnative_share/android/src/main/kotlin/com/dartnative/share/DNShareBridge.kt index df7757c..69c6adc 100644 --- a/plugins/dartnative_share/android/src/main/kotlin/com/dartnative/share/DNShareBridge.kt +++ b/plugins/dartnative_share/android/src/main/kotlin/com/dartnative/share/DNShareBridge.kt @@ -172,7 +172,7 @@ private fun chooserFor(target: Intent, token: Long?): Intent { * this becomes the photo/video caption. * * Each file is copied into the app's cache dir - * (`cacheDir/dn_share/`) and vended via `FileProvider` so the + * (`cacheDir/dn_share//`) and vended via `FileProvider` so the * receiving app can read it. The cache dir is cleared on every call. */ @Keep @@ -220,14 +220,17 @@ private fun shareFilesImpl( val authority = providerAuthority(ctx) val uris = ArrayList(paths.size) - for (path in paths) { + for ((index, path) in paths.withIndex()) { if (path.isEmpty()) continue val src = File(path) if (!src.exists()) { Log.w(TAG, "shareFiles: source file does not exist: $path") continue } - val dest = File(cacheDir, src.name) + // Preserve the filename without colliding with another source. + val attachmentDir = File(cacheDir, index.toString()) + attachmentDir.mkdirs() + val dest = File(attachmentDir, src.name) src.copyTo(dest, overwrite = true) uris += FileProvider.getUriForFile(ctx, authority, dest) } @@ -341,5 +344,5 @@ private fun startChooser(chooser: Intent) { private fun clearDir(dir: File) { if (!dir.exists()) return - dir.listFiles()?.forEach { it.delete() } + dir.listFiles()?.forEach { it.deleteRecursively() } }