2018-07-26 05:26:11 +00:00
|
|
|
import { blobStream, concatStream } from './streams';
|
|
|
|
|
2018-08-10 20:11:17 +00: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 05:26:11 +00:00
|
|
|
export default class Archive {
|
2021-06-04 12:03:58 +00:00
|
|
|
constructor(files = [], defaultTimeLimit = 86400, defaultDownloadLimit = 1) {
|
2018-07-26 05:26:11 +00:00
|
|
|
this.files = Array.from(files);
|
2019-02-26 18:39:50 +00:00
|
|
|
this.defaultTimeLimit = defaultTimeLimit;
|
2023-01-06 08:42:45 +00:00
|
|
|
this.defaultDownloadLimit = defaultDownloadLimit;
|
2019-02-26 18:39:50 +00:00
|
|
|
this.timeLimit = defaultTimeLimit;
|
2021-06-04 12:03:58 +00:00
|
|
|
this.dlimit = defaultDownloadLimit;
|
2019-02-12 19:50:06 +00:00
|
|
|
this.password = null;
|
2018-07-26 05:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:09:18 +00:00
|
|
|
get numFiles() {
|
|
|
|
return this.files.length;
|
|
|
|
}
|
|
|
|
|
2018-07-26 05:26:11 +00: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 19:24:41 +00:00
|
|
|
|
2019-02-26 18:39:50 +00:00
|
|
|
addFiles(files, maxSize, maxFiles) {
|
|
|
|
if (this.files.length + files.length > maxFiles) {
|
2018-08-07 22:40:17 +00:00
|
|
|
throw new Error('tooManyFiles');
|
|
|
|
}
|
2019-01-04 00:12:40 +00:00
|
|
|
const newFiles = files.filter(
|
|
|
|
file => file.size > 0 && !isDupe(file, this.files)
|
|
|
|
);
|
2018-08-10 20:11:17 +00:00
|
|
|
const newSize = newFiles.reduce((total, file) => total + file.size, 0);
|
2018-08-07 22:40:17 +00:00
|
|
|
if (this.size + newSize > maxSize) {
|
|
|
|
throw new Error('fileTooBig');
|
2018-08-03 19:24:41 +00:00
|
|
|
}
|
2018-08-10 20:11:17 +00:00
|
|
|
this.files = this.files.concat(newFiles);
|
2018-08-03 19:24:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-25 02:07:10 +00:00
|
|
|
remove(file) {
|
|
|
|
const index = this.files.indexOf(file);
|
|
|
|
if (index > -1) {
|
|
|
|
this.files.splice(index, 1);
|
|
|
|
}
|
2018-08-03 19:24:41 +00:00
|
|
|
}
|
2018-12-21 18:27:46 +00:00
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.files = [];
|
2023-01-06 08:42:45 +00:00
|
|
|
this.dlimit = this.defaultDownloadLimit;
|
2019-02-26 18:39:50 +00:00
|
|
|
this.timeLimit = this.defaultTimeLimit;
|
2019-02-12 19:50:06 +00:00
|
|
|
this.password = null;
|
2018-12-21 18:27:46 +00:00
|
|
|
}
|
2018-07-26 05:26:11 +00:00
|
|
|
}
|