fileinfo.controller.ts 1.12 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import formidable from "formidable";
import { asyncWrap } from "../helpers/asyncWrap";
import { TypedRequest } from "../types";

export const uploadFile = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequest;
  const form = formidable({ multiples: false, uploadDir: "uploads" });

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

export const uploadFiles = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequest;
  const form = formidable({ multiples: true, uploadDir: "uploads" });

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