Skip to content

Commit e46858b

Browse files
fix: files service
1 parent 6451c6c commit e46858b

File tree

2 files changed

+103
-15
lines changed

2 files changed

+103
-15
lines changed

services/files.service.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,53 @@
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+
]);
240

341
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+
);
1651
};
1752

1853
export const saveFile = (
@@ -41,7 +76,7 @@ export const readFile = (file: File) => {
4176
reader.onerror = () => {
4277
reject(reader.error);
4378
};
44-
reader.readAsText(file);
79+
reader.readAsDataURL(file);
4580
});
4681
};
4782

types/files.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
export type FileExtension = "txt" | "md" | "json" | "csv";
1+
export type FileExtension =
2+
| "txt"
3+
| "md"
4+
| "json"
5+
| "csv"
6+
| "png"
7+
| "jpg"
8+
| "jpeg"
9+
| "webp"
10+
| "gif"
11+
| "svg"
12+
| "pdf"
13+
| "doc"
14+
| "docx"
15+
| "xls"
16+
| "xlsx"
17+
| "ppt"
18+
| "pptx"
19+
| "mp3"
20+
| "mp4"
21+
| "mov"
22+
| "avi"
23+
| "mkv"
24+
| "zip"
25+
| "rar"
26+
| "tar"
27+
| "gz";
28+
29+
export type FileContentType =
30+
| "text/plain"
31+
| "text/markdown"
32+
| "application/json"
33+
| "text/csv"
34+
| "image/png"
35+
| "image/jpeg"
36+
| "image/webp"
37+
| "image/gif"
38+
| "image/svg+xml"
39+
| "application/pdf"
40+
| "application/msword"
41+
| "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
42+
| "application/vnd.ms-excel"
43+
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
44+
| "application/vnd.ms-powerpoint"
45+
| "application/vnd.openxmlformats-officedocument.presentationml.presentation"
46+
| "audio/mpeg"
47+
| "video/mp4"
48+
| "video/quicktime"
49+
| "video/x-msvideo"
50+
| "video/x-matroska"
51+
| "application/zip"
52+
| "application/vnd.rar"
53+
| "application/x-tar"
54+
| "application/gzip";

0 commit comments

Comments
 (0)