Skip to content

An open-source cross-platform alternative to AirDrop

fix(android): keep file names when saving to a custom folder (#3507)

Tien Do Nam3a79b003bb2c
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.kt
index ccc9a080..34034241 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
@@ -12,6 +12,7 @@ import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.Settings
+import android.webkit.MimeTypeMap
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
@@ -207,7 +208,6 @@ class MainActivity : FlutterActivity() {
private fun handleCreateFile(call: MethodCall, result: MethodChannel.Result) {
val parentUriString = call.argument<String>("parentUri")
val fileName = call.argument<String>("fileName")
- val mimeType = call.argument<String>("mimeType") ?: "application/octet-stream"
if (parentUriString == null || fileName == null) {
result.error("INVALID_ARGUMENT", "Missing parentUri or fileName", null)
return
@@ -228,6 +228,13 @@ class MainActivity : FlutterActivity() {
parentUri
}
+ // Providers append an extension when the MIME type does not match the one Android maps the
+ // file's extension to (e.g. "test.conf" as text/plain becomes "test.conf.txt"), so derive it
+ // from the same map. application/octet-stream keeps the name as-is for unknown extensions.
+ val extension = fileName.substringAfterLast('.', "").lowercase(Locale.ROOT)
+ val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
+ ?: "application/octet-stream"
+
val documentUri =
DocumentsContract.createDocument(contentResolver, parentDocumentUri, mimeType, fileName)
if (documentUri == null) {
diff --git a/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart b/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart
index e4bc059e..b3e96102 100644
--- a/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart
+++ b/packages/localsend_isolates/lib/src/isolate/child/server_isolate.dart
@@ -656,7 +656,6 @@ Future<void> _handleFileUpload({
cacheDirectory: config.cacheDirectory,
fileName: desiredName,
saveToGallery: shouldSaveToGallery,
- isImage: isImage,
createdDirectories: session.createdDirectories,
androidSdkInt: config.androidSdkInt,
);
diff --git a/packages/localsend_isolates/lib/src/task/server/file_saver.dart b/packages/localsend_isolates/lib/src/task/server/file_saver.dart
index a8a644f5..9eaffe8f 100644
--- a/packages/localsend_isolates/lib/src/task/server/file_saver.dart
+++ b/packages/localsend_isolates/lib/src/task/server/file_saver.dart
@@ -6,7 +6,6 @@ import 'package:localsend_isolates/util/android_channel.dart' as android_channel
import 'package:localsend_isolates/util/content_uri_helper.dart';
import 'package:localsend_isolates/util/file_path_helper.dart';
import 'package:logging/logging.dart';
-import 'package:mime/mime.dart';
import 'package:path/path.dart' as p;
final _logger = Logger('FileSaver');
@@ -46,7 +45,6 @@ Future<FileSaveTarget> prepareFileSaveTarget({
required String cacheDirectory,
required String fileName,
required bool saveToGallery,
- required bool isImage,
required Set<String> createdDirectories,
int? androidSdkInt,
}) async {
@@ -76,7 +74,6 @@ Future<FileSaveTarget> prepareFileSaveTarget({
final createdFile = await android_channel.createFileAndroid(
parentUri: parentUri,
fileName: finalName,
- mimeType: lookupMimeType(finalName) ?? (isImage ? 'image/*' : '*/*'),
);
return FileSaveTarget(
path: null,
diff --git a/packages/localsend_isolates/lib/util/android_channel.dart b/packages/localsend_isolates/lib/util/android_channel.dart
index f3bf701c..215df594 100644
--- a/packages/localsend_isolates/lib/util/android_channel.dart
+++ b/packages/localsend_isolates/lib/util/android_channel.dart
@@ -69,12 +69,10 @@ class CreatedFileAndroid {
Future<CreatedFileAndroid> createFileAndroid({
required String parentUri,
required String fileName,
- required String mimeType,
}) async {
final result = await _methodChannel.invokeMethod<Map>('createFile', {
'parentUri': parentUri,
'fileName': fileName,
- 'mimeType': mimeType,
});
if (result == null) {
throw StateError('Android could not create $fileName in $parentUri');