save blob to indexeddb instead of base64 image string

This commit is contained in:
lloydzhou
2024-07-15 20:47:49 +08:00
parent 6ece818d69
commit a1117cd4ee
2 changed files with 20 additions and 2 deletions

View File

@@ -32,6 +32,16 @@ export function useFileDB() {
return useIndexedDB(StoreKey.File);
}
export function base64Image2Blob(base64Data: string, contentType: string) {
const byteCharacters = atob(base64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
return new Blob([byteArray], { type: contentType });
}
export function saveFileData(db, fileId, data) {
// save file content and return url start with `indexeddb://`
db.add({ id: fileId, data });
@@ -40,6 +50,9 @@ export function saveFileData(db, fileId, data) {
export async function getFileData(db, fileId, contentType = "image/png") {
const { data } = await db.getByID(fileId);
if (typeof data == "object") {
return URL.createObjectURL(data);
}
return `data:${contentType};base64,${data}`;
}