Skip to content

Commit 5097e7b

Browse files
authored
Merge pull request #21 from which-ecosystem/deletions
Cleanup after poll deletion
2 parents c43e6d1 + 1cddff7 commit 5097e7b

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

hooks/deleteImages.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { HookContext } from '@feathersjs/feathers';
2+
import Bluebird from 'bluebird';
3+
import _ from 'lodash';
4+
import Debug from 'debug';
5+
6+
const debug = Debug('s3-reuploads');
7+
8+
export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => {
9+
const {
10+
service,
11+
app,
12+
id
13+
} = context;
14+
15+
const fileService = app.service('files');
16+
const model = service.Model;
17+
const instance = await model.findOne({ _id: id });
18+
19+
Bluebird.map(paths, async (path: string) => {
20+
const url = _.get(instance, path);
21+
22+
// If image is not from our s3, fetch it!
23+
if (fileService.isS3url(url)) {
24+
debug('Found s3 url! Deleting...');
25+
const s3Path = fileService.getS3PathFromUrl(url);
26+
await fileService.deleteFile(s3Path);
27+
debug(`Deleted: ${s3Path}`);
28+
}
29+
});
30+
return context;
31+
};
32+

services/files/files.class.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class Files {
2424
}
2525

2626
public isS3url(url: string): boolean {
27-
return url.startsWith(`https://${this.bucket}.s3`);
27+
return url?.startsWith(`https://${this.bucket}.s3`);
2828
}
2929

3030
public generateS3Path(prefix = '', ext = 'png'): string {
@@ -33,6 +33,11 @@ export default class Files {
3333
return prefix ? `${prefix}/${fileName}` : fileName;
3434
}
3535

36+
public getS3PathFromUrl(url: string): string {
37+
const dotComIndex = url.indexOf('.com');
38+
return url.slice(dotComIndex + 5);
39+
}
40+
3641
async getUploadUrl(path: string): Promise<string> {
3742
// Return signed upload URL
3843
return this.s3.getSignedUrl('putObject', {
@@ -81,6 +86,13 @@ export default class Files {
8186
return this.getDownloadUrl(s3Path);
8287
}
8388

89+
async deleteFile(s3Path: string): Promise<void> {
90+
return this.s3.deleteObject({
91+
Bucket: this.bucket,
92+
Key: s3Path
93+
}).promise();
94+
}
95+
8496
setup(app: Application): void {
8597
this.app = app;
8698
this.s3 = new S3({

services/polls/polls.hooks.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HookContext } from '@feathersjs/feathers';
22
import { disallow } from 'feathers-hooks-common';
3+
import { NotAuthenticated } from '@feathersjs/errors';
34
import { Types } from 'mongoose';
45
import bluebird from 'bluebird'; import _ from 'lodash';
56
import { Poll } from 'which-types';
@@ -9,6 +10,7 @@ import VoteModel from '../../models/votes/vote.model';
910
import sortByDate from '../../hooks/sortByDate';
1011
import signAuthority from '../../hooks/signAuthority';
1112
import fetchImages from '../../hooks/fetchImages';
13+
import deleteImages from '../../hooks/deleteImages';
1214

1315

1416
const convertPoll = async (context: HookContext): Promise<HookContext> => {
@@ -44,12 +46,24 @@ const convertPoll = async (context: HookContext): Promise<HookContext> => {
4446
return context;
4547
};
4648

49+
const onDelete = async (context: HookContext): Promise<HookContext> => {
50+
const { params: { user }, service, id } = context;
51+
if (id) {
52+
const { author } = await service.get(id);
53+
if (author._id.toString() !== user._id.toString()) {
54+
throw new NotAuthenticated('You can only DELETE your own posts!');
55+
}
56+
VoteModel.deleteMany({ pollId: id.toString() });
57+
}
58+
return context;
59+
};
60+
4761

4862
export default {
4963
before: {
5064
find: sortByDate,
5165
create: signAuthority,
52-
remove: disallow('external'),
66+
remove: [onDelete, deleteImages(['contents.left.url', 'contents.right.url'])],
5367
update: disallow('external'),
5468
patch: disallow('external')
5569
},

0 commit comments

Comments
 (0)