Advertisement
Guest User

StorageCloudinary

a guest
Feb 18th, 2020
1,335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Storage.js
  2. // add storage.js to your app.use()
  3.  
  4. var multer = require('multer');
  5. const cloudinary = require("cloudinary");
  6. const cloudinaryStorage = require("multer-storage-cloudinary");
  7. const {
  8.    CLOUD_NAME,
  9.    API_KEY,
  10.    API_SECRET
  11. } = require('../config');
  12.  
  13. const router = express.Router()
  14.  
  15. // Account access information
  16. cloudinary.config({
  17.    cloud_name: CLOUD_NAME,
  18.    api_key: API_KEY,
  19.    api_secret: API_SECRET
  20. });
  21. // Uploading Image Configuration
  22. const storage = cloudinaryStorage({
  23.    cloudinary: cloudinary,
  24.    folder: "images",
  25.    allowedFormats: ["jpg", "png"],
  26.    transformation: [
  27.       { if: "w_gt_1900", width: 1900, crop: "scale" },
  28.       { if: "h_gt_1900", height: 1900, crop: "scale" },
  29.       { quality: "auto" },
  30.       { format: 'jpg' }
  31.    ]
  32. });
  33. const parser = multer({ storage: storage });
  34.  
  35. router.post('/upload', parser.single("file"), (req, res) => {
  36.    const imageUUID = req.file.public_id;
  37.    
  38.    //Code to store imageUUID in your database
  39.    
  40.    res.json(imageUUID); // Return the UUID to the front end like this if necessary
  41. });
  42.  
  43. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement