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

Commit e9ae0bd

Browse files
committed
Remove leading dot and warn users about it
1 parent 0f58fa2 commit e9ae0bd

File tree

5 files changed

+33
-56
lines changed

5 files changed

+33
-56
lines changed

packages/file_selector/file_selector/lib/file_selector.dart

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'package:flutter/foundation.dart';
6+
77
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
8+
89
export 'package:file_selector_platform_interface/file_selector_platform_interface.dart'
910
show XFile, XTypeGroup;
1011

@@ -14,7 +15,6 @@ Future<XFile> openFile({
1415
String initialDirectory,
1516
String confirmButtonText,
1617
}) {
17-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
1818
return FileSelectorPlatform.instance.openFile(
1919
acceptedTypeGroups: acceptedTypeGroups,
2020
initialDirectory: initialDirectory,
@@ -27,7 +27,6 @@ Future<List<XFile>> openFiles({
2727
String initialDirectory,
2828
String confirmButtonText,
2929
}) {
30-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
3130
return FileSelectorPlatform.instance.openFiles(
3231
acceptedTypeGroups: acceptedTypeGroups,
3332
initialDirectory: initialDirectory,
@@ -41,7 +40,6 @@ Future<String> getSavePath({
4140
String suggestedName,
4241
String confirmButtonText,
4342
}) async {
44-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
4543
return FileSelectorPlatform.instance.getSavePath(
4644
acceptedTypeGroups: acceptedTypeGroups,
4745
initialDirectory: initialDirectory,
@@ -57,22 +55,3 @@ Future<String> getDirectoryPath({
5755
return FileSelectorPlatform.instance.getDirectoryPath(
5856
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
5957
}
60-
61-
List<XTypeGroup> _verifyTypeGroups(List<XTypeGroup> groups) {
62-
if (groups == null) return groups;
63-
for (var i = 0; i < groups.length; i++) {
64-
if (groups[i] == null || groups[i].extensions == null) continue;
65-
for (var j = 0; j < groups[i].extensions.length; j++) {
66-
if (groups[i].extensions[j] == null) continue;
67-
if (groups[i].extensions[j].startsWith('.')) {
68-
if (kDebugMode) {
69-
print('acceptedTypeGroups[${i}].extensions[${j}]'
70-
' with value "${groups[i].extensions[j]} is invalid.'
71-
' Please remove the leading dot.');
72-
}
73-
groups[i].extensions[j] = groups[i].extensions[j].substring(1);
74-
}
75-
}
76-
}
77-
return groups;
78-
}

packages/file_selector/file_selector/test/file_selector_test.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,6 @@ void main() {
7979
final file = await openFile(acceptedTypeGroups: acceptedTypeGroups);
8080
expect(file, expectedFile);
8181
});
82-
83-
test('works with an extension with leading dot', () async {
84-
final correctTypeGroups = [
85-
XTypeGroup(label: 'images', extensions: [
86-
'jpg',
87-
'png',
88-
]),
89-
];
90-
final incorrectTypeGroups = [
91-
XTypeGroup(label: 'images', extensions: [
92-
'.jpg',
93-
'.png',
94-
]),
95-
];
96-
97-
openFile(acceptedTypeGroups: incorrectTypeGroups);
98-
verify(mock.openFile(acceptedTypeGroups: incorrectTypeGroups));
99-
});
10082
});
10183

10284
group('openFiles', () {

packages/file_selector/file_selector_platform_interface/lib/src/types/x_type_group/x_type_group.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2020 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4+
import 'package:flutter/foundation.dart';
45

56
/// A set of allowed XTypes
67
class XTypeGroup {
@@ -14,7 +15,22 @@ class XTypeGroup {
1415
this.mimeTypes,
1516
this.macUTIs,
1617
this.webWildCards,
17-
});
18+
}) {
19+
_verifyExtensions();
20+
}
21+
22+
void _verifyExtensions() {
23+
if (extensions == null) return;
24+
for (var i = 0; i < extensions!.length; i++) {
25+
if (!extensions![i].startsWith('.')) continue;
26+
if (kDebugMode) {
27+
print('extensions[${i}] with value "${extensions![i]}" is invalid.'
28+
' We are mutating the extensions list to remove the leading dot.'
29+
' Please fix it.');
30+
}
31+
extensions![i] = extensions![i].substring(1);
32+
}
33+
}
1834

1935
/// The 'name' or reference to this group of types
2036
final String? label;
@@ -41,11 +57,4 @@ class XTypeGroup {
4157
'webWildCards': webWildCards,
4258
};
4359
}
44-
45-
bool operator ==(o) =>
46-
label == o.label &&
47-
listEquals(extensions, o.extensions) &&
48-
listEquals(mimeTypes, o.mimeTypes) &&
49-
listEquals(macUTIs, o.macUTIs) &&
50-
listEquals(webWildCards, o.webWildCards);
5160
}

packages/file_selector/file_selector_platform_interface/test/method_channel_file_selector_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ void main() {
2929
test('passes the accepted type groups correctly', () async {
3030
final group = XTypeGroup(
3131
label: 'text',
32-
extensions: ['.txt'],
32+
extensions: ['txt'],
3333
mimeTypes: ['text/plain'],
3434
macUTIs: ['public.text'],
3535
);
3636

3737
final groupTwo = XTypeGroup(
3838
label: 'image',
39-
extensions: ['.jpg'],
39+
extensions: ['jpg'],
4040
mimeTypes: ['image/jpg'],
4141
macUTIs: ['public.image'],
4242
webWildCards: ['image/*']);
@@ -90,14 +90,14 @@ void main() {
9090
test('passes the accepted type groups correctly', () async {
9191
final group = XTypeGroup(
9292
label: 'text',
93-
extensions: ['.txt'],
93+
extensions: ['txt'],
9494
mimeTypes: ['text/plain'],
9595
macUTIs: ['public.text'],
9696
);
9797

9898
final groupTwo = XTypeGroup(
9999
label: 'image',
100-
extensions: ['.jpg'],
100+
extensions: ['jpg'],
101101
mimeTypes: ['image/jpg'],
102102
macUTIs: ['public.image'],
103103
webWildCards: ['image/*']);
@@ -152,14 +152,14 @@ void main() {
152152
test('passes the accepted type groups correctly', () async {
153153
final group = XTypeGroup(
154154
label: 'text',
155-
extensions: ['.txt'],
155+
extensions: ['txt'],
156156
mimeTypes: ['text/plain'],
157157
macUTIs: ['public.text'],
158158
);
159159

160160
final groupTwo = XTypeGroup(
161161
label: 'image',
162-
extensions: ['.jpg'],
162+
extensions: ['jpg'],
163163
mimeTypes: ['image/jpg'],
164164
macUTIs: ['public.image'],
165165
webWildCards: ['image/*']);

packages/file_selector/file_selector_platform_interface/test/x_type_group_test.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void main() {
99
group('XTypeGroup', () {
1010
test('toJSON() creates correct map', () {
1111
final label = 'test group';
12-
final extensions = ['.txt', '.jpg'];
12+
final extensions = ['txt', 'jpg'];
1313
final mimeTypes = ['text/plain'];
1414
final macUTIs = ['public.plain-text'];
1515
final webWildCards = ['image/*'];
@@ -41,5 +41,12 @@ void main() {
4141
expect(jsonMap['macUTIs'], null);
4242
expect(jsonMap['webWildCards'], null);
4343
});
44+
45+
test('Validates extensions have not leading dots', () {
46+
final extensions = ['.txt', '.jpg'];
47+
final group = XTypeGroup(extensions: extensions);
48+
49+
expect(group.extensions, ['txt', 'jpg']);
50+
});
4451
});
4552
}

0 commit comments

Comments
 (0)