parse static method
Constructor that parses a mimeType
string.
defaultMimeType
if mimeType
is invalid.
Implementation
static MimeType? parse(String? mimeType, [String? defaultMimeType]) {
if (mimeType != null) {
mimeType = mimeType.trim();
if (mimeType.isEmpty) {
if (defaultMimeType != null) {
mimeType = defaultMimeType.trim();
if (mimeType.isEmpty) return null;
} else {
return null;
}
}
} else if (defaultMimeType != null) {
mimeType = defaultMimeType.trim();
if (mimeType.isEmpty) return null;
} else {
return null;
}
assert(mimeType.isNotEmpty && mimeType == mimeType.trim());
mimeType = mimeType.toLowerCase();
var parts = split(mimeType, ';', 2);
mimeType = parts[0];
String? charset = (parts.length == 2 ? parts[1] : '').trim();
charset = normalizeCharset(charset);
if (mimeType == 'json' || mimeType.endsWith('/json')) {
return MimeType('application', 'json', charset);
} else if (mimeType == 'javascript' ||
mimeType == 'js' ||
mimeType.endsWith('/javascript') ||
mimeType.endsWith('/js')) {
return MimeType('application', 'javascript', charset);
} else if (mimeType == 'jpeg' ||
mimeType == 'jpg' ||
mimeType.endsWith('/jpeg') ||
mimeType.endsWith('/jpg')) {
return MimeType('image', 'jpeg');
} else if (mimeType == 'png' || mimeType.endsWith('/png')) {
return MimeType('image', 'png', charset);
} else if (mimeType == 'svg' ||
mimeType.endsWith('/svg') ||
mimeType.endsWith('/svg+xml')) {
return MimeType('image', 'svg+xml', charset);
} else if (mimeType == 'text' || mimeType == 'txt') {
return MimeType('text', 'plain');
} else if (mimeType == 'css' || mimeType.endsWith('/css')) {
return MimeType('text', 'css', charset);
} else if (mimeType == 'html' ||
mimeType == 'htm' ||
mimeType.endsWith('/html') ||
mimeType.endsWith('/htm')) {
return MimeType('text', 'html', charset);
} else if (mimeType == 'icon' ||
mimeType == 'ico' ||
mimeType.endsWith('/x-icon') ||
mimeType.endsWith('/icon')) {
return MimeType('image', 'x-icon', charset);
} else if (mimeType == 'gif' || mimeType.endsWith('/gif')) {
return MimeType('image', 'gif', charset);
} else if (mimeType == 'otf' || mimeType.endsWith('/otf')) {
return MimeType('font', 'otf', charset);
} else if (mimeType == 'ttf' || mimeType.endsWith('/ttf')) {
return MimeType('font', 'ttf', charset);
} else if (mimeType == 'woff' || mimeType.endsWith('/woff')) {
return MimeType('font', 'woff', charset);
} else if (mimeType == 'woff2' || mimeType.endsWith('/woff2')) {
return MimeType('font', 'woff2', charset);
} else if (mimeType == 'webp' || mimeType.endsWith('/webp')) {
return MimeType('image', 'webp', charset);
} else if (mimeType == 'weba' ||
mimeType == 'audio/webm' ||
mimeType == 'audio/weba') {
return MimeType('audio', 'webm', charset);
} else if (mimeType == 'webm' || mimeType == 'video/webm') {
return MimeType('video', 'webm', charset);
} else if (mimeType == 'yaml' ||
mimeType == 'yml' ||
mimeType.endsWith('/yaml')) {
return MimeType('application', 'yaml', charset);
} else if (mimeType == 'xml' || mimeType.endsWith('/xml')) {
return MimeType('text', 'xml', charset);
} else if (mimeType == 'zip' || mimeType.endsWith('/zip')) {
return MimeType('application', 'zip', charset);
} else if (mimeType == 'gzip' ||
mimeType == 'gz' ||
mimeType.endsWith('/gzip')) {
return MimeType('application', 'gzip', charset);
} else if (mimeType == 'pdf' || mimeType.endsWith('/pdf')) {
return MimeType('application', 'pdf', charset);
} else if (mimeType == 'mp3' ||
mimeType.endsWith('/mp3') ||
mimeType.endsWith('audio/mpeg')) {
return MimeType('audio', 'mp3', charset);
} else if (mimeType == 'mpeg' || mimeType.endsWith('video/mpeg')) {
return MimeType('video', 'mpeg', charset);
} else if (mimeType == 'xhtml' ||
mimeType.endsWith('/xhtml') ||
mimeType.endsWith('/xhtml+xml')) {
return MimeType('application', 'xhtml');
} else if (mimeType == 'markdown' ||
mimeType == 'md' ||
mimeType.endsWith('/markdown')) {
return MimeType('text', 'markdown', charset);
} else if (mimeType == 'dart' || mimeType.endsWith('/dart')) {
return MimeType('application', 'dart', charset);
}
var idx = mimeType.indexOf('/');
if (idx > 0) {
var type = mimeType.substring(0, idx).trim();
var subType = mimeType.substring(idx + 1).trim();
if (type.isNotEmpty && subType.isNotEmpty) {
return MimeType(type, subType, charset);
} else {
throw ArgumentError('Invalid MimeType: $mimeType');
}
}
return MimeType('application', mimeType, charset);
}