diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-17 23:40:16 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-17 23:40:39 +0300 |
commit | 1cddff7de99c6b209d35137ceb7cde3045573dc0 (patch) | |
tree | dde301d6529fc1140e413c9cd3db8c4033cff552 | |
parent | c43e6d185b7606ff8c0d038746b9e376e04143b5 (diff) | |
download | which-api-1cddff7de99c6b209d35137ceb7cde3045573dc0.tar.gz |
feat: cleanup after poll deletion
-rw-r--r-- | hooks/deleteImages.ts | 32 | ||||
-rw-r--r-- | services/files/files.class.ts | 14 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 16 |
3 files changed, 60 insertions, 2 deletions
diff --git a/hooks/deleteImages.ts b/hooks/deleteImages.ts new file mode 100644 index 0000000..8bfa47c --- /dev/null +++ b/hooks/deleteImages.ts @@ -0,0 +1,32 @@ +import { HookContext } from '@feathersjs/feathers'; +import Bluebird from 'bluebird'; +import _ from 'lodash'; +import Debug from 'debug'; + +const debug = Debug('s3-reuploads'); + +export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => { + const { + service, + app, + id + } = context; + + const fileService = app.service('files'); + const model = service.Model; + const instance = await model.findOne({ _id: id }); + + Bluebird.map(paths, async (path: string) => { + const url = _.get(instance, path); + + // If image is not from our s3, fetch it! + if (fileService.isS3url(url)) { + debug('Found s3 url! Deleting...'); + const s3Path = fileService.getS3PathFromUrl(url); + await fileService.deleteFile(s3Path); + debug(`Deleted: ${s3Path}`); + } + }); + return context; +}; + diff --git a/services/files/files.class.ts b/services/files/files.class.ts index f2a0960..8308b12 100644 --- a/services/files/files.class.ts +++ b/services/files/files.class.ts @@ -24,7 +24,7 @@ export default class Files { } public isS3url(url: string): boolean { - return url.startsWith(`https://${this.bucket}.s3`); + return url?.startsWith(`https://${this.bucket}.s3`); } public generateS3Path(prefix = '', ext = 'png'): string { @@ -33,6 +33,11 @@ export default class Files { return prefix ? `${prefix}/${fileName}` : fileName; } + public getS3PathFromUrl(url: string): string { + const dotComIndex = url.indexOf('.com'); + return url.slice(dotComIndex + 5); + } + async getUploadUrl(path: string): Promise<string> { // Return signed upload URL return this.s3.getSignedUrl('putObject', { @@ -81,6 +86,13 @@ export default class Files { return this.getDownloadUrl(s3Path); } + async deleteFile(s3Path: string): Promise<void> { + return this.s3.deleteObject({ + Bucket: this.bucket, + Key: s3Path + }).promise(); + } + setup(app: Application): void { this.app = app; this.s3 = new S3({ diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 7853c54..3bc26e8 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -1,5 +1,6 @@ import { HookContext } from '@feathersjs/feathers'; import { disallow } from 'feathers-hooks-common'; +import { NotAuthenticated } from '@feathersjs/errors'; import { Types } from 'mongoose'; import bluebird from 'bluebird'; import _ from 'lodash'; import { Poll } from 'which-types'; @@ -9,6 +10,7 @@ import VoteModel from '../../models/votes/vote.model'; import sortByDate from '../../hooks/sortByDate'; import signAuthority from '../../hooks/signAuthority'; import fetchImages from '../../hooks/fetchImages'; +import deleteImages from '../../hooks/deleteImages'; const convertPoll = async (context: HookContext): Promise<HookContext> => { @@ -44,12 +46,24 @@ const convertPoll = async (context: HookContext): Promise<HookContext> => { return context; }; +const onDelete = async (context: HookContext): Promise<HookContext> => { + const { params: { user }, service, id } = context; + if (id) { + const { author } = await service.get(id); + if (author._id.toString() !== user._id.toString()) { + throw new NotAuthenticated('You can only DELETE your own posts!'); + } + VoteModel.deleteMany({ pollId: id.toString() }); + } + return context; +}; + export default { before: { find: sortByDate, create: signAuthority, - remove: disallow('external'), + remove: [onDelete, deleteImages(['contents.left.url', 'contents.right.url'])], update: disallow('external'), patch: disallow('external') }, |