eletrotupi / tcc/ api/src/services/avatar.tsc901c35af9c84b6a29e5185545ced1129f33b8f4
860 B | 37 lines Raw
1
import s3 from '@app/lib/s3';
2
import multerBase from 'multer';
3
import multerS3 from 'multer-s3';
4
import path from 'path';
5
import crypto from 'crypto';
6

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

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

32
      cb(null, `avatars/${uuid}.${ext}`)
33
    },
34
  })
35
})
36

37
export default avatarMulter;