aboutsummaryrefslogtreecommitdiffstats
path: root/lib/upload.js
blob: 452bf5c989a58875a45c443712303035d755f92a (plain) (blame)
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
const multer = require("multer");

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "uploads");
  },
  filename: async (req, file, cb) => {
    const parts = file.originalname.split(".");
    const ext = parts.length > 1 ? parts[parts.length - 1] : "bin";
    cb(null, `${req.user.username}-${file.fieldname}.${ext}`);
  }
});

upload = multer({
  storage,
  onFileUploadStart: (file, req, res) => {
    console.log("CHECKING SIZE")
    const maxSize = 32 * 1000 * 1000;
    if (req.files.file.length > maxSize) {
      return false;
    }
  }
});

app.post(
  "/profile",
  auth,
  upload.single("squirt"),
  ah(async (req, res) => {
    let { filename } = req.file;
    res.send({ filename });
  })
);