aboutsummaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-08-12 21:17:39 +0300
committerGitHub <noreply@github.com>2020-08-12 21:17:39 +0300
commit3e966272b855b4c167582abf21439429d11c2c8b (patch)
tree0425766e8fa6bad616f687e660e2469e914c6a98 /hooks
parent5462174ca076aef53d06b92372141c665c923ea3 (diff)
parent4194a35550bb962563f447b91811759f0d291f75 (diff)
downloadwhich-api-3e966272b855b4c167582abf21439429d11c2c8b.tar.gz
Merge pull request #20 from which-ecosystem/s3-reuploads
Improve reuploads and add script
Diffstat (limited to 'hooks')
-rw-r--r--hooks/fetchImages.ts7
1 files changed, 6 insertions, 1 deletions
diff --git a/hooks/fetchImages.ts b/hooks/fetchImages.ts
index 44aac6c..a7eb30c 100644
--- a/hooks/fetchImages.ts
+++ b/hooks/fetchImages.ts
@@ -1,7 +1,9 @@
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 {
@@ -19,10 +21,13 @@ export default (paths: string[]) => async (context: HookContext): Promise<HookCo
// If image is not from our s3, fetch it!
if (!fileService.isS3url(url)) {
+ debug('Found non-s3 url!');
const filePath = await fileService.downloadFile(url);
const s3Path = fileService.generateS3Path(user?.username);
const s3Url = await fileService.uploadFileToS3(filePath, s3Path);
- return model.findOneAndUpdate({ _id: result._id }, { [path]: s3Url });
+ await model.findOneAndUpdate({ _id: result._id }, { $set: { [path]: s3Url } });
+ debug(`Fetched and updated: from ${url} to ${s3Url}`);
+ return s3Url;
}
return url;
});