Skip to content

Commit

Permalink
fix: blobToArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Oct 29, 2024
1 parent 6fbbac5 commit d4c9f35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
35 changes: 19 additions & 16 deletions src/common/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,25 @@ export function bitfield(bitfield = 0) {
// Cast

export async function blobToArrayBuffer(blob: Blob | File): Promise<ArrayBuffer | undefined> {
return await blob.arrayBuffer()
// try {
// return new Promise((resolve, reject) => {
// try {
// const reader = new FileReader()
// reader.onloadend = () => resolve(reader.result as ArrayBuffer)
// reader.onerror = err => reject(err)
// reader.onabort = err => reject(err)
// reader.readAsArrayBuffer(blob)
// }
// catch (err) {
// reject(err)
// }
// })
// }
// catch (err) {}
if (blob.arrayBuffer)
return await blob.arrayBuffer()

// Fallback for older browsers
try {
return new Promise((resolve, reject) => {
try {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result as ArrayBuffer)
reader.onerror = err => reject(err)
reader.onabort = err => reject(err)
reader.readAsArrayBuffer(blob)
}
catch (err) {
reject(err)
}
})
}
catch (err) {}
}

export async function blobToUint8Array(blob: Blob | File): Promise<Uint8Array | undefined> {
Expand Down
3 changes: 2 additions & 1 deletion src/common/data/datauri.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { blobToArrayBuffer } from '../bin'
import { fromBase64, toBase64 } from './bin'

export function dataUriToUint8Array(dataUri: string): Uint8Array | undefined {
Expand All @@ -18,7 +19,7 @@ export function dataUriToBlob(dataUri: string): Blob | undefined {
}

export async function blobToDataUri(blob: Blob): Promise<string | undefined> {
const bin = await blob.arrayBuffer()
const bin = await blobToArrayBuffer(blob)
if (bin)
return `data:${blob.type},${toBase64(bin)}`
}
Expand Down

0 comments on commit d4c9f35

Please sign in to comment.