Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE-5431: fix(mimetype): set file path to lower case before verifying the mime type #23

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
10 changes: 7 additions & 3 deletions lib/src/utils/mime_type_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ String? lookupMimeType(String path, {List<int>? headerBytes}) {
return mime.lookupMimeType(path, headerBytes: headerBytes);
}

String lookupMimeTypeWithDefaultType(String path, {List<int>? headerBytes}) =>
lookupMimeType(path, headerBytes: headerBytes) ??
'application/octet-stream';
String lookupMimeTypeWithDefaultType(String path, {List<int>? headerBytes}) {
path = path.toLowerCase();

return lookupMimeType(path, headerBytes: headerBytes) ?? octetStream;
}

/// This implementation is specific for `file_saver` package.
MimeType getMimeTypeFromString(String mimeType) {
Expand Down Expand Up @@ -76,3 +78,5 @@ MimeType getMimeTypeFromString(String mimeType) {
return MimeType.OTHER;
}
}

const String octetStream = 'application/octet-stream';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ardrive_io
description: A new Flutter package project.
version: 1.4.4
version: 1.4.5
homepage:
publish_to: none

Expand Down
27 changes: 24 additions & 3 deletions test/src/utils/mime_type_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ void main() {
'path/to/my/non tar/file.iso': 'application/x-iso9660-image',
'ardrive.png': 'image/png',
'.tar.gz': 'application/gzip',
'my.non.tar.gz.file': 'application/octet-stream',
'where\'s waldo.tar.gz.': 'application/octet-stream',
'my.non.tar.gz.file': octetStream,
'where\'s waldo.tar.gz.': octetStream,
};

const pathToMimeMappingUpperCase = <String, String?>{
'.my.file.TAR.GZ': 'application/gzip',
'abcdefghijklmnñopqrstuvwxyz.tar.GZ': 'application/gzip',
'path/to/my/file.TAR.GZ': 'application/gzip',
'path/to/my/file.GZ': 'application/gzip',
'path/to/my/file.tGZ': 'application/gzip',
'path/to/my/non tar/file.ISO': 'application/x-iso9660-image',
'ardrive.PNG': 'image/png',
'.tar.GZ': 'application/gzip',
'my.non.tar.GZ.file': octetStream,
'where\'s waldo.TAR.gz.': octetStream,
};

test('returns the expected mime type', () {
Expand All @@ -47,12 +60,20 @@ void main() {
}
});

test('returns the expected mime type when written in uppercase', () {
for (final path in pathToMimeMappingUpperCase.keys) {
final expectedMime = pathToMimeMappingUpperCase[path];
final mime = lookupMimeTypeWithDefaultType(path);
expect(mime, expectedMime);
}
});

test(
'returns the application/octet-stream type when not provided a mime type',
() {
final mime =
lookupMimeTypeWithDefaultType('path/some_file_without_extension');
expect(mime, 'application/octet-stream');
expect(mime, octetStream);
});
});
}
5 changes: 2 additions & 3 deletions test/src/utils/path_utils_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:ardrive_io/src/io_exception.dart';
import 'package:ardrive_io/src/utils/path_utils.dart';
import 'package:ardrive_io/ardrive_io.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Expand Down Expand Up @@ -76,7 +75,7 @@ void main() {
test('should return the extension from mimetype name without .', () {
final ext = getFileExtension(
name: 'file',
contentType: 'application/octet-stream',
contentType: octetStream,
withExtensionDot: false,
);

Expand Down