-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchAndSaveFileFromUrl.js
117 lines (108 loc) · 3.96 KB
/
FetchAndSaveFileFromUrl.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Converts a data URI to a Blob object.
* @param {string} dataURI - The data URI string.
* @returns {Blob} The resulting Blob object.
*/
const dataURItoBlob = (dataURI) => {
// Retrieve byte string and MIME type
const byteString = atob(dataURI.split(",")[1]);
const mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0];
// Convert to ArrayBuffer and create Uint8Array
const arrayBuffer = new ArrayBuffer(byteString.length);
const int8Array = new Uint8Array(arrayBuffer);
// Map byteString characters to int8Array
for (let i = 0; i < byteString.length; i++) {
int8Array[i] = byteString.charCodeAt(i);
}
// Return blob from ArrayBuffer
return new Blob([arrayBuffer], { type: mimeString });
};
/**
* Retrieves the base64 representation of a file from a Blob object.
* @param {Blob} blob - The Blob object containing the file data.
* @returns {Promise<string>} A Promise resolving to the base64 representation of the file.
*/
const getBase64FileFromBlob = (blob) => {
return new Promise((resolve, reject) => {
try {
const reader = new FileReader();
reader.onload = function () {
const base64File = reader.result;
resolve(base64File); // Resolve the Promise with base64 file
};
reader.onerror = function (error) {
reader.abort();
reject(error); // Reject if there's an error
};
reader.readAsDataURL(blob);
} catch (error) {
console.error("Error getting file:", error);
reject(error); // Reject if there's an error
}
});
};
/**
* Fetches a file URL and returns a Blob object.
* @param {string} fileUrl - The URL of the file to fetch.
* @returns {Promise<Blob|null>} A Promise resolving to the fetched Blob object, or null if an error occurs.
*/
async function fetchFileUrl(fileUrl) {
try {
const response = await fetch(fileUrl); // Fetch file URL
const blob = await response.blob(); // Convert response to blob
return blob;
} catch (error) {
console.error("Error fetching:", error);
return null;
}
}
/**
* Fetches a file from a URL, processes it, saves it in local storage, and returns its displayable URL.
* @param {string} fileUrl - The URL of the file to fetch.
* @returns {Promise<string|null>} A Promise resolving to the displayable URL of the file, or null if an error occurs.
* @example
* // Usage example:
* const fileUrl = 'https://example.com/file.pdf';
* fetchAndSaveFileFromUrl(fileUrl)
* .then((displayableFileUrl) => {
* console.log('Processed file URL:', displayableFileUrl);
* // Use the displayableFileUrl as needed
* })
* .catch((error) => {
* console.error('Error processing file:', error);
* });
*/
export const fetchAndSaveFileFromUrl = async (fileUrl) => {
const [retrieveSavedFile, saveFileToStorage] = [
() => localStorage.getItem(`savedFile:${fileUrl}`),
(url) => localStorage.setItem(`savedFile:${fileUrl}`, url),
];
const fetchAndSaveFile = async () => {
if (retrieveSavedFile()) return; // If file exists in storage, return
const blob = await fetchFileUrl(fileUrl); // Fetch file blob
if (!blob) {
console.error("No blob found");
return; // If no blob, return
}
const base64File = await getBase64FileFromBlob(blob);
if (!base64File) {
console.error("No base64File generated from blob");
return;
} // If no base64File, return
saveFileToStorage(base64File);
};
const getAndDisplayFile = () => {
const base64File = retrieveSavedFile(); // Retrieve base64 file from storage
if (!base64File) {
console.error("No base64File found in local storage");
return;
} // If no base64File, return
const blob = dataURItoBlob(base64File); // Convert base64 to blob
const newFileUrl = URL.createObjectURL(blob); // Create object URL for blob
return newFileUrl; // Return object URL
};
return Promise.resolve()
.then(fetchAndSaveFile)
.then(getAndDisplayFile)
.catch((error) => console.error(error));
};