Created
July 20, 2022 09:50
-
-
Save katendeglory/551f35d9d85880689b1161ca1c7c346d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* --- */ | |
/* ROUTE: webinars */ | |
/* Multer Stuffs */ | |
const uuid = require('uuid'); | |
const cloudinary = require('cloudinary').v2; | |
const { CloudinaryStorage } = require('multer-storage-cloudinary'); | |
const multer = require('multer'); | |
cloudinary.config({ | |
cloud_name: 'xxx', | |
api_key: 'xxx', | |
api_secret: 'xxx' | |
}); | |
const storage = new CloudinaryStorage({ | |
cloudinary, | |
params: { | |
folder: 'products', | |
format: async (req, file) => 'jpeg', // supports promises as well | |
public_id: (req, file) => { | |
return `${file.fieldname}-${Date.now()}-${uuid.v4()}.jpeg` | |
}, | |
}, | |
}); | |
const upload = multer({ storage: storage }); | |
router.post('/', auth.isAdmin, upload.fields([{ name: "poster", maxCount: 1 }]), async (req, res) => { | |
try { | |
const webinar = new Webinar({ | |
...req.body, | |
addedBy: req.user.username, | |
poster: req.files.poster[0].path, | |
}); | |
const saved = await webinar.save(); | |
res.send({ ...saved._doc }); | |
} catch (err) { | |
APIError.res(res, err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment