-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added getBase64 and formatted code
- Loading branch information
1 parent
0ccd48a
commit 20c1655
Showing
7 changed files
with
61 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ | |
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const isBrowser = | ||
typeof window !== 'undefined' && typeof window.document !== 'undefined' | ||
|
||
export const getBase64 = ( | ||
file: File | Buffer, | ||
mimeType?: string | ||
): Promise<string> => { | ||
if (isBrowser) { | ||
return new Promise((resolve, reject) => { | ||
if (!(file instanceof File)) { | ||
reject( | ||
new Error( | ||
'Invalid file type for browser environment. Expected a File object.' | ||
) | ||
) | ||
return | ||
} | ||
|
||
const reader = new FileReader() | ||
reader.onloadend = () => { | ||
const base64File = reader.result as string | ||
resolve(`data:${file.type};base64,${base64File.split(',')[1]}`) | ||
} | ||
reader.onerror = () => reject(new Error('Error reading file')) | ||
|
||
reader.readAsDataURL(file) | ||
}) | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
if (!(file instanceof Buffer)) { | ||
reject( | ||
new Error( | ||
'Invalid file type for Node.js environment. Expected a Buffer object.' | ||
) | ||
) | ||
return | ||
} | ||
|
||
const base64File = file.toString('base64') | ||
const mime = mimeType || 'application/octet-stream' // Provide a default MIME type if not provided | ||
resolve(`data:${mime};base64,${base64File}`) | ||
}) | ||
} | ||
} |