@ethersphere/bee-js
Version:
Javascript client for Bee
46 lines (45 loc) • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCollectionSize = exports.makeCollectionFromFileList = exports.makeFilePath = exports.assertCollection = exports.isCollection = void 0;
const error_1 = require("./error");
function isCollection(data) {
if (!Array.isArray(data)) {
return false;
}
return data.every(entry => typeof entry === 'object' && entry.path && entry.size !== undefined);
}
exports.isCollection = isCollection;
function assertCollection(data) {
if (!isCollection(data)) {
throw new error_1.BeeArgumentError('invalid collection', data);
}
}
exports.assertCollection = assertCollection;
function makeFilePath(file) {
if (file.webkitRelativePath && file.webkitRelativePath !== '') {
return file.webkitRelativePath.replace(/.*?\//i, '');
}
if (file.name) {
return file.name;
}
throw new TypeError('file is not valid File object');
}
exports.makeFilePath = makeFilePath;
function makeCollectionFromFileList(fileList) {
return Array.from(fileList).map(file => ({
path: makeFilePath(file),
size: file.size,
file,
}));
}
exports.makeCollectionFromFileList = makeCollectionFromFileList;
/**
* Calculate cumulative size of files
*
* @param fileList list of files to check
* @returns size in bytes
*/
function getCollectionSize(fileList) {
return Array.from(fileList).reduce((sum, file) => sum + file.size, 0);
}
exports.getCollectionSize = getCollectionSize;