|
1 |
| -import { FileExtension } from "@/types"; |
| 1 | +import { FileExtension, FileContentType } from "@/types/files"; |
| 2 | + |
| 3 | +export const fileExtensions = new Map<FileExtension, FileContentType>([ |
| 4 | + ["txt", "text/plain"], |
| 5 | + ["md", "text/markdown"], |
| 6 | + ["json", "application/json"], |
| 7 | + ["csv", "text/csv"], |
| 8 | + ["png", "image/png"], |
| 9 | + ["jpg", "image/jpeg"], |
| 10 | + ["jpeg", "image/jpeg"], |
| 11 | + ["webp", "image/webp"], |
| 12 | + ["gif", "image/gif"], |
| 13 | + ["svg", "image/svg+xml"], |
| 14 | + ["pdf", "application/pdf"], |
| 15 | + ["doc", "application/msword"], |
| 16 | + [ |
| 17 | + "docx", |
| 18 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 19 | + ], |
| 20 | + ["xls", "application/vnd.ms-excel"], |
| 21 | + [ |
| 22 | + "xlsx", |
| 23 | + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 24 | + ], |
| 25 | + ["ppt", "application/vnd.ms-powerpoint"], |
| 26 | + [ |
| 27 | + "pptx", |
| 28 | + "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 29 | + ], |
| 30 | + ["mp3", "audio/mpeg"], |
| 31 | + ["mp4", "video/mp4"], |
| 32 | + ["mov", "video/quicktime"], |
| 33 | + ["avi", "video/x-msvideo"], |
| 34 | + ["mkv", "video/x-matroska"], |
| 35 | + ["zip", "application/zip"], |
| 36 | + ["rar", "application/vnd.rar"], |
| 37 | + ["tar", "application/x-tar"], |
| 38 | + ["gz", "application/gzip"], |
| 39 | +]); |
2 | 40 |
|
3 | 41 | export const getContentType = (extension: FileExtension) => {
|
4 |
| - switch (extension) { |
5 |
| - case "txt": |
6 |
| - return "text/plain"; |
7 |
| - case "md": |
8 |
| - return "text/markdown"; |
9 |
| - case "json": |
10 |
| - return "application/json"; |
11 |
| - case "csv": |
12 |
| - return "text/csv"; |
13 |
| - default: |
14 |
| - return "text/plain"; |
15 |
| - } |
| 42 | + return fileExtensions.get(extension) || "text/plain"; |
| 43 | +}; |
| 44 | + |
| 45 | +export const getExtension = (contentType: FileContentType) => { |
| 46 | + return ( |
| 47 | + Array.from(fileExtensions.entries()).find( |
| 48 | + ([_, value]) => value === contentType |
| 49 | + )?.[0] || "txt" |
| 50 | + ); |
16 | 51 | };
|
17 | 52 |
|
18 | 53 | export const saveFile = (
|
@@ -41,7 +76,7 @@ export const readFile = (file: File) => {
|
41 | 76 | reader.onerror = () => {
|
42 | 77 | reject(reader.error);
|
43 | 78 | };
|
44 |
| - reader.readAsText(file); |
| 79 | + reader.readAsDataURL(file); |
45 | 80 | });
|
46 | 81 | };
|
47 | 82 |
|
|
0 commit comments