drop.chapril.org-firefoxsend/app/archive.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-08-08 00:40:17 +02:00
/* global LIMITS */
2018-07-26 07:26:11 +02:00
import { blobStream, concatStream } from './streams';
2018-08-10 22:11:17 +02:00
function isDupe(newFile, array) {
for (const file of array) {
if (
newFile.name === file.name &&
newFile.size === file.size &&
newFile.lastModified === file.lastModified
) {
return true;
}
}
return false;
}
2018-07-26 07:26:11 +02:00
export default class Archive {
2018-08-08 00:40:17 +02:00
constructor(files = []) {
2018-07-26 07:26:11 +02:00
this.files = Array.from(files);
}
get name() {
return this.files.length > 1 ? 'Send-Archive.zip' : this.files[0].name;
}
get type() {
return this.files.length > 1 ? 'send-archive' : this.files[0].type;
}
get size() {
return this.files.reduce((total, file) => total + file.size, 0);
}
2018-07-31 20:09:18 +02:00
get numFiles() {
return this.files.length;
}
2018-07-26 07:26:11 +02:00
get manifest() {
return {
files: this.files.map(file => ({
name: file.name,
size: file.size,
type: file.type
}))
};
}
get stream() {
return concatStream(this.files.map(file => blobStream(file)));
}
2018-08-03 21:24:41 +02:00
2018-08-08 00:40:17 +02:00
addFiles(files, maxSize) {
if (this.files.length + files.length > LIMITS.MAX_FILES_PER_ARCHIVE) {
throw new Error('tooManyFiles');
}
2018-08-10 22:11:17 +02:00
const newFiles = files.filter(file => !isDupe(file, this.files));
const newSize = newFiles.reduce((total, file) => total + file.size, 0);
2018-08-08 00:40:17 +02:00
if (this.size + newSize > maxSize) {
throw new Error('fileTooBig');
2018-08-03 21:24:41 +02:00
}
2018-08-10 22:11:17 +02:00
this.files = this.files.concat(newFiles);
2018-08-03 21:24:41 +02:00
return true;
}
remove(index) {
this.files.splice(index, 1);
}
2018-07-26 07:26:11 +02:00
}