Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove leading dot and warn users about it
  • Loading branch information
tugorez committed Feb 26, 2021
commit e956b0672f398ce763e826520c23d49e9f90ea3d
25 changes: 2 additions & 23 deletions packages/file_selector/file_selector/lib/file_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/foundation.dart';

import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';

export 'package:file_selector_platform_interface/file_selector_platform_interface.dart'
show XFile, XTypeGroup;

Expand All @@ -14,7 +15,6 @@ Future<XFile?> openFile({
String? initialDirectory,
String? confirmButtonText,
}) {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFile(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -27,7 +27,6 @@ Future<List<XFile>> openFiles({
String? initialDirectory,
String? confirmButtonText,
}) {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.openFiles(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -41,7 +40,6 @@ Future<String?> getSavePath({
String? suggestedName,
String? confirmButtonText,
}) async {
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
return FileSelectorPlatform.instance.getSavePath(
acceptedTypeGroups: acceptedTypeGroups,
initialDirectory: initialDirectory,
Expand All @@ -57,22 +55,3 @@ Future<String?> getDirectoryPath({
return FileSelectorPlatform.instance.getDirectoryPath(
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
}

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: 0 additions & 18 deletions packages/file_selector/file_selector/test/file_selector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ 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
@@ -1,6 +1,7 @@
// Copyright 2020 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';

/// A set of allowed XTypes
class XTypeGroup {
Expand All @@ -14,7 +15,22 @@ class XTypeGroup {
this.mimeTypes,
this.macUTIs,
this.webWildCards,
});
}) {
_verifyExtensions();
}

void _verifyExtensions() {
if (extensions == null) return;
for (var i = 0; i < extensions!.length; i++) {
if (!extensions![i].startsWith('.')) continue;
if (kDebugMode) {
print('extensions[${i}] with value "${extensions![i]}" is invalid.'
' We are mutating the extensions list to remove the leading dot.'
' Please fix it.');
}
extensions![i] = extensions![i].substring(1);
}
}

/// The 'name' or reference to this group of types
final String? label;
Expand All @@ -41,11 +57,4 @@ 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ void main() {
test('passes the accepted type groups correctly', () async {
final group = XTypeGroup(
label: 'text',
extensions: ['.txt'],
extensions: ['txt'],
mimeTypes: ['text/plain'],
macUTIs: ['public.text'],
);

final groupTwo = XTypeGroup(
label: 'image',
extensions: ['.jpg'],
extensions: ['jpg'],
mimeTypes: ['image/jpg'],
macUTIs: ['public.image'],
webWildCards: ['image/*']);
Expand Down Expand Up @@ -90,14 +90,14 @@ void main() {
test('passes the accepted type groups correctly', () async {
final group = XTypeGroup(
label: 'text',
extensions: ['.txt'],
extensions: ['txt'],
mimeTypes: ['text/plain'],
macUTIs: ['public.text'],
);

final groupTwo = XTypeGroup(
label: 'image',
extensions: ['.jpg'],
extensions: ['jpg'],
mimeTypes: ['image/jpg'],
macUTIs: ['public.image'],
webWildCards: ['image/*']);
Expand Down Expand Up @@ -152,14 +152,14 @@ void main() {
test('passes the accepted type groups correctly', () async {
final group = XTypeGroup(
label: 'text',
extensions: ['.txt'],
extensions: ['txt'],
mimeTypes: ['text/plain'],
macUTIs: ['public.text'],
);

final groupTwo = XTypeGroup(
label: 'image',
extensions: ['.jpg'],
extensions: ['jpg'],
mimeTypes: ['image/jpg'],
macUTIs: ['public.image'],
webWildCards: ['image/*']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void main() {
group('XTypeGroup', () {
test('toJSON() creates correct map', () {
final label = 'test group';
final extensions = ['.txt', '.jpg'];
final extensions = ['txt', 'jpg'];
final mimeTypes = ['text/plain'];
final macUTIs = ['public.plain-text'];
final webWildCards = ['image/*'];
Expand Down Expand Up @@ -41,5 +41,12 @@ void main() {
expect(jsonMap['macUTIs'], null);
expect(jsonMap['webWildCards'], null);
});

test('Validates extensions have not leading dots', () {
final extensions = ['.txt', '.jpg'];
final group = XTypeGroup(extensions: extensions);

expect(group.extensions, ['txt', 'jpg']);
});
});
}