From 23560c78799d66b171e4a084df8a6c2f1060df80 Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Thu, 30 Mar 2017 17:28:28 -0400 Subject: [PATCH] delete file from s3 if data environment variable is undefined --- server/controllers/file.controller.js | 6 ++++-- server/controllers/project.controller.js | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index b827db01..a3e4b9a7 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -46,8 +46,10 @@ function deleteMany(files, ids) { const objectKeys = []; each(ids, (id, cb) => { - if (files.id(id).url && moment(process.env.S3_DATE) < moment(files.id(id).createdAt)) { - objectKeys.push(files.id(id).url.split("/").pop()); + if (files.id(id).url) { + if (!process.env.S3_DATE || (process.env.S3_DATE && moment(process.env.S3_DATE) < moment(files.id(id).createdAt))) { + objectKeys.push(files.id(id).url.split("/").pop()); + } } files.id(id).remove(); cb(); diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index cfdfce42..591758e9 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -92,8 +92,15 @@ export function getProject(req, res) { function deleteFilesFromS3(files) { deleteObjectsFromS3( - files.filter((file) => file.url && moment(process.env.S3_DATE) < moment(file.createdAt)) - .map((file) => file.url.split('/').pop()) + files.filter((file) => { + if (file.url) { + if (!process.env.S3_DATE || (process.env.S3_DATE && moment(process.env.S3_DATE) < moment(file.createdAt))) { + return true; + } + } + return false; + }) + .map((file) => file.url.split('/').pop()) ); }