aboutsummaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-08-17 23:42:34 +0300
committerGitHub <noreply@github.com>2020-08-17 23:42:34 +0300
commit5097e7b05d260b378f315aa48aa577692b0495dd (patch)
treedde301d6529fc1140e413c9cd3db8c4033cff552 /hooks
parentc43e6d185b7606ff8c0d038746b9e376e04143b5 (diff)
parent1cddff7de99c6b209d35137ceb7cde3045573dc0 (diff)
downloadwhich-api-5097e7b05d260b378f315aa48aa577692b0495dd.tar.gz
Merge pull request #21 from which-ecosystem/deletions
Cleanup after poll deletion
Diffstat (limited to 'hooks')
-rw-r--r--hooks/deleteImages.ts32
1 files changed, 32 insertions, 0 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;
+};
+