|
| 1 | +import { match } from 'ciaplu'; |
| 2 | + |
1 | 3 | export function mimeFromHex (hex: string) {
|
2 |
| - switch (hex.substring(0, 4)) { // 2 bytes |
3 |
| - case '424D': |
4 |
| - return { ext: 'bmp', mime: 'image/bmp' }; |
5 |
| - case '1F8B': |
6 |
| - return { ext: 'tar.gz', mime: 'application/gzip' }; |
7 |
| - case '0B77': |
8 |
| - return { ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' }; |
9 |
| - case '7801': |
10 |
| - return { ext: 'dmg', mime: 'application/x-apple-diskimage' }; |
11 |
| - case '4D5A': |
12 |
| - return { ext: 'exe', mime: 'application/x-msdownload' }; |
13 |
| - case '1FA0': |
14 |
| - case '1F9D': |
15 |
| - return { ext: 'Z', mime: 'application/x-compress' }; |
16 |
| - default: |
17 |
| - switch (hex.substring(0, 6)) { // 3 bytes |
18 |
| - case 'FFD8FF': |
19 |
| - return { ext: 'jpg', mime: 'image/jpeg' }; |
20 |
| - case '4949BC': |
21 |
| - return { ext: 'jxr', mime: 'image/vnd.ms-photo' }; |
22 |
| - case '425A68': |
23 |
| - return { ext: 'bz2', mime: 'application/x-bzip2' }; |
24 |
| - default: |
25 |
| - switch (hex) { // 4 bites |
26 |
| - case '89504E47': |
27 |
| - return { ext: 'png', mime: 'image/png' }; |
28 |
| - case '47494638': |
29 |
| - return { ext: 'gif', mime: 'image/gif' }; |
30 |
| - case '25504446': |
31 |
| - return { ext: 'pdf', mime: 'application/pdf' }; |
32 |
| - case '504B0304': |
33 |
| - return { ext: 'zip', mime: 'application/zip' }; |
34 |
| - case '425047FB': |
35 |
| - return { ext: 'bpg', mime: 'image/bpg' }; |
36 |
| - case '4D4D002A': |
37 |
| - return { ext: 'tif', mime: 'image/tiff' }; |
38 |
| - case '00000100': |
39 |
| - return { ext: 'ico', mime: 'image/x-icon' }; |
40 |
| - default: |
41 |
| - return { ext: '', mime: 'unknown ' + hex }; |
42 |
| - } |
43 |
| - } |
44 |
| - } |
| 4 | + return match(hex.substring(0, 4)) // 2 bytes |
| 5 | + .with('424D', () => ({ ext: 'bmp', mime: 'image/bmp' })) |
| 6 | + .with('1F8B', () => ({ ext: 'tar.gz', mime: 'application/gzip' })) |
| 7 | + .with('0B77', () => ({ ext: 'ac3', mime: 'audio/vnd.dolby.dd-raw' })) |
| 8 | + .with('7801', () => ({ ext: 'dmg', mime: 'application/x-apple-diskimage' })) |
| 9 | + .with('4D5A', () => ({ ext: 'exe', mime: 'application/x-msdownload' })) |
| 10 | + .when((val) => ['1FA0', '1F9D'].includes(val), () => ({ ext: 'Z', mime: 'application/x-compress' })) |
| 11 | + .extracting(() => hex.substring(0, 6)) // 3 bytes |
| 12 | + .with('FFD8FF', () => ({ ext: 'jpg', mime: 'image/jpeg' })) |
| 13 | + .with('4949BC', () => ({ ext: 'jxr', mime: 'image/vnd.ms-photo' })) |
| 14 | + .with('425A68', () => ({ ext: 'bz2', mime: 'application/x-bzip2' })) |
| 15 | + .extracting(() => hex) // 4 bytes |
| 16 | + .with('89504E47', () => ({ ext: 'png', mime: 'image/png' })) |
| 17 | + .with('47494638', () => ({ ext: 'gif', mime: 'image/gif' })) |
| 18 | + .with('25504446', () => ({ ext: 'pdf', mime: 'application/pdf' })) |
| 19 | + .with('504B0304', () => ({ ext: 'zip', mime: 'application/zip' })) |
| 20 | + .with('425047FB', () => ({ ext: 'bpg', mime: 'image/bpg' })) |
| 21 | + .with('4D4D002A', () => ({ ext: 'tif', mime: 'image/tiff' })) |
| 22 | + .with('00000100', () => ({ ext: 'ico', mime: 'image/x-icon' })) |
| 23 | + .otherwise(() => ({ ext: '', mime: 'unknown ' + hex })) |
| 24 | + .return(); |
45 | 25 | }
|
0 commit comments