|
@@ -1,25 +1,32 @@
|
|
|
module.exports = function dataURItoBlob (dataURI, opts, toFile) {
|
|
|
// get the base64 data
|
|
|
- var data = dataURI.split(',')[1]
|
|
|
+ const data = dataURI.split(',')[1]
|
|
|
|
|
|
// user may provide mime type, if not get it from data URI
|
|
|
- var mimeType = opts.mimeType || dataURI.split(',')[0].split(':')[1].split(';')[0]
|
|
|
+ let mimeType = opts.mimeType || dataURI.split(',')[0].split(':')[1].split(';')[0]
|
|
|
|
|
|
// default to plain/text if data URI has no mimeType
|
|
|
if (mimeType == null) {
|
|
|
mimeType = 'plain/text'
|
|
|
}
|
|
|
|
|
|
- var binary = atob(data)
|
|
|
- var array = []
|
|
|
- for (var i = 0; i < binary.length; i++) {
|
|
|
+ const binary = atob(data)
|
|
|
+ const array = []
|
|
|
+ for (let i = 0; i < binary.length; i++) {
|
|
|
array.push(binary.charCodeAt(i))
|
|
|
}
|
|
|
|
|
|
+ let bytes
|
|
|
+ try {
|
|
|
+ bytes = new Uint8Array(array) // eslint-disable-line compat/compat
|
|
|
+ } catch (err) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+
|
|
|
// Convert to a File?
|
|
|
if (toFile) {
|
|
|
- return new File([new Uint8Array(array)], opts.name || '', { type: mimeType })
|
|
|
+ return new File([bytes], opts.name || '', { type: mimeType })
|
|
|
}
|
|
|
|
|
|
- return new Blob([new Uint8Array(array)], { type: mimeType })
|
|
|
+ return new Blob([bytes], { type: mimeType })
|
|
|
}
|