-
Notifications
You must be signed in to change notification settings - Fork 78
[pkg-mime] contenttype to ext #431
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
Changes from 30 commits
9355522
e7f328e
7eb72c3
80079be
d3ce7ae
29050ff
84f3bd8
c27fa37
f2a62f7
bdb76d3
16f54d5
dccafc0
59c6ec2
9e4b9d0
bc90ebd
cd5e501
557520e
8c1f0a0
0701165
7ad3723
58ad484
eed2ba9
dc5db0b
d285aae
342b63f
40cd80c
60730a6
04f8169
8ba5efb
c1d7b69
e654a32
c236bd8
b11854c
874f740
d5e3d47
7b3193f
c33a7ea
b5e6981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'default_extension_map.dart'; | ||
|
|
||
| /// Reverse map of [defaultExtensionMap] with overrides for common extensions | ||
| /// since different extensions may map to the same MIME type. | ||
|
||
| final Map<String, String> _defaultMimeTypeMap = { | ||
| for (var entry in defaultExtensionMap.entries) entry.value: entry.key, | ||
| }..addAll({ | ||
|
||
| 'application/vnd.ms-excel': 'xls', | ||
| 'application/vnd.ms-powerpoint': 'ppt', | ||
| 'image/jpeg': 'jpg', | ||
| 'image/tiff': 'tif', | ||
| 'image/svg+xml': 'svg', | ||
| 'text/calendar': 'ics', | ||
| 'text/javascript': 'js', | ||
| 'text/plain': 'txt', | ||
| 'text/sgml': 'sgml', | ||
| 'text/x-pascal': 'pas', | ||
| 'video/mp4': 'mp4', | ||
| 'video/mpeg': 'mpg', | ||
| 'video/quicktime': 'mov', | ||
| 'video/x-matroska': 'mkv', | ||
| }); | ||
|
|
||
| /// The file extension for a given MIME type. | ||
|
||
| /// | ||
| /// If there are multiple extensions for [mimeType], return preferred extension | ||
|
||
| /// if defined in [_defaultMimeTypeMap], otherwise an extension chosen by the | ||
| /// library. | ||
| /// | ||
| /// If no extension is found, `null` is returned. | ||
| String? extensionFromMime(String mimeType) => | ||
| _defaultMimeTypeMap[mimeType.toLowerCase()]; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
| // for details. 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:mime/mime.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('global-lookup-mime-type', () { | ||
|
||
| test('valid-mime-type', () { | ||
| expect(extensionFromMime('text/x-dart'), equals('dart')); | ||
| expect(extensionFromMime('text/javascript'), equals('js')); | ||
| expect(extensionFromMime('application/java-archive'), equals('jar')); | ||
| expect(extensionFromMime('application/json'), equals('json')); | ||
| expect(extensionFromMime('application/pdf'), equals('pdf')); | ||
| expect(extensionFromMime('application/vnd.ms-excel'), equals('xls')); | ||
| expect(extensionFromMime('application/xhtml+xml'), equals('xht')); | ||
| expect(extensionFromMime('image/jpeg'), equals('jpg')); | ||
| expect(extensionFromMime('image/png'), equals('png')); | ||
| expect(extensionFromMime('text/css'), equals('css')); | ||
| expect(extensionFromMime('text/html'), equals('htm')); | ||
| expect(extensionFromMime('text/plain'), equals('txt')); | ||
| expect(extensionFromMime('text/x-c'), equals('c')); | ||
| }); | ||
|
|
||
| test('invalid-mime-type', () { | ||
| expect(extensionFromMime('invalid-mime-type'), isNull); | ||
| expect(extensionFromMime('invalid/mime/type'), isNull); | ||
| }); | ||
|
|
||
| test('unknown-mime-type', () { | ||
| expect(extensionFromMime('application/to-be-invented'), isNull); | ||
| }); | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conside formatting the comment as:
..)); // Prints "html".(Dittos below.)
Be direct in comments. It saves words and usually avoids ambiguity. Present tense verbs win!
Distinguish different kinds of text. Here "html" is an actual string value, but is not distinguished from the other words.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done