-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
103 lines (88 loc) · 2.54 KB
/
index.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require("dotenv").config();
const express = require("express");
const multer = require("multer");
const { s3Uploadv2, s3Uploadv3 } = require("./s3Service");
const uuid = require("uuid").v4;
const app = express();
//single file upload
// const upload = multer({ dest: "uploads/" });
// app.post("/upload", upload.single("file"), (req, res) => {
// res.json({ status: "success" });
// });
// multiple file uploads
// const upload = multer({ dest: "uploads/" });
// app.post("/upload", upload.array("file", 2), (req, res) => {
// res.json({ status: "success" });
// });
// multiple fields upload
// const upload = multer({ dest: "uploads/" });
// const multiUpload = upload.fields([
// { name: "avatar", maxCount: 1 },
// { name: "resume", maxCount: 1 },
// ]);
// app.post("/upload", multiUpload, (req, res) => {
// console.log(req.files);
// res.json({ status: "success" });
// });
// custom filename
// const storage = multer.diskStorage({
// destination: (req, file, cb) => {
// cb(null, "uploads");
// },
// filename: (req, file, cb) => {
// const { originalname } = file;
// cb(null, `${uuid()}-${originalname}`);
// },
// });
const storage = multer.memoryStorage();
const fileFilter = (req, file, cb) => {
if (file.mimetype.split("/")[0] === "image") {
cb(null, true);
} else {
cb(new multer.MulterError("LIMIT_UNEXPECTED_FILE"), false);
}
};
// ["image", "jpeg"]
const upload = multer({
storage,
fileFilter,
limits: { fileSize: 1000000000, files: 2 },
});
// app.post("/upload", upload.array("file"), async (req, res) => {
// try {
// const results = await s3Uploadv2(req.files);
// console.log(results);
// return res.json({ status: "success" });
// } catch (err) {
// console.log(err);
// }
// });
app.post("/upload", upload.array("file"), async (req, res) => {
try {
const results = await s3Uploadv3(req.files);
console.log(results);
return res.json({ status: "success" });
} catch (err) {
console.log(err);
}
});
app.use((error, req, res, next) => {
if (error instanceof multer.MulterError) {
if (error.code === "LIMIT_FILE_SIZE") {
return res.status(400).json({
message: "file is too large",
});
}
if (error.code === "LIMIT_FILE_COUNT") {
return res.status(400).json({
message: "File limit reached",
});
}
if (error.code === "LIMIT_UNEXPECTED_FILE") {
return res.status(400).json({
message: "File must be an image",
});
}
}
});
app.listen(4000, () => console.log("listening on port 4000"));