archive.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* global MAXFILESIZE */
  2. import { blobStream, concatStream } from './streams';
  3. export default class Archive {
  4. constructor(files) {
  5. this.files = Array.from(files);
  6. }
  7. get name() {
  8. return this.files.length > 1 ? 'Send-Archive.zip' : this.files[0].name;
  9. }
  10. get type() {
  11. return this.files.length > 1 ? 'send-archive' : this.files[0].type;
  12. }
  13. get size() {
  14. return this.files.reduce((total, file) => total + file.size, 0);
  15. }
  16. get numFiles() {
  17. return this.files.length;
  18. }
  19. get manifest() {
  20. return {
  21. files: this.files.map(file => ({
  22. name: file.name,
  23. size: file.size,
  24. type: file.type
  25. }))
  26. };
  27. }
  28. get stream() {
  29. return concatStream(this.files.map(file => blobStream(file)));
  30. }
  31. addFiles(files) {
  32. const newSize = files.reduce((total, file) => total + file.size, 0);
  33. if (this.size + newSize > MAXFILESIZE) {
  34. return false;
  35. }
  36. this.files = this.files.concat(files);
  37. return true;
  38. }
  39. checkSize() {
  40. return this.size <= MAXFILESIZE;
  41. }
  42. remove(index) {
  43. this.files.splice(index, 1);
  44. }
  45. }