smartmime/ts/index.ts

27 lines
740 B
TypeScript
Raw Permalink Normal View History

2023-07-11 22:32:17 +00:00
import * as plugins from './smartmime.plugins.js';
2020-03-04 14:21:44 +00:00
2021-08-10 09:49:39 +00:00
export const binaryFileTypes = ['jpg', 'png', 'pdf'];
2020-03-04 14:21:44 +00:00
2021-08-10 09:49:39 +00:00
export const supportedFileTypes = ['json', 'html', 'svg', 'jpg', 'ts', 'js'];
2020-03-04 14:21:44 +00:00
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;
2021-08-10 09:49:39 +00:00
};
2020-03-04 15:47:15 +00:00
export const getEncoding = (pathArg: string) => {
return isBinary(pathArg) ? 'binary' : 'utf8';
2021-08-10 09:49:39 +00:00
};