file.controller.ts 649 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import formidable from "formidable";
import { asyncWrap } from "../helpers/asyncWrap";
3
import { TypedRequest } from "../types";
Yoon, Daeki's avatar
Yoon, Daeki committed
4

5
6
7
export const uploadAvatar = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequest;
  const form = formidable({ multiples: false, uploadDir: "uploads" });
Yoon, Daeki's avatar
Yoon, Daeki committed
8
9
10
11
12
13
14
15

  await new Promise((resolve, reject) => {
    form.parse(req, (err, fields, files) => {
      if (err) {
        reject(err);
        return;
      }

16
17
18
19
      // console.log("fields", fields);
      // console.log("files", files);
      req.body = fields;
      req.files = files;
Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
22
23
24
25
26

      resolve(files);
    });
  });
  next();
  return;
});