Skip to content

Commit

Permalink
Merge pull request #897 from miguelpruivo/bug/cannot-pick-filenames-w…
Browse files Browse the repository at this point in the history
…ith-comma-followed-by-blank

#890: cannot pick filenames with comma followed by blank
  • Loading branch information
Miguel Ruivo authored Nov 28, 2021
2 parents 13bc9b4 + bfd0548 commit f72d4b7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.2.5

##### Desktop (macOS)
Fixes the issue of picking filenames that contain a comma followed by a space ([#890](https://github.com/miguelpruivo/flutter_file_picker/issues/890)).

## 4.2.4

##### iOS
Expand Down
25 changes: 18 additions & 7 deletions lib/src/file_picker_macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,32 @@ class FilePickerMacOS extends FilePicker {
.replaceAll('\n', '\\\n');

/// Transforms the result string (stdout) of `osascript` into a [List] of
/// file paths.
/// POSIX file paths.
List<String> resultStringToFilePaths(String fileSelectionResult) {
if (fileSelectionResult.trim().isEmpty) {
return [];
}
return fileSelectionResult

final paths = fileSelectionResult
.trim()
.split(', ')
.split(', alias ')
.map((String path) => path.trim())
.where((String path) => path.isNotEmpty)
.map((String path) {
final pathElements = path.split(':').where((e) => e.isNotEmpty).toList();
.toList();

if (paths.length == 1 && paths.first.startsWith('file ')) {
// The first token of the first path is "file" in case of the save file
// dialog
paths[0] = paths[0].substring(5);
} else if (paths.isNotEmpty && paths.first.startsWith('alias ')) {
// The first token of the first path is "alias" in case of the
// file/directory picker dialog
paths[0] = paths[0].substring(6);
}

// First token is either "alias" or "file" (in case of saveFile dialog)
final volumeName = pathElements[0].split(' ').sublist(1).join(' ');
return paths.map((String path) {
final pathElements = path.split(':').where((e) => e.isNotEmpty).toList();
final volumeName = pathElements[0];
return ['/Volumes', volumeName, ...pathElements.sublist(1)].join('/');
}).toList();
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A package that allows you to use a native file explorer to pick sin
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
repository: https://github.com/miguelpruivo/flutter_file_picker
issue_tracker: https://github.com/miguelpruivo/flutter_file_picker/issues
version: 4.2.4
version: 4.2.5

dependencies:
flutter:
Expand Down
34 changes: 34 additions & 0 deletions test/file_picker_macos_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,40 @@ void main() {
equals('/Volumes/TAILS 4.20 - 202/EFI/debian/grub/x86_64-efi'),
);
});

test(
'should interpret the result of picking filenames that contain blanks and commas',
() {
final picker = FilePickerMacOS();

final filePaths = picker.resultStringToFilePaths(
'alias Macintosh:Users:JohnDoe:test, test.csv, alias macOS Base System:bin:unicorn , generator.sh',
);

expect(filePaths.length, equals(2));
expect(
filePaths[0],
equals('/Volumes/Macintosh/Users/JohnDoe/test, test.csv'),
);
expect(
filePaths[1],
equals('/Volumes/macOS Base System/bin/unicorn , generator.sh'),
);
});

test('should interpret the result of the save file dialog', () {
final picker = FilePickerMacOS();

final filePaths = picker.resultStringToFilePaths(
'file macOS:Users:JohnDoe:Desktop:bill.pdf',
);

expect(filePaths.length, equals(1));
expect(
filePaths[0],
equals('/Volumes/macOS/Users/JohnDoe/Desktop/bill.pdf'),
);
});
});

group('generateCommandLineArguments()', () {
Expand Down

0 comments on commit f72d4b7

Please sign in to comment.