Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/common/picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import 'package:fl_clash/common/common.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

Future<void> copyPickedFileToPath(
PlatformFile file,
String destinationPath,
) async {
final sourcePath = file.path;
if (sourcePath != null) {
await File(sourcePath).safeCopy(destinationPath);
return;
}
await File(destinationPath).safeWriteAsBytes(await file.readBytes());
}

class Picker {
Future<PlatformFile?> pickerFile() async {
return FilePicker.pickFile(initialDirectory: await appPath.downloadDirPath);
Expand Down
7 changes: 2 additions & 5 deletions lib/views/backup_and_restore.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:dynamic_color/dynamic_color.dart';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/common/dav_client.dart';
Expand Down Expand Up @@ -139,9 +137,8 @@ class _BackupAndRestoreState extends ConsumerState<BackupAndRestore>
Future<void> _restoreOnLocal(RestoreOption option) async {
final appLocalizations = context.appLocalizations;
final file = await picker.pickerFile();
final path = file?.path;
if (path == null) return;
await File(path).safeCopy(await appPath.backupFilePath);
if (file == null) return;
await copyPickedFileToPath(file, await appPath.backupFilePath);
final res = await globalState.loadingRun<bool>(
() async {
await globalState.container
Expand Down
43 changes: 43 additions & 0 deletions test/common/picker_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/file_picker.dart';
import 'package:fl_clash/common/picker.dart';
Expand All @@ -25,5 +26,47 @@ void main() {

expect(String.fromCharCodes(bytes), 'mixed-port: 7890');
});

test(
'copies a bytes-only picked file to the restore destination',
() async {
final directory = await Directory.systemTemp.createTemp(
'fl_clash_picker_restore_test_',
);
addTearDown(() => directory.delete(recursive: true));
final destination = File('${directory.path}/backup.zip');
final platformFile = PlatformFile(
name: 'backup.zip',
size: 3,
bytes: Uint8List.fromList([1, 2, 3]),
);

await copyPickedFileToPath(platformFile, destination.path);

expect(await destination.readAsBytes(), [1, 2, 3]);
},
);

test(
'copies a path-backed picked file to the restore destination',
() async {
final directory = await Directory.systemTemp.createTemp(
'fl_clash_picker_restore_path_test_',
);
addTearDown(() => directory.delete(recursive: true));
final source = File('${directory.path}/source.zip');
final destination = File('${directory.path}/backup.zip');
await source.writeAsBytes([4, 5, 6]);
final platformFile = PlatformFile(
name: 'source.zip',
path: source.path,
size: 3,
);

await copyPickedFileToPath(platformFile, destination.path);

expect(await destination.readAsBytes(), [4, 5, 6]);
},
);
});
}
Loading