Skip to content

An open-source cross-platform alternative to AirDrop

feat: create missing directories on receiver

Tien Do Nam8ebec6f2702b
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index e3094f26..bc62691d 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
diff --git a/assets/CHANGELOG.md b/assets/CHANGELOG.md
index 12a0d5ad..73588e39 100644
--- a/assets/CHANGELOG.md
+++ b/assets/CHANGELOG.md
@@ -1,5 +1,6 @@
## 1.9.0 (2023-)
+- feat: directory share
- feat: allow deleting history entry when file could not be opened (by @TheGB0077)
- feat: close message request when copied / opened link
- feat(desktop): move troubleshoot out of navigation into send page
diff --git a/lib/provider/network/server_provider.dart b/lib/provider/network/server_provider.dart
index 1146998a..ff67ce15 100644
--- a/lib/provider/network/server_provider.dart
+++ b/lib/provider/network/server_provider.dart
@@ -29,6 +29,7 @@ import 'package:localsend_app/util/native/get_destination_directory.dart';
import 'package:localsend_app/util/native/platform_check.dart';
import 'package:localsend_app/util/native/tray_helper.dart';
import 'package:localsend_app/util/security_helper.dart';
+import 'package:path/path.dart' as p;
import 'package:routerino/routerino.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
@@ -334,14 +335,14 @@ class ServerNotifier extends Notifier<ServerState?> {
),
);
- final destinationPath = await _digestFilePath(
- dir: receiveState.destinationDirectory,
- fileName: receivingFile.desiredName!,
- );
+ try {
+ final destinationPath = await _digestFilePathAndPrepareDirectory(
+ parentDirectory: receiveState.destinationDirectory,
+ fileName: receivingFile.desiredName!,
+ );
- print('Saving ${receivingFile.file.fileName} to $destinationPath');
+ print('Saving ${receivingFile.file.fileName} to $destinationPath');
- try {
final saveToGallery = checkPlatformWithGallery() &&
ref.read(settingsProvider).saveToGallery &&
(receivingFile.file.fileType == FileType.image || receivingFile.file.fileType == FileType.video);
@@ -546,11 +547,17 @@ extension on Request {
}
/// If there is a file with the same name, then it appends a number to its file name
-Future<String> _digestFilePath({required String dir, required String fileName}) async {
+Future<String> _digestFilePathAndPrepareDirectory({required String parentDirectory, required String fileName}) async {
+ final actualFileName = p.basename(fileName);
+ final fileNameParts = p.split(fileName);
+ final dir = p.joinAll([parentDirectory, ...fileNameParts.take(fileNameParts.length - 1)]);
+
+ Directory(dir).createSync(recursive: true);
+
String destinationPath;
int counter = 1;
do {
- destinationPath = counter == 1 ? '$dir/$fileName' : '$dir/${fileName.withCount(counter)}';
+ destinationPath = counter == 1 ? p.join(dir, actualFileName) : p.join(dir, actualFileName.withCount(counter));
counter++;
} while (await File(destinationPath).exists());
return destinationPath;
diff --git a/lib/provider/selection/selected_sending_files_provider.dart b/lib/provider/selection/selected_sending_files_provider.dart
index 2c1e5b9c..3a6f411b 100644
--- a/lib/provider/selection/selected_sending_files_provider.dart
+++ b/lib/provider/selection/selected_sending_files_provider.dart
@@ -66,10 +66,12 @@ class SelectedSendingFilesNotifier extends Notifier<List<CrossFile>> {
}
Future<void> addDirectory(String directoryPath) async {
+ print('Reading files in $directoryPath');
final newFiles = <CrossFile>[];
await for (final entity in Directory(directoryPath).list(recursive: true)) {
if (entity is File) {
final relative = p.relative(entity.path, from: directoryPath).replaceAll('\\', '/');
+ print('Add file $relative');
final file = CrossFile(
name: relative,
fileType: relative.guessFileType(),
diff --git a/lib/util/native/file_picker.dart b/lib/util/native/file_picker.dart
index b4b6a05e..76996882 100644
--- a/lib/util/native/file_picker.dart
+++ b/lib/util/native/file_picker.dart
@@ -12,6 +12,7 @@ import 'package:localsend_app/util/sleep.dart';
import 'package:localsend_app/util/ui/asset_picker_translated_text_delegate.dart';
import 'package:localsend_app/widget/dialogs/loading_dialog.dart';
import 'package:localsend_app/widget/dialogs/message_input_dialog.dart';
+import 'package:permission_handler/permission_handler.dart';
import 'package:routerino/routerino.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
@@ -68,6 +69,20 @@ enum FilePickerOption {
break;
case FilePickerOption.folder:
ref.read(pickingStatusProvider.notifier).state = true;
+
+ if (checkPlatform([TargetPlatform.android])) {
+ try {
+ await Permission.manageExternalStorage.request();
+ } catch (e) {
+ print(e);
+ }
+ }
+
+ // ignore: use_build_context_synchronously
+ if (!context.mounted) {
+ return;
+ }
+
// ignore: unawaited_futures
showDialog(
context: context,
diff --git a/lib/util/native/file_saver.dart b/lib/util/native/file_saver.dart
index e9d825d7..e1418598 100644
--- a/lib/util/native/file_saver.dart
+++ b/lib/util/native/file_saver.dart
@@ -4,7 +4,7 @@ import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
/// Saves the data [stream] to the [destinationPath].
-/// [onProgress] will be called on every 100 KB.
+/// [onProgress] will be called on every 100 ms.
Future<void> saveFile({
required String destinationPath,
required String name,