Skip to content

Commit 55ef6e3

Browse files
committed
fix: base64 conditions
1 parent 1a66bcf commit 55ef6e3

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/client.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -226,57 +226,38 @@ async function getCustomData(file) {
226226

227227
// This function reads the file and returns its src
228228
function readFile(file) {
229-
// Return a new promise that resolves the file object
230229
return new Promise((resolve) => {
231-
// Split the file type into an array
232230
const fileType = file.type.split('/');
233231
let readAs;
234232

235-
// Check if the file type is a directory
236233
if (fileType[1] === 'directory') {
237234
return resolve(file)
238-
}
239-
// Check if the file type is a image
240-
else if (['jpg', 'jpeg', 'png', 'gif', 'bmp'].includes(fileType[1])
235+
} else if (['jpg', 'jpeg', 'png', 'gif', 'bmp'].includes(fileType[1])
241236
|| fileType[0] === 'image') {
242237
readAs = 'readAsDataURL';
243-
}
244-
// Check if the file type is a video
245-
else if (['mp4', 'avi', 'mov', 'mpeg', 'flv'].includes(fileType[1])
238+
} else if (['mp4', 'avi', 'mov', 'mpeg', 'flv'].includes(fileType[1])
246239
|| fileType[0] === 'video') {
247240
readAs = 'readAsDataURL';
248-
}
249-
// Check if the file type is an audio
250-
else if (['mp3', 'wav', 'wma', 'aac', 'ogg'].includes(fileType[1])
241+
} if (['mp3', 'wav', 'wma', 'aac', 'ogg'].includes(fileType[1])
251242
|| fileType[0] === 'audio') { // updated condition
252243
readAs = 'readAsDataURL';
253-
}
254-
// Check if the file type is a pdf
255-
else if (fileType[1] === 'pdf') {
244+
} else if (fileType[1] === 'pdf') {
256245
readAs = 'readAsDataURL';
257-
}
258-
// Check if the file type is a document
259-
else if (['doc', 'msword', 'docx', 'xlsx', 'pptx'].includes(fileType[1])) {
246+
} else if (['doc', 'msword', 'docx', 'xlsx', 'pptx'].includes(fileType[1])) {
260247
readAs = 'readAsBinaryString';
261-
}
262-
// Otherwise, assume the file type is text
263-
else {
248+
} else {
264249
readAs = 'readAsText';
265250
}
266251

267-
// Create a FileReader instance to read the file
268252
const reader = new FileReader();
269-
// Read the file based on the file type
270253
reader[readAs](file);
271-
// When the file is loaded, resolve the file object
254+
272255
reader.onload = () => {
273256
file.src = reader.result;
274-
// If the file type is a document, convert it to base64 encoding
275257
if (['doc', 'msword', 'docx', 'xlsx', 'pptx'].includes(fileType)) {
276258
file.src = btoa(file.src);
277259
}
278260

279-
// Resolve the file object
280261
resolve(file);
281262
};
282263
});

0 commit comments

Comments
 (0)