smartmime/ts/index.ts
2020-03-04 15:47:15 +00:00

38 lines
758 B
TypeScript

import * as plugins from './smartmime.plugins';
export const binaryFileTypes = [
'jpg',
'png',
'pdf'
];
export const supportedFileTypes = [
'json',
'html',
'svg',
'jpg',
'ts',
'js',
];
export type TMimeTypes = 'image/jpeg' | 'image/svg+xml' | 'application/json' | 'text/html';
export const detectMimeType = (pathArg: string) => {
return plugins.mimeTypes.lookup(pathArg) as TMimeTypes;
};
export const isBinary = (pathArg: string) => {
let isBinary = false;
for (const binaryFileType of binaryFileTypes) {
if (isBinary) {
continue;
}
isBinary = pathArg.endsWith(`.${binaryFileType}`);
}
return isBinary;
}
export const getEncoding = (pathArg: string) => {
return isBinary(pathArg) ? 'binary' : 'utf8';
}