delete files based on S3, with a date to be backwards compatible
This commit is contained in:
parent
75b49d10a9
commit
8392acdf3f
4 changed files with 33 additions and 10 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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' });
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue