Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload image to cloudinary #315

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ The illustration above showcases a conversation between two separate tabs opened
JWTEXPIRES=90d
JWTSECRET=<-giveanysecretkey->
JWT_COOKIE_EXPIRES=90
CLOUD_NAME=<CLOUDINARY CLOUD NAME>
CLOUDINARY_API_KEY=<CLOUDINARY API KEY>
CLOUDINARY_API_SECRET=<CLOUD API SECRET>
```

5. Start Backend Server:
Expand Down
6 changes: 3 additions & 3 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const catchAsync=require('../utils/catchAsync');
const AppError = require('../utils/AppError');
const Chat=require('../models/chatModel');
const multer=require('multer');

const uploadPhoto=require('../utils/UploadImage');
const multerstorage=multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,'public/img/user')
Expand Down Expand Up @@ -36,7 +36,6 @@ const filterObj = (obj, ...allowedFields) => {
Object.keys(obj).forEach(el => {
if (allowedFields.includes(el)) newObj[el] = obj[el];
});

return newObj;
};

Expand Down Expand Up @@ -74,7 +73,8 @@ exports.UploadPhoto=catchAsync(async(req,res,next)=>{
let data;
if (req.file)
data= req.file.filename;
const updatedUser = await User.findByIdAndUpdate(req.user.id,{pic:data}, {
const uploadedUri=await uploadPhoto.UploadImage(data);
const updatedUser = await User.findByIdAndUpdate(req.user.id,{pic:uploadedUri}, {
new: true,
runValidators: true
});
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"axios": "^1.5.0",
"bcryptjs": "^2.4.3",
"cloudinary": "^2.5.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand Down
24 changes: 24 additions & 0 deletions backend/utils/UploadImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const cloudinary = require('cloudinary').v2;
const path = require('path');

/**
* UploadImage function - This Function is to upload Image in Cloudinary
* @param {*} data - Upload the Local Image to Cloudinary and return the result
* @returns {String} - Return the Image URL
*/
const UploadImage = async (data) => {
const imagePath = path.join(__dirname, "../public/img/user/", data);
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret:process.env.CLOUDINARY_API_SECRET
});
try{
const result=await cloudinary.uploader.upload(imagePath);
return result.secure_url;
}catch(err){
console.log(err);
}
};

module.exports = { UploadImage };