diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-12 19:09:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-12 19:09:54 +0300 |
commit | 5462174ca076aef53d06b92372141c665c923ea3 (patch) | |
tree | 1bbe4629b0a0ee537a21be15c91fd31b243df97d /hooks/fetchImages.ts | |
parent | c2e16321e679d52ad9d6e08b5cdb785b172ad830 (diff) | |
parent | 8f4ae4fd89cab5ba4f9e8d2750bc8589ce997ff1 (diff) | |
download | which-api-5462174ca076aef53d06b92372141c665c923ea3.tar.gz |
Merge pull request #19 from which-ecosystem/s3-reuploads
S3 reuploads
Diffstat (limited to 'hooks/fetchImages.ts')
-rw-r--r-- | hooks/fetchImages.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/hooks/fetchImages.ts b/hooks/fetchImages.ts new file mode 100644 index 0000000..44aac6c --- /dev/null +++ b/hooks/fetchImages.ts @@ -0,0 +1,31 @@ +import { HookContext } from '@feathersjs/feathers'; +import Bluebird from 'bluebird'; +import _ from 'lodash'; + + +export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => { + const { + service, + app, + result, + params: { user } + } = context; + + const fileService = app.service('files'); + const model = service.Model; + + Bluebird.map(paths, async (path: string) => { + const url = _.get(result, path); + + // If image is not from our s3, fetch it! + if (!fileService.isS3url(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 }); + } + return url; + }); + return context; +}; + |