70 lines
1.7 KiB
JavaScript
70 lines
1.7 KiB
JavaScript
|
/**
|
||
|
* Compatibility functions for working with File API objects
|
||
|
*
|
||
|
* https://developer.mozilla.org/en-US/docs/Web/API/File
|
||
|
*/
|
||
|
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) {
|
||
|
return value instanceof P ? value : new P(function (resolve) {
|
||
|
resolve(value);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) {
|
||
|
try {
|
||
|
step(generator.next(value));
|
||
|
} catch (e) {
|
||
|
reject(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function rejected(value) {
|
||
|
try {
|
||
|
step(generator["throw"](value));
|
||
|
} catch (e) {
|
||
|
reject(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function step(result) {
|
||
|
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
||
|
}
|
||
|
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
|
||
|
export function isFile(file) {
|
||
|
// browser
|
||
|
if (typeof File === 'function') {
|
||
|
return file instanceof File;
|
||
|
} // 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 function fileArrayBuffer(file) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
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);
|
||
|
});
|
||
|
});
|
||
|
}
|