From 1cddff7de99c6b209d35137ceb7cde3045573dc0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 17 Aug 2020 23:40:16 +0300 Subject: feat: cleanup after poll deletion --- hooks/deleteImages.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 hooks/deleteImages.ts (limited to 'hooks') 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 => { + 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; +}; + -- cgit v1.2.3