eletrotupi / tcc/ commit / ad68b5c

api: add file limitations for the avatar service + content disposition

Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago ad68b5c88b71e8eee79a79e79609ea7618520328 Browse files
Parents 387edc2
Changed files
1
Additions
+16
Deletions
-3
api/src/services/avatar.ts
1 1
import s3 from '@app/lib/s3';
2 2
import multerBase from 'multer';
3 3
import multerS3 from 'multer-s3';
4 4
import path from 'path';
5 5
import crypto from 'crypto';
6 6

7 7
const avatarMulter = multerBase({
8 +
  limits: { fileSize: 8 * 1024 * 1024 }, // 8MB
9 +
  fileFilter: (_req, file, cb) => {
10 +
    const allowed = [
11 +
      'image/jpeg',
12 +
      'image/png',
13 +
      'image/webp',
14 +
      'image/jpg'
15 +
    ];
16 +

17 +
    cb(null, allowed.includes(file.mimetype));
18 +
  },
19 +

8 20
  storage: multerS3({
9 21
    s3,
10 22
    acl: "public-read",
23 +
    contentDisposition: "inline",
11 24
    bucket: process.env.S3_BUCKET!,
12 -
    metadata: function (req, file, cb) {
25 +
    metadata: (req, file, cb) => {
13 26
      cb(null, { fieldName: file.fieldname });
14 27
    },
15 -
    key: function (req, file, cb) {
28 +
    key: (req, file, cb) => {
16 29
      const ext = path.extname(file.originalname).slice(1);
17 30
      const uuid = crypto.randomUUID();
18 31

19 32
      cb(null, `avatars/${uuid}.${ext}`)
20 -
    }
33 +
    },
21 34
  })
22 35
})
23 36

24 37
export default avatarMulter;