OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library mime.mime_type; | 5 library mime.mime_type; |
6 | 6 |
7 import 'default_extension_map.dart'; | 7 import 'default_extension_map.dart'; |
8 import 'magic_number.dart'; | 8 import 'magic_number.dart'; |
9 | 9 |
10 final MimeTypeResolver _globalResolver = new MimeTypeResolver(); | 10 final MimeTypeResolver _globalResolver = new MimeTypeResolver(); |
11 | 11 |
12 /** | 12 /** |
13 * The maximum number of bytes needed, to match all default magic-numbers. | 13 * The maximum number of bytes needed, to match all default magic-numbers. |
14 */ | 14 */ |
15 // NOTE: this is not formatted AS_A_CONST to avoid a breaking change | 15 int get defaultMagicNumbersMaxLength => _globalResolver.magicNumbersMaxLength; |
16 const int defaultMagicNumbersMaxLength = 12; | |
17 | 16 |
18 /** | 17 /** |
19 * Extract the extension from [path] and use that for MIME-type lookup, using | 18 * Extract the extension from [path] and use that for MIME-type lookup, using |
20 * the default extension map. | 19 * the default extension map. |
21 * | 20 * |
22 * If no matching MIME-type was found, `null` is returned. | 21 * If no matching MIME-type was found, `null` is returned. |
23 * | 22 * |
24 * If [headerBytes] is present, a match for known magic-numbers will be | 23 * If [headerBytes] is present, a match for known magic-numbers will be |
25 * performed first. This allows the correct mime-type to be found, even though | 24 * performed first. This allows the correct mime-type to be found, even though |
26 * a file have been saved using the wrong file-name extension. If less than | 25 * a file have been saved using the wrong file-name extension. If less than |
(...skipping 15 matching lines...) Expand all Loading... |
42 /** | 41 /** |
43 * Create a new empty [MimeTypeResolver]. | 42 * Create a new empty [MimeTypeResolver]. |
44 */ | 43 */ |
45 MimeTypeResolver.empty() : _useDefault = false, _magicNumbersMaxLength = 0; | 44 MimeTypeResolver.empty() : _useDefault = false, _magicNumbersMaxLength = 0; |
46 | 45 |
47 /** | 46 /** |
48 * Create a new [MimeTypeResolver] containing the default scope. | 47 * Create a new [MimeTypeResolver] containing the default scope. |
49 */ | 48 */ |
50 MimeTypeResolver() : | 49 MimeTypeResolver() : |
51 _useDefault = true, | 50 _useDefault = true, |
52 _magicNumbersMaxLength = defaultMagicNumbersMaxLength; | 51 _magicNumbersMaxLength = DEFAULT_MAGIC_NUMBERS_MAX_LENGTH; |
53 | 52 |
54 /** | 53 /** |
55 * Get the maximum number of bytes required to match all magic numbers, when | 54 * Get the maximum number of bytes required to match all magic numbers, when |
56 * performing [lookup] with headerBytes present. | 55 * performing [lookup] with headerBytes present. |
57 */ | 56 */ |
58 int get magicNumbersMaxLength => _magicNumbersMaxLength; | 57 int get magicNumbersMaxLength => _magicNumbersMaxLength; |
59 | 58 |
60 /** | 59 /** |
61 * Extract the extension from [path] and use that for MIME-type lookup. | 60 * Extract the extension from [path] and use that for MIME-type lookup. |
62 * | 61 * |
(...skipping 12 matching lines...) Expand all Loading... |
75 if (result != null) return result; | 74 if (result != null) return result; |
76 if (_useDefault) { | 75 if (_useDefault) { |
77 result = _matchMagic(headerBytes, DEFAULT_MAGIC_NUMBERS); | 76 result = _matchMagic(headerBytes, DEFAULT_MAGIC_NUMBERS); |
78 if (result != null) return result; | 77 if (result != null) return result; |
79 } | 78 } |
80 } | 79 } |
81 var ext = _ext(path); | 80 var ext = _ext(path); |
82 result = _extensionMap[ext]; | 81 result = _extensionMap[ext]; |
83 if (result != null) return result; | 82 if (result != null) return result; |
84 if (_useDefault) { | 83 if (_useDefault) { |
85 result = DEFAULT_EXTENSION_MAP[ext]; | 84 result = defaultExtensionMap[ext]; |
86 if (result != null) return result; | 85 if (result != null) return result; |
87 } | 86 } |
88 return null; | 87 return null; |
89 } | 88 } |
90 | 89 |
91 /** | 90 /** |
92 * Add a new MIME-type mapping to the [MimeTypeResolver]. If the [extension] | 91 * Add a new MIME-type mapping to the [MimeTypeResolver]. If the [extension] |
93 * is already present in the [MimeTypeResolver], it'll be overwritten. | 92 * is already present in the [MimeTypeResolver], it'll be overwritten. |
94 */ | 93 */ |
95 void addExtension(String extension, String mimeType) { | 94 void addExtension(String extension, String mimeType) { |
(...skipping 24 matching lines...) Expand all Loading... |
120 return null; | 119 return null; |
121 } | 120 } |
122 | 121 |
123 static String _ext(String path) { | 122 static String _ext(String path) { |
124 int index = path.lastIndexOf('.'); | 123 int index = path.lastIndexOf('.'); |
125 if (index < 0 || index + 1 >= path.length) return path; | 124 if (index < 0 || index + 1 >= path.length) return path; |
126 return path.substring(index + 1).toLowerCase(); | 125 return path.substring(index + 1).toLowerCase(); |
127 } | 126 } |
128 } | 127 } |
129 | 128 |
OLD | NEW |