Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Verify extensions are working.
Browse files Browse the repository at this point in the history
  • Loading branch information
tugorez committed Feb 20, 2021
1 parent 3ee4548 commit 0f58fa2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/file_selector/file_selector/lib/file_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Future<XFile> openFile({
String initialDirectory,
String confirmButtonText,
}) {
_verifyExtensions(acceptedTypeGroups);
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFile(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -27,7 +27,7 @@ Future<List<XFile>> openFiles({
String initialDirectory,
String confirmButtonText,
}) {
_verifyExtensions(acceptedTypeGroups);
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFiles(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -41,7 +41,7 @@ Future<String> getSavePath({
String suggestedName,
String confirmButtonText,
}) async {
_verifyExtensions(acceptedTypeGroups);
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.getSavePath(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -58,15 +58,21 @@ Future<String> getDirectoryPath({
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
}

void _verifyExtensions(List<XTypeGroup> acceptedTypeGroups) {
acceptedTypeGroups?.asMap()?.forEach((i, acceptedTypeGroup) {
acceptedTypeGroup.extensions?.asMap()?.forEach((j, ext) {
if (kDebugMode && ext.startsWith('.')) {
print(
'acceptedTypeGroups[${i}].extensions[${j}] with value "${ext}" is invalid.'
' Remove the leading dot.',
);
List<XTypeGroup> _verifyTypeGroups(List<XTypeGroup> groups) {
if (groups == null) return groups;
for (var i = 0; i < groups.length; i++) {
if (groups[i] == null || groups[i].extensions == null) continue;
for (var j = 0; j < groups[i].extensions.length; j++) {
if (groups[i].extensions[j] == null) continue;
if (groups[i].extensions[j].startsWith('.')) {
if (kDebugMode) {
print('acceptedTypeGroups[${i}].extensions[${j}]'
' with value "${groups[i].extensions[j]} is invalid.'
' Please remove the leading dot.');
}
groups[i].extensions[j] = groups[i].extensions[j].substring(1);
}
});
});
}
}
return groups;
}
18 changes: 18 additions & 0 deletions packages/file_selector/file_selector/test/file_selector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ void main() {
final file = await openFile(acceptedTypeGroups: acceptedTypeGroups);
expect(file, expectedFile);
});

test('works with an extension with leading dot', () async {
final correctTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'jpg',
'png',
]),
];
final incorrectTypeGroups = [
XTypeGroup(label: 'images', extensions: [
'.jpg',
'.png',
]),
];

openFile(acceptedTypeGroups: incorrectTypeGroups);
verify(mock.openFile(acceptedTypeGroups: incorrectTypeGroups));
});
});

group('openFiles', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ class XTypeGroup {
'webWildCards': webWildCards,
};
}

bool operator ==(o) =>
label == o.label &&
listEquals(extensions, o.extensions) &&
listEquals(mimeTypes, o.mimeTypes) &&
listEquals(macUTIs, o.macUTIs) &&
listEquals(webWildCards, o.webWildCards);
}

0 comments on commit 0f58fa2

Please sign in to comment.