-
Notifications
You must be signed in to change notification settings - Fork 0
/
help.js
60 lines (50 loc) · 1.22 KB
/
help.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
//model
photo = {
publicId: { type: String },
url: { type: String },
};
//cloudinary configuration
cloudinary.config({
cloud_name: config.get("cloudinary.cloudName"),
api_key: config.get("cloudinary.apiKey"),
api_secret: config.get("cloudinary.apiSecret"),
});
//cloudinary storage instantiated
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: "BOOK_TREKKER/PRODUCTS",
},
});
//multer file filter (only images are allowed)
const fileFilter = (req, file, cb) => {
if (
file.mimetype == "image/png" ||
file.mimetype == "image/jpg" ||
file.mimetype == "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
return cb(new AppError("Only .png, .jpg and .jpeg format allowed!", 400));
}
};
//file size limit to 1MB
const maxSize = 1 * 1024 * 1024;
const limits = { fileSize: maxSize };
//multer option objects
exports.multerOptions = {
storage: storage,
fileFilter,
limits,
};
//create
exports.createProduct = catchAsync(async (req, res) => {
const product = new Product(req.productData);
await product.save();
const newProduct = await Product.findById(product._id).populate(
"category",
"name _id"
);
res.send(newProduct);
});