-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
39 lines (37 loc) · 1 KB
/
Copy pathsample.js
File metadata and controls
39 lines (37 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export async function urlToFile(url, fetch) {
try {
const response = await fetch(url);
const blob = await response.blob();
const filename = extractFilenameFromUrl(url);
const type = getMimeType(filename);
const file = new File([blob], filename, { type });
return file;
} catch (error) {
console.warn("Error converting URL to File:", error);
return null;
}
}
function extractFilenameFromUrl(url) {
const urlObj = new URL(url);
const path = urlObj.pathname;
const parts = path.split("/");
return parts[parts.length - 1];
}
function getMimeType(filename) {
const extension = filename.split(".").pop().toLowerCase();
switch (extension) {
case "pdf":
return "application/pdf";
case "jpg":
case "jpeg":
return "image/jpeg";
case "png":
return "image/png";
case "doc":
return "application/msword";
case "docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
default:
return "";
}
}