An open-source cross-platform alternative to AirDrop
feat: save directories in Android SAF
Tien Do Nam9d464e3b7944
diff --git a/app/lib/util/file_path_helper.dart b/app/lib/util/file_path_helper.dartindex 8349c034..d019f6fe 100644--- a/app/lib/util/file_path_helper.dart+++ b/app/lib/util/file_path_helper.dart@@ -49,6 +49,11 @@ extension FilePathStringExt on String {}}+ String withoutFileName() {+ final parts = replaceAll('\\', '/').split('/');+ return parts.take(parts.length - 1).join('/');+ }+FileType guessFileType() {switch (extension) {case 'bmp':diff --git a/app/lib/util/native/content_uri_helper.dart b/app/lib/util/native/content_uri_helper.dartindex 18245073..8baaf981 100644--- a/app/lib/util/native/content_uri_helper.dart+++ b/app/lib/util/native/content_uri_helper.dart@@ -49,4 +49,18 @@ class ContentUriHelper {return '$folderName/$withoutBasePath';}}++ /// Converts+ /// content://com.android.externalstorage.documents/tree/primary%3ALocalSend/subFolder+ /// to+ /// content://com.android.externalstorage.documents/tree/primary%3ALocalSend%2FsubFolder+ static String encodeTreeUri(String uri) {+ final treeIndex = uri.indexOf('/tree/');+ if (treeIndex == -1) {+ return uri;+ }++ final encodedPath = Uri.encodeComponent(Uri.decodeComponent(uri.substring(treeIndex + 6)));+ return uri.substring(0, treeIndex + 6) + encodedPath;+ }}diff --git a/app/lib/util/native/file_saver.dart b/app/lib/util/native/file_saver.dartindex b25616f1..b4205393 100644--- a/app/lib/util/native/file_saver.dart+++ b/app/lib/util/native/file_saver.dart@@ -4,6 +4,7 @@ import 'dart:typed_data';import 'package:gal/gal.dart';import 'package:legalize/legalize.dart';import 'package:localsend_app/util/file_path_helper.dart';+import 'package:localsend_app/util/native/content_uri_helper.dart';import 'package:logging/logging.dart';import 'package:path/path.dart' as p;import 'package:saf_stream/saf_stream.dart';@@ -30,8 +31,10 @@ Future<void> saveFile({SafWriteStreamInfo? safInfo;if (destinationPath.startsWith('content://')) {+ final uriString = ContentUriHelper.encodeTreeUri(destinationPath.withoutFileName());+ _logger.info('Using SAF to save file to $uriString');safInfo = await _saf.startWriteStream(- Uri.parse(destinationPath),+ Uri.parse(uriString),name,isImage ? 'image/*' : '*/*',);@@ -39,8 +42,10 @@ Future<void> saveFile({final sdCardPath = getSdCardPath(destinationPath);if (sdCardPath != null) {// Use Android SAF to save the file to the SD card+ final uriString = ContentUriHelper.encodeTreeUri(sdCardPath.path.withoutFileName());+ _logger.info('Using SAF to save file to $uriString');safInfo = await _saf.startWriteStream(- Uri.parse('content://com.android.externalstorage.documents/tree/${sdCardPath.sdCardId}:${sdCardPath.path}'),+ Uri.parse('content://com.android.externalstorage.documents/tree/${sdCardPath.sdCardId}:$uriString'),name,isImage ? 'image/*' : '*/*',);diff --git a/app/test/unit/util/native/content_uri_helper_test.dart b/app/test/unit/util/native/content_uri_helper_test.dartindex 1f91cd6b..d28f3dcd 100644--- a/app/test/unit/util/native/content_uri_helper_test.dart+++ b/app/test/unit/util/native/content_uri_helper_test.dart@@ -98,4 +98,21 @@ void main() {);});});++ group('encodeTreeUri', () {+ test('should return the encoded tree uri', () {+ expect(+ ContentUriHelper.encodeTreeUri(+ 'content://com.android.externalstorage.documents/tree/primary%3ALocalSend/subFolder'),+ 'content://com.android.externalstorage.documents/tree/primary%3ALocalSend%2FsubFolder',+ );+ });++ test('should return the encoded tree uri with a folder in SD card', () {+ expect(+ ContentUriHelper.encodeTreeUri('content://com.android.externalstorage.documents/tree/1234-5678:LocalSend/subFolder'),+ 'content://com.android.externalstorage.documents/tree/1234-5678%3ALocalSend%2FsubFolder',+ );+ });+ });}