An open-source cross-platform alternative to AirDrop
fix(android): allow receiving files with the same name after moving them to Trash (#3590)
Shlomo0090c7f4f0bb
diff --git a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.ktindex 34034241..6da642cb 100644--- a/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt+++ b/app/android/app/src/main/kotlin/org/localsend/localsend_app/MainActivity.kt@@ -7,6 +7,7 @@ import android.content.Contextimport android.content.Intentimport android.content.pm.PackageManagerimport android.database.Cursor+import android.media.MediaScannerConnectionimport android.net.Uriimport android.os.Buildimport android.os.Environment@@ -17,6 +18,7 @@ import io.flutter.embedding.android.FlutterActivityimport io.flutter.embedding.engine.FlutterEngineimport io.flutter.plugin.common.MethodCallimport io.flutter.plugin.common.MethodChannel+import java.io.Fileimport java.text.SimpleDateFormatimport java.util.Dateimport java.util.Locale@@ -104,6 +106,8 @@ class MainActivity : FlutterActivity() {"openFileForWriting" -> handleOpenFileForWriting(call, result)+ "scanFile" -> handleScanFile(call, result)+"openContentUri" -> {openUri(context, call.argument<String>("uri")!!)result.success(null)@@ -168,6 +172,22 @@ class MainActivity : FlutterActivity() {Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f) != 0.0f;}+ private fun handleScanFile(call: MethodCall, result: MethodChannel.Result) {+ val path = call.argument<String>("path")+ if (path.isNullOrBlank() || !File(path).isAbsolute) {+ result.error("INVALID_ARGUMENT", "Expected an absolute filesystem path", null)+ return+ }++ try {+ MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null) { _, uri ->+ result.success(uri != null)+ }+ } catch (e: Exception) {+ result.error("SCAN_FAILED", e.message ?: "Failed to scan file", null)+ }+ }+private fun handleGetFileDescriptor(call: MethodCall, result: MethodChannel.Result) {val uriString = call.argument<String>("uri")if (uriString == null) {diff --git a/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart b/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dartindex b3e96102..735fab01 100644--- a/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart+++ b/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart@@ -1,4 +1,5 @@import 'dart:async';+import 'dart:io';import 'package:flutter/services.dart';import 'package:localsend_isolates/constants.dart';@@ -11,6 +12,7 @@ import 'package:localsend_isolates/src/isolate/child/sync_provider.dart';import 'package:localsend_isolates/src/isolate/dto/send_to_isolate_data.dart';import 'package:localsend_isolates/src/task/server/file_saver.dart';import 'package:localsend_isolates/src/task/server/http_server.dart';+import 'package:localsend_isolates/util/android_channel.dart';import 'package:localsend_isolates/util/future_queue.dart';import 'package:localsend_isolates/util/rust.dart';import 'package:logging/logging.dart';@@ -716,6 +718,13 @@ Future<void> _handleFileUpload({);} else {filePath = target.displayPath;+ final path = target.path;+ if (Platform.isAndroid && path != null) {+ // Scan before reporting success so Android can handle the saved file correctly+ // when it is moved to Trash, allowing another file with the same name.+ // https://github.com/localsend/localsend/issues/2007+ await scanFileAndroid(path: path);+ }}_logger.info('Saved ${dartFile.fileName}.');diff --git a/packages/localsend_isolates/lib/util/android_channel.dart b/packages/localsend_isolates/lib/util/android_channel.dartindex 215df594..9974703d 100644--- a/packages/localsend_isolates/lib/util/android_channel.dart+++ b/packages/localsend_isolates/lib/util/android_channel.dart@@ -1,3 +1,5 @@+import 'dart:async';+import 'package:flutter/services.dart';import 'package:localsend_isolates/util/content_uri_helper.dart';import 'package:logging/logging.dart';@@ -5,6 +7,24 @@ import 'package:logging/logging.dart';const _methodChannel = MethodChannel('org.localsend.localsend_app/localsend');final _logger = Logger('AndroidSaf');+/// Scans a saved file at an absolute filesystem [path]+Future<void> scanFileAndroid({required String path}) async {+ try {+ final scanned = await _methodChannel+ .invokeMethod<bool>('scanFile', {+ 'path': path,+ })+ .timeout(const Duration(seconds: 30));+ if (scanned != true) {+ _logger.warning('Android media scan returned no URI for the saved file: $path');+ }+ } on TimeoutException {+ _logger.warning('Android media scan timed out after 30 seconds for the saved file: $path');+ } catch (e, st) {+ _logger.warning('Android media scan failed for the saved file: $path', e, st);+ }+}+/// Opens [uri] for reading and returns an owned Linux file descriptor.////// The descriptor stays open after this call and must be closed by the native