@ethersphere/bee-js
Version:
Javascript client for Bee
37 lines • 1.05 kB
JavaScript
import { BeeArgumentError } from "./error.js";
export function isCollection(data) {
if (!Array.isArray(data)) {
return false;
}
return data.every(entry => typeof entry === 'object' && entry.path && entry.size !== undefined);
}
export function assertCollection(data) {
if (!isCollection(data)) {
throw new BeeArgumentError('invalid collection', data);
}
}
export 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');
}
export function makeCollectionFromFileList(fileList) {
return Array.from(fileList).map(file => ({
path: makeFilePath(file),
size: file.size,
file
}));
}
/**
* Calculate cumulative size of files
*
* @param fileList list of files to check
* @returns size in bytes
*/
export function getCollectionSize(fileList) {
return Array.from(fileList).reduce((sum, file) => sum + file.size, 0);
}