-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #431 from mx1up/issue13_contenttype_to_ext
[pkg-mime] contenttype to ext
- Loading branch information
Showing
9 changed files
with
110 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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'; | ||
|
||
/// Default extension for recognized MIME types. | ||
/// | ||
/// Is the inverse of [defaultExtensionMap], and where that | ||
/// map has multiple extensions which map to the same | ||
/// MIME type, this map maps that MIME type to a *default* | ||
/// extension. | ||
/// | ||
/// Used by [extensionFromMime]. | ||
final Map<String, String> _defaultMimeTypeMap = { | ||
for (var entry in defaultExtensionMap.entries) entry.value: entry.key, | ||
'application/msword': 'doc', | ||
'application/vnd.ms-excel': 'xls', | ||
'application/vnd.ms-powerpoint': 'ppt', | ||
'application/x-debian-package': 'deb', | ||
'application/xhtml+xml': 'xhtml', | ||
'application/xml': 'xml', | ||
'audio/x-aiff': 'aif', | ||
'audio/midi': 'mid', | ||
'audio/mp4': 'm4a', | ||
'audio/ogg': 'ogg', | ||
'image/jpeg': 'jpg', | ||
'image/tiff': 'tif', | ||
'image/svg+xml': 'svg', | ||
'model/vrml': 'vrml', | ||
'text/calendar': 'ics', | ||
'text/html': 'html', | ||
'text/javascript': 'js', | ||
'text/markdown': 'md', | ||
'text/plain': 'txt', | ||
'text/sgml': 'sgml', | ||
'text/x-asm': 'asm', | ||
'text/x-c': 'c', | ||
'text/x-pascal': 'pas', | ||
'video/mp4': 'mp4', | ||
'video/mpeg': 'mpg', | ||
'video/quicktime': 'mov', | ||
'video/x-matroska': 'mkv', | ||
}; | ||
|
||
/// The default file extension for a given MIME type. | ||
/// | ||
/// If [mimeType] has multiple associated extensions, | ||
/// the returned string is one of those, chosen as the default | ||
/// extension for that MIME type. | ||
/// | ||
/// Returns `null` if [mimeType] is not a recognized and | ||
/// supported MIME type. | ||
String? extensionFromMime(String mimeType) => | ||
_defaultMimeTypeMap[mimeType.toLowerCase()]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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() { | ||
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('xhtml')); | ||
expect(extensionFromMime('image/jpeg'), equals('jpg')); | ||
expect(extensionFromMime('image/png'), equals('png')); | ||
expect(extensionFromMime('text/css'), equals('css')); | ||
expect(extensionFromMime('text/html'), equals('html')); | ||
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters