Skip to content

Instantly share code, notes, and snippets.

@paullryan
Forked from lsauer/gist:5196979
Last active February 18, 2024 10:51
Show Gist options
  • Save paullryan/0587edf73c073d83d176 to your computer and use it in GitHub Desktop.
Save paullryan/0587edf73c073d83d176 to your computer and use it in GitHub Desktop.
MimeTypes es6 Object with kind determination for CMS's
const applicationTypesExt = {
'a' : 'application/octet-stream',
'ai' : 'application/postscript',
'bin' : 'application/octet-stream',
'cdf' : 'application/x-cdf',
'csh' : 'application/x-csh',
'dll' : 'application/octet-stream',
'doc' : 'application/msword',
'dot' : 'application/msword',
'dvi' : 'application/x-dvi',
'eps' : 'application/postscript',
'exe' : 'application/octet-stream',
'gtar' : 'application/x-gtar',
'hdf' : 'application/x-hdf',
'js' : 'application/x-javascript',
'latex' : 'application/x-latex',
'man' : 'application/x-troff-man',
'me' : 'application/x-troff-me',
'mif' : 'application/x-mif',
'ms' : 'application/x-troff-ms',
'nc' : 'application/x-netcdf',
'o' : 'application/octet-stream',
'obj' : 'application/octet-stream',
'oda' : 'application/oda',
'pdf' : 'application/pdf',
'pfx' : 'application/x-pkcs12',
'pot' : 'application/vnd.ms-powerpoint',
'ppa' : 'application/vnd.ms-powerpoint',
'pps' : 'application/vnd.ms-powerpoint',
'ppt' : 'application/vnd.ms-powerpoint',
'pptx' : 'application/vnd.ms-powerpoint',
'ps' : 'application/postscript',
'pwz' : 'application/vnd.ms-powerpoint',
'pyc' : 'application/x-python-code',
'pyo' : 'application/x-python-code',
'ram' : 'application/x-pn-realaudio',
'roff' : 'application/x-troff',
'so' : 'application/octet-stream',
'src' : 'application/x-wais-source',
'swf' : 'application/x-shockwave-flash',
't' : 'application/x-troff',
'tar' : 'application/x-tar',
'tcl' : 'application/x-tcl',
'tex' : 'application/x-tex',
'texi' : 'application/x-texinfo',
'texinfo': 'application/x-texinfo',
'rdf' : 'application/xml',
'sh' : 'application/x-sh',
'shar' : 'application/x-shar',
'tr' : 'application/x-troff',
'ustar' : 'application/x-ustar',
'wiz' : 'application/msword',
'wsdl' : 'application/xml',
'xlb' : 'application/vnd.ms-excel',
'xls' : 'application/vnd.ms-excel',
'xlsx' : 'application/vnd.ms-excel',
'xpdl' : 'application/xml',
'xsl' : 'application/xml',
'zip' : 'application/zip',
};
const applicationTypes = makeUniqueArrayOfValues(applicationTypesExt);
const audioTypesExt = {
'aif' : 'audio/x-aiff',
'aifc' : 'audio/x-aiff',
'aiff' : 'audio/x-aiff',
'au' : 'audio/basic',
'mp2' : 'audio/mpeg',
'mp3' : 'audio/mpeg',
'ra' : 'audio/x-pn-realaudio',
'snd' : 'audio/basic',
'wav' : 'audio/x-wav',
};
const audioTypes = makeUniqueArrayOfValues(audioTypesExt);
const videoTypesExt = {
'avi' : 'video/x-msvideo',
'm1v' : 'video/mpeg',
'mov' : 'video/quicktime',
'movie' : 'video/x-sgi-movie',
'mp4' : 'video/mp4',
'mpa' : 'video/mpeg',
'mpe' : 'video/mpeg',
'mpeg' : 'video/mpeg',
'mpg' : 'video/mpeg',
'qt' : 'video/quicktime',
};
const videoTypes = makeUniqueArrayOfValues(videoTypesExt);
const textTypesExt = {
'bat' : 'text/plain',
'c' : 'text/plain',
'css' : 'text/css',
'etx' : 'text/x-setext',
'h' : 'text/plain',
'htm' : 'text/html',
'html' : 'text/html',
'ksh' : 'text/plain',
'py' : 'text/x-python',
'rtx' : 'text/richtext',
'sgm' : 'text/x-sgml',
'sgml' : 'text/x-sgml',
'tsv' : 'text/tab-separated-values',
'txt' : 'text/plain',
'vcf' : 'text/x-vcard',
'xml' : 'text/xml',
};
const textTypes = makeUniqueArrayOfValues(textTypesExt);
const imageTypesExt = {
'bmp' : 'image/x-ms-bmp',
'gif' : 'image/gif',
'jpe' : 'image/jpeg',
'jpeg' : 'image/jpeg',
'jpg' : 'image/jpeg',
'pbm' : 'image/x-portable-bitmap',
'pgm' : 'image/x-portable-graymap',
'png' : 'image/png',
'pnm' : 'image/x-portable-anymap',
'ppm' : 'image/x-portable-pixmap',
'ras' : 'image/x-cmu-raster',
'rgb' : 'image/x-rgb',
'tif' : 'image/tiff',
'tiff' : 'image/tiff',
'xbm' : 'image/x-xbitmap',
'xpm' : 'image/x-xpixmap',
'xwd' : 'image/x-xwindowdump',
};
const imageTypes = makeUniqueArrayOfValues(imageTypesExt);
const messageTypesExt = {
'eml' : 'message/rfc822',
'mht' : 'message/rfc822',
'mhtml' : 'message/rfc822',
'nws' : 'message/rfc822',
};
const messageTypes = makeUniqueArrayOfValues(messageTypesExt);
function concatObjects(objects){
let finalObj = {};
objects.forEach(function(obj){
for(let key in obj){
finalObj[key] = obj[key];
}
});
return finalObj;
}
function makeUniqueArrayOfValues(obj){
let unique = [];
for(let key in obj){
if(unique.indexOf(obj[key]) === -1){
unique.push(obj[key]);
}
}
return unique;
}
const extensions = {
application: applicationTypesExt,
audio: audioTypesExt,
video: videoTypesExt,
text: textTypesExt,
image: imageTypesExt,
message: messageTypesExt
};
const kinds = {
application: applicationTypes,
audio: audioTypes,
video: videoTypes,
text: textTypes,
image: imageTypes,
message: messageTypes
};
const fullExtension = concatObjects([applicationTypesExt, audioTypesExt, videoTypesExt, textTypesExt, imageTypesExt, messageTypesExt])
class MimeTypes {
static get extensions(){
return extensions;
}
static get kinds() {
return kinds;
}
static get fullExtension() {
return fullExtension;
}
static isKind(kind, mimeType){
return MimeTypes.kinds[kind].indexOf(mimeType) > -1;
}
static getMimeType(extension){
return MimeTypes.fullExtension[extension];
}
static getKind(mimeType) {
for(let key in MimeTypes.kinds){
if(MimeTypes.isKind(key, mimeType)){
return key;
}
}
}
}
export default MimeTypes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment