diff --git a/src/controllers/MediaController.ts b/src/controllers/MediaController.ts new file mode 100644 index 0000000..f215cd6 --- /dev/null +++ b/src/controllers/MediaController.ts @@ -0,0 +1,49 @@ +import Organization from '../models/Organization' +import * as express from 'express' +import aws from 'aws-sdk' +import multer from 'multer' +import multerS3 from 'multer-s3' + +export default class MediaController { + static uploadRoute(): express.RequestHandler[] { + // + aws.config.update({ + secretAccessKey: process.env.S3_SECRET_KEY, + accessKeyId: process.env.S3_ACCESS_KEY, + region: 'fr-par', + httpOptions: { + timeout: 4 * 60 * 1000, + } + }) + + const s3 = new aws.S3({ + endpoint: 's3.fr-par.scw.cloud' + }) + + return [ + multer({ + storage: multerS3({ + s3: s3, + bucket: 'development-bucket', + acl: 'public-read', + contentType: multerS3.AUTO_CONTENT_TYPE, + key: (req: any, file: any, cb: any) => { + console.log(file) + cb(null, Date.now().toString() + '_' + file.originalname) //use Date.now() for unique file keys + } + }) + }).single('file'), + this.upload + ] + } + + static upload(req: express.Request, res: express.Response) { + + + + res.json({ + success: true, + data: { file: req.file } + }) + } +} \ No newline at end of file