feat: add MIME sniffing and output MIME lookup
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
import { fileTypeFromFile } from 'file-type';
|
||||
|
||||
const OUTPUT_MIME_TYPES = {
|
||||
jpg: 'image/jpeg',
|
||||
jpeg: 'image/jpeg',
|
||||
png: 'image/png',
|
||||
webp: 'image/webp',
|
||||
gif: 'image/gif',
|
||||
tiff: 'image/tiff',
|
||||
avif: 'image/avif',
|
||||
bmp: 'image/bmp',
|
||||
pdf: 'application/pdf',
|
||||
html: 'text/html',
|
||||
txt: 'text/plain',
|
||||
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
};
|
||||
|
||||
export async function detectInputMime(filePath) {
|
||||
const result = await fileTypeFromFile(filePath);
|
||||
return result ?? null;
|
||||
}
|
||||
|
||||
export function outputMimeType(targetFormat) {
|
||||
const mime = OUTPUT_MIME_TYPES[targetFormat];
|
||||
if (!mime) {
|
||||
throw new Error(`No known MIME type for target format "${targetFormat}"`);
|
||||
}
|
||||
return mime;
|
||||
}
|
||||
Reference in New Issue
Block a user