diff options
author | eug-vs <eug-vs@keemail.me> | 2020-08-10 23:13:56 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-08-10 23:13:56 +0300 |
commit | 0efc3134eb348e68e7bf7519e770237e15811900 (patch) | |
tree | 6e966cb1fcf32a84a87637fd8d07277995d3cf81 | |
parent | 9ac1a7e583aab2fb7640269e0257ac56fdcf1877 (diff) | |
download | which-api-0efc3134eb348e68e7bf7519e770237e15811900.tar.gz |
feat: create Files service for s3 uploads
-rw-r--r-- | services/files/files.class.ts | 33 | ||||
-rw-r--r-- | services/files/files.service.ts | 7 | ||||
-rw-r--r-- | services/index.ts | 2 |
3 files changed, 42 insertions, 0 deletions
diff --git a/services/files/files.class.ts b/services/files/files.class.ts new file mode 100644 index 0000000..8a104b7 --- /dev/null +++ b/services/files/files.class.ts @@ -0,0 +1,33 @@ +import { Application } from '@feathersjs/express'; +import { Params } from '@feathersjs/feathers'; +import { S3 } from 'aws-sdk'; +import { v4 as uuidv4 } from 'uuid'; + + +export default class Files { + app!: Application; + s3!: S3; + bucket!: string; + + async find(params: Params): Promise<string> { + // Return signed upload URL + return this.s3.getSignedUrl('putObject', { + Bucket: this.bucket, + Key: `${params.user?.username}/${uuidv4()}.png`, + ContentType: 'image/*', + Expires: 300, + }); + } + + setup(app: Application): void { + this.app = app; + this.s3 = new S3({ + accessKeyId: process.env.ACCESSKEYID, + secretAccessKey: process.env.SECRETACCESSKEY, + signatureVersion: 'v4', + region: 'eu-central-1' + }); + this.bucket = process.env.BUCKET || ''; + } +} + diff --git a/services/files/files.service.ts b/services/files/files.service.ts new file mode 100644 index 0000000..60461a4 --- /dev/null +++ b/services/files/files.service.ts @@ -0,0 +1,7 @@ +import { Application } from '@feathersjs/express'; +import Files from './files.class'; + +export default (app: Application): void => { + app.use('/files', new Files()); +}; + diff --git a/services/index.ts b/services/index.ts index e5ea703..f8ec3a5 100644 --- a/services/index.ts +++ b/services/index.ts @@ -6,6 +6,7 @@ import Votes from './votes/votes.service'; import Auth from './auth/auth.service'; import Feed from './feed/feed.service'; import Feedback from './feedback/feedback.service'; +import Files from './files/files.service'; import tryAuthenticate from '../hooks/tryAuthenticate'; import logging from '../hooks/logging'; @@ -19,6 +20,7 @@ export default (app: Application): void => { app.configure(Votes); app.configure(Feed); app.configure(Feedback); + app.configure(Files); app.hooks({ before: { |