Skip to content

Commit

Permalink
Added Cloudinary and multer
Browse files Browse the repository at this point in the history
  • Loading branch information
shashwatdwi176 committed Jan 12, 2024
1 parent d3afd4e commit a14d248
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 3 deletions.
184 changes: 183 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
},
"dependencies": {
"bcrypt": "^5.1.1",
"cloudinary": "^1.41.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.0.3",
"mongoose-aggregate-paginate-v2": "^1.0.7"
"mongoose-aggregate-paginate-v2": "^1.0.7",
"multer": "^1.4.5-lts.1"
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const app = express()
}
catch(error){
console.error("ERROR: ",error)
throw err
throw err;
}
})()
Expand Down
16 changes: 16 additions & 0 deletions src/middlewares/multer.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//In this we save the file in our server then we upload it on cloudinary

import multer from "multer";

const storage = multer.diskStorage({
destination: function(req,res, cb){
cb(null , "./public/temp") // in temp all the files are getting stored
},
filename: function (req, file, cb){
cb (null, file.organisation) //the filename has same as orginal filename uploaded by user
}
})

export const upload = multer ({
storage,
})
27 changes: 27 additions & 0 deletions src/utils/cloudinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { v2 as cloudinary } from "cloudinary";
import fs from "fs"; //This id used for file handling it is inbuilt package of node for file operation fs stands for file system

cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});

const uploadOnCloudinary = async (loacalFilePath) => {
try {
if (!loacalFilePath) return null; //check if no file is selected to upload

//upload on cloudinary
const response = await cloudinary.uploader.upload(localFilePath, {
resource_type: "auto", // set the file type can be upload
});
//file uploaded successfully
console.log("File uploaded successfully on cloudinary !!!", reponse.url);
return response;
} catch (error) {
fs.unlinkSync(localFilePath); // remove the locally saved temporary file as the upload operation got failed
return null;
}
};

export { uploadOnCloudinary };

0 comments on commit a14d248

Please sign in to comment.