diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-12 18:42:29 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-12 18:42:29 +0300 |
commit | 4b2397d68b62d9f5682d1fd9f4fb2082ac4aa260 (patch) | |
tree | 98ee0ebb37ba576ba1c0faad75fed76ae4c3a022 | |
parent | f08e2a37677e711cad7397c532670913645f010a (diff) | |
download | which-api-4b2397d68b62d9f5682d1fd9f4fb2082ac4aa260.tar.gz |
feat: impelemnt fetchImages hook
-rw-r--r-- | hooks/fetchImages.ts | 25 | ||||
-rw-r--r-- | services/polls/polls.hooks.ts | 4 | ||||
-rw-r--r-- | services/users/users.hooks.ts | 3 |
3 files changed, 31 insertions, 1 deletions
diff --git a/hooks/fetchImages.ts b/hooks/fetchImages.ts new file mode 100644 index 0000000..d6e7d27 --- /dev/null +++ b/hooks/fetchImages.ts @@ -0,0 +1,25 @@ +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; +}; + diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts index 35eae29..7a5b1da 100644 --- a/services/polls/polls.hooks.ts +++ b/services/polls/polls.hooks.ts @@ -8,6 +8,7 @@ import { PollSchema } from '../../models/polls/poll.schema'; import VoteModel from '../../models/votes/vote.model'; import sortByDate from '../../hooks/sortByDate'; import signAuthority from '../../hooks/signAuthority'; +import fetchImages from '../../hooks/fetchImages'; const convertPoll = async (context: HookContext): Promise<HookContext> => { @@ -53,7 +54,8 @@ export default { patch: disallow('external') }, after: { - all: convertPoll + all: convertPoll, + create: fetchImages(['contents.left.url', 'contents.right.url']) } }; diff --git a/services/users/users.hooks.ts b/services/users/users.hooks.ts index 29f1074..ddfc47f 100644 --- a/services/users/users.hooks.ts +++ b/services/users/users.hooks.ts @@ -4,6 +4,7 @@ import { discard, disallow } from 'feathers-hooks-common'; import { HookContext } from '@feathersjs/feathers'; import { NotAuthenticated } from '@feathersjs/errors'; import requireAuth from '../../hooks/requireAuth'; +import fetchImages from '../../hooks/fetchImages'; const hashPassword = hooks.hashPassword('password'); @@ -24,6 +25,8 @@ const compareUser = async (context: HookContext): Promise<HookContext> => { export default { after: { all: hooks.protect('password'), + create: fetchImages(['avatarUrl']), + patch: fetchImages(['avatarUrl']), get: discard('password') // Protect password from local get's }, before: { |