From 29bafe1bae929f8031c7b53972a77ce399cbc9ab Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 10 Aug 2018 13:11:17 -0700 Subject: [PATCH] prevent uploading duplicate files --- app/archive.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/archive.js b/app/archive.js index 6070799f..24d1a2de 100644 --- a/app/archive.js +++ b/app/archive.js @@ -1,6 +1,19 @@ /* global MAXFILESIZE */ import { blobStream, concatStream } from './streams'; +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; +} + export default class Archive { constructor(files) { this.files = Array.from(files); @@ -37,11 +50,12 @@ export default class Archive { } addFiles(files) { - const newSize = files.reduce((total, file) => total + file.size, 0); + const newFiles = files.filter(file => !isDupe(file, this.files)); + const newSize = newFiles.reduce((total, file) => total + file.size, 0); if (this.size + newSize > MAXFILESIZE) { return false; } - this.files = this.files.concat(files); + this.files = this.files.concat(newFiles); return true; }