|
| 1 | +import { extname } from 'path'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Inline MIME table covering the file types commonly served from |
| 5 | + * Tigris buckets. Mirrors the AWS CLI behaviour of `mimetypes.guess_type` |
| 6 | + * by extension — extension-only, no content sniffing. Returns |
| 7 | + * `undefined` for unknown extensions so callers omit the |
| 8 | + * `Content-Type` header and let the server default apply (matches |
| 9 | + * `aws s3 cp`'s behaviour, which never emits a fallback |
| 10 | + * `application/octet-stream`). |
| 11 | + */ |
| 12 | +const MIME_TABLE: Record<string, string> = { |
| 13 | + // Markup / scripts |
| 14 | + html: 'text/html', |
| 15 | + htm: 'text/html', |
| 16 | + css: 'text/css', |
| 17 | + js: 'text/javascript', |
| 18 | + mjs: 'text/javascript', |
| 19 | + cjs: 'text/javascript', |
| 20 | + json: 'application/json', |
| 21 | + map: 'application/json', |
| 22 | + xml: 'application/xml', |
| 23 | + svg: 'image/svg+xml', |
| 24 | + webmanifest: 'application/manifest+json', |
| 25 | + wasm: 'application/wasm', |
| 26 | + |
| 27 | + // Plain text |
| 28 | + txt: 'text/plain', |
| 29 | + log: 'text/plain', |
| 30 | + md: 'text/markdown', |
| 31 | + csv: 'text/csv', |
| 32 | + yaml: 'application/yaml', |
| 33 | + yml: 'application/yaml', |
| 34 | + |
| 35 | + // Documents |
| 36 | + pdf: 'application/pdf', |
| 37 | + rtf: 'application/rtf', |
| 38 | + |
| 39 | + // Images |
| 40 | + png: 'image/png', |
| 41 | + jpg: 'image/jpeg', |
| 42 | + jpeg: 'image/jpeg', |
| 43 | + gif: 'image/gif', |
| 44 | + webp: 'image/webp', |
| 45 | + avif: 'image/avif', |
| 46 | + ico: 'image/x-icon', |
| 47 | + bmp: 'image/bmp', |
| 48 | + tif: 'image/tiff', |
| 49 | + tiff: 'image/tiff', |
| 50 | + |
| 51 | + // Fonts |
| 52 | + woff: 'font/woff', |
| 53 | + woff2: 'font/woff2', |
| 54 | + ttf: 'font/ttf', |
| 55 | + otf: 'font/otf', |
| 56 | + eot: 'application/vnd.ms-fontobject', |
| 57 | + |
| 58 | + // Video |
| 59 | + mp4: 'video/mp4', |
| 60 | + m4v: 'video/x-m4v', |
| 61 | + webm: 'video/webm', |
| 62 | + mov: 'video/quicktime', |
| 63 | + avi: 'video/x-msvideo', |
| 64 | + mkv: 'video/x-matroska', |
| 65 | + |
| 66 | + // Audio |
| 67 | + mp3: 'audio/mpeg', |
| 68 | + m4a: 'audio/mp4', |
| 69 | + wav: 'audio/wav', |
| 70 | + ogg: 'audio/ogg', |
| 71 | + flac: 'audio/flac', |
| 72 | + aac: 'audio/aac', |
| 73 | + opus: 'audio/opus', |
| 74 | + |
| 75 | + // Archives |
| 76 | + zip: 'application/zip', |
| 77 | + tar: 'application/x-tar', |
| 78 | + gz: 'application/gzip', |
| 79 | + tgz: 'application/gzip', |
| 80 | + bz2: 'application/x-bzip2', |
| 81 | + '7z': 'application/x-7z-compressed', |
| 82 | + rar: 'application/vnd.rar', |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * Look up a Content-Type from a file path's extension. Returns |
| 87 | + * `undefined` when the extension is unknown — callers should omit the |
| 88 | + * Content-Type rather than fall back to `application/octet-stream`. |
| 89 | + */ |
| 90 | +export function getContentType(filePath: string): string | undefined { |
| 91 | + const ext = extname(filePath).slice(1).toLowerCase(); |
| 92 | + if (!ext) return undefined; |
| 93 | + return MIME_TABLE[ext]; |
| 94 | +} |
0 commit comments