forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
48 lines (42 loc) · 1.1 KB
/
Copy pathupload.js
File metadata and controls
48 lines (42 loc) · 1.1 KB
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
import multer from "multer";
import { APIError } from "./errorHandler.js";
const storage = multer.memoryStorage();
const imageFilter = (req, file, cb) => {
if (file.mimetype.startsWith("image/")) {
cb(null, true);
} else {
cb(new APIError("Not an image! Please upload only images.", 400));
}
};
const bookFilter = (req, file, cb) => {
if (
file.fieldname === "thumbnail" &&
file.mimetype.startsWith("image/")
) {
cb(null, true);
} else if (
file.fieldname === "file" &&
(file.mimetype === "application/pdf" ||
file.mimetype === "application/epub+zip")
) {
cb(null, true);
} else {
cb(new APIError("Invalid file format. Thumbnail must be an image, file must be PDF/EPUB.", 400));
}
};
export const uploadImage = multer({
storage,
limits: {
fileSize: 5 * 1024 * 1024, // 5MB limit
},
fileFilter: imageFilter,
});
export const uploadBook = multer({
storage,
limits: {
fileSize: 50 * 1024 * 1024, // 50MB limit
},
fileFilter: bookFilter,
});
// Default export for backward compatibility where not yet replaced
export default uploadImage;