sindresorhus / file-type

Detect the file type of a file, stream, or data
MIT License
3.64k stars 345 forks source link

fileTypeFromBase64 #610

Closed phaux closed 12 months ago

phaux commented 12 months ago

We need to know the mime type before we can parse it using fetch data url.

const type = fileTypeFromBase64(base64string) // add this

const blob = await fetch(`data:${type.mime};base64,${base64string}`)
    .then(resp => resp.blob())
sindresorhus commented 12 months ago

You can easily convert Base64 to a Buffer. I don't think this is common of enough need to support built-in.

Buffer.from(b64string, 'base64');
phaux commented 12 months ago

But Buffer.from is node only :/

sindresorhus commented 12 months ago

https://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer

phaux commented 12 months ago

Yeah I know, but the fetch data url method is async and better on front-end where you want consistent 60fps. Just sayin.

What alternatives do we have according to that SO post: