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

Cloudinary setup #53

Closed
wants to merge 5 commits into from
Closed
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
112 changes: 112 additions & 0 deletions server/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const Profile = require("../models/Profile");
const asyncHandler = require("express-async-handler");

// @route POST /profile/create
// @desc Create new profile
// @access Public
exports.createProfile = asyncHandler(async (req, res, next) => {

const {
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
} = req.body;

const profile = await Profile.create({
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
});

res.status(201).json({
profile,
});
});

// @route PUT /profile
// @desc updates a profile with given ID
// @access Private
exports.updateProfile = asyncHandler(async (req, res, next) => {
const id = req.user._id;

const {
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
} = req.body;

const profile = await Profile.findOneAndUpdate(
{ user: id },
{
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
},
{ new: true }
);

res.status(200).json({
profile,
});
});

// @route GET /profile
// @desc gets a profile with the given ID
// @access Private
exports.getProfile = asyncHandler(async (req, res, next) => {
const id = req.user._id;
const profile = await Profile.findOne({ user: id });

if (!profile) {
res.status(404);
throw new Error("The profile does not exist");
}

res.status(200).json({
profile,
});
});

// @route GET /profiles
// @desc gets all profiles
// @access Private
exports.getAllProfiles = asyncHandler(async (req, res, next) => {
const profiles = await Profile.find();

if (!profiles) {
res.status(500);
throw new Error("0 results");
}

res.status(200).json({
profiles,
});
});
30 changes: 30 additions & 0 deletions server/middleware/multer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const multer = require("multer");
const path = require("path");

const storage = multer.diskStorage({});

const upload = multer({
storage,
limit: { fileSize: 1000000 },
fileFilter: (req, file, cb) => {
checkFiletype(file, cb);
},
});

const checkFiletype = (file, cb) => {
const { originalname, mimetype } = file;
const validExtName = /jpeg|jpg|png|gif/;

const fileTypeCheck = validExtName.test(
path.extname(originalname).toLocaleLowerCase()
);
const mimeTypeCheck = validExtName.test(mimetype);

if (fileTypeCheck && mimeTypeCheck) {
return cb(null, true);
} else {
return cb(null, false);
}
};

module.exports = upload;
13 changes: 13 additions & 0 deletions server/utils/cloudinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const cloudinary = require("cloudinary").v2;
require('dotenv').config();

const { CLOUDINARY_API_NAME, CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET } = process.env;

cloudinary.config({
cloud_name: CLOUDINARY_API_NAME,
api_key: CLOUDINARY_API_KEY,
api_secret: CLOUDINARY_API_SECRET,
secure:true
});

module.exports = { cloudinary };