From 8392acdf3f61d4f14f73bf4938035c22ca23efd6 Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Thu, 30 Mar 2017 16:52:36 -0400 Subject: [PATCH] delete files based on S3, with a date to be backwards compatible --- server/controllers/aws.controller.js | 19 +++++++++++-------- server/controllers/file.controller.js | 13 ++++++++++++- server/controllers/project.controller.js | 10 +++++++++- server/routes/aws.routes.js | 1 + 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/server/controllers/aws.controller.js b/server/controllers/aws.controller.js index 1ff524ed..49f3a9b9 100644 --- a/server/controllers/aws.controller.js +++ b/server/controllers/aws.controller.js @@ -21,17 +21,13 @@ function getExtension(filename) { return (i < 0) ? '' : filename.substr(i); } -export function deleteObjectsFromS3(urlList, callback) { - if (urlList.length > 0) { - const objectKeyList = []; - each(urlList, (url) => { - const objectKey = url.split("/").pop(); - objectKeyList.push({Key: objectKey}) - }); +export function deleteObjectsFromS3(keyList, callback) { + const keys = keyList.map((key) => { return {Key: key}; }); + if (keyList.length > 0) { const params = { Bucket: `${process.env.S3_BUCKET}`, Delete: { - Objects: objectKeyList, + Objects: keys, }, }; const del = client.deleteObjects(params); @@ -47,6 +43,13 @@ export function deleteObjectsFromS3(urlList, callback) { } } +export function deleteObjectFromS3(req, res) { + const objectKey = req.params.object_key; + deleteObjectsFromS3([objectKey], function() { + res.json({ success:true }); + }); +} + export function signS3(req, res) { const fileExtension = getExtension(req.body.name); const filename = uuid.v4() + fileExtension; diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index 6cf4af5a..b827db01 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -1,5 +1,8 @@ import Project from '../models/project'; +import each from 'async/each'; +import moment from 'moment'; import { resolvePathToFile } from '../utils/filePath'; +import { deleteObjectsFromS3 } from './aws.controller'; // Bug -> timestamps don't get created, but it seems like this will // be fixed in mongoose soon @@ -40,8 +43,16 @@ function getAllDescendantIds(files, nodeId) { } function deleteMany(files, ids) { - ids.forEach((id) => { + 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()); + } files.id(id).remove(); + cb(); + }, (err) => { + deleteObjectsFromS3(objectKeys); }); } diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index fc19a23c..cfdfce42 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -3,7 +3,7 @@ import request from 'request'; import moment from 'moment'; import Project from '../models/project'; import User from '../models/user'; - +import { deleteObjectsFromS3 } from './aws.controller'; export function createProject(req, res) { if (!req.user) { @@ -90,12 +90,20 @@ 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()) + ); +} + export function deleteProject(req, res) { Project.findById(req.params.project_id, (findProjectErr, project) => { if (!req.user || !project.user.equals(req.user._id)) { res.status(403).json({ success: false, message: 'Session does not match owner of project.' }); return; } + deleteFilesFromS3(project.files); Project.remove({ _id: req.params.project_id }, (removeProjectError) => { if (removeProjectError) { res.status(404).send({ message: 'Project with that id does not exist' }); diff --git a/server/routes/aws.routes.js b/server/routes/aws.routes.js index 8d8e1462..85daddf7 100644 --- a/server/routes/aws.routes.js +++ b/server/routes/aws.routes.js @@ -5,5 +5,6 @@ const router = new Router(); router.route('/S3/sign').post(AWSController.signS3); router.route('/S3/copy').post(AWSController.copyObjectInS3); +router.route('/S3/:object_key').delete(AWSController.deleteObjectFromS3); export default router;