Skip to content

Commit 9427583

Browse files
committed
#167 delete files in S3 when deleted in editor
1 parent 51336ba commit 9427583

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"extract-text-webpack-plugin": "^1.0.1",
4444
"file-loader": "^0.8.5",
4545
"node-sass": "^3.7.0",
46-
"nodemon": "^1.9.2",
46+
"nodemon": "^1.11.0",
4747
"postcss-cssnext": "^2.7.0",
4848
"postcss-focus": "^1.0.0",
4949
"postcss-loader": "^0.9.1",
@@ -109,6 +109,7 @@
109109
"redux-thunk": "^2.1.0",
110110
"request": "^2.76.0",
111111
"request-promise": "^4.1.1",
112+
"s3": "^4.4.0",
112113
"s3-policy": "^0.2.0",
113114
"shortid": "^2.2.6",
114115
"srcdoc-polyfill": "^0.2.0",

server/controllers/file.controller.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Project from '../models/project';
22
import { resolvePathToFile } from '../utils/filePath';
3+
import { deleteObjectFromS3 } from '../utils/s3Operations';
34

45
// Bug -> timestamps don't get created, but it seems like this will
56
// be fixed in mongoose soon
@@ -39,6 +40,10 @@ function getAllDescendantIds(files, nodeId) {
3940

4041
function deleteMany(files, ids) {
4142
ids.forEach(id => {
43+
// Delete file from S3 bucket
44+
if (!(files.id(id).url === undefined)) {
45+
deleteObjectFromS3(files.id(id).url);
46+
}
4247
files.id(id).remove();
4348
});
4449
}

server/controllers/project.controller.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Project from '../models/project';
22
import User from '../models/user';
33
import archiver from 'archiver';
44
import request from 'request';
5+
import { deleteObjectFromS3 } from '../utils/s3Operations';
56

67

78
export function createProject(req, res) {
@@ -63,12 +64,24 @@ export function getProject(req, res) {
6364
});
6465
}
6566

66-
export function deleteProject(req, res) {
67-
Project.remove({ _id: req.params.project_id }, (err) => {
68-
if (err) {
69-
return res.status(404).send({ message: 'Project with that id does not exist' });
67+
function deleteFilesFromS3(files) {
68+
files.forEach(file => {
69+
if (!(file.url === undefined)) {
70+
deleteObjectFromS3(file.url);
7071
}
71-
return res.json({ success: true });
72+
});
73+
}
74+
75+
export function deleteProject(req, res) {
76+
// Deleting project files form S3
77+
Project.findById(req.params.project_id, (err, project) => {
78+
deleteFilesFromS3(project.files);
79+
Project.remove({ _id: req.params.project_id }, (err) => {
80+
if (err) {
81+
return res.status(404).send({ message: 'Project with that id does not exist' });
82+
}
83+
return res.json({ success: true });
84+
});
7285
});
7386
}
7487

server/utils/s3Operations.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import s3 from 's3';
2+
3+
let client = s3.createClient({
4+
maxAsyncS3: 20,
5+
s3RetryCount: 3,
6+
s3RetryDelay: 1000,
7+
multipartUploadThreshold: 20971520, // this is the default (20 MB)
8+
multipartUploadSize: 15728640, // this is the default (15 MB)
9+
s3Options: {
10+
accessKeyId: `${process.env.AWS_ACCESS_KEY}`,
11+
secretAccessKey: `${process.env.AWS_SECRET_KEY}`,
12+
},
13+
});
14+
15+
export function deleteObjectFromS3(url) {
16+
let objectKey = url.split("/").pop();
17+
let params = {
18+
Bucket: `${process.env.S3_BUCKET}`,
19+
Delete: {
20+
Objects: [
21+
{
22+
Key: objectKey,
23+
},
24+
],
25+
},
26+
};
27+
let del = client.deleteObjects(params);
28+
del.on('end', function() {
29+
console.log(`${objectKey} file deleted from S3 bucket`);
30+
});
31+
}

0 commit comments

Comments
 (0)