@ethersphere/bee-js
Version:
Javascript client for Bee
31 lines • 894 B
JavaScript
/**
* Compatibility functions for working with File API objects
*
* https://developer.mozilla.org/en-US/docs/Web/API/File
*/
export function isFile(file) {
// browser
if (typeof File === 'function' && file instanceof File) {
return true;
}
// node.js
const f = file;
return typeof f === 'object' && typeof f.name === 'string' && (typeof f.stream === 'function' || typeof f.arrayBuffer === 'function');
}
/**
* Compatibility helper for browsers where the `arrayBuffer function is
* missing from `File` objects.
*
* @param file A File object
*/
export async function fileArrayBuffer(file) {
if (file.arrayBuffer) {
return file.arrayBuffer();
}
// workaround for Safari where arrayBuffer is not supported on Files
return new Promise(resolve => {
const fr = new FileReader();
fr.onload = () => resolve(fr.result);
fr.readAsArrayBuffer(file);
});
}