Skip to content

Commit

Permalink
Cloudinary & Loading Feature Added
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavshukla committed Jan 9, 2021
1 parent 9d10a65 commit ac3b720
Show file tree
Hide file tree
Showing 24 changed files with 253 additions and 131 deletions.
39 changes: 20 additions & 19 deletions cloudinaryUpload.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
require('dotenv').load();
var fs = require('fs');
require('dotenv').config();
var cloudinary = require('cloudinary').v2;

var uploads = {};
// File upload
async function UploadImageByCloudinary(imgData){
console.log(imgData);
try{
const image = await cloudinary.uploader.upload(imgData);
// console.log();
// console.log("** File Upload");
// console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
// console.log("* " + image.public_id);
// console.log("* " + image.url);
// waitForAllUploads("pizza", err, image);
return image.url;
}
catch(err){
console.log(err);
};
}

// set your env variable CLOUDINARY_URL or set the following configuration
/* cloudinary.config({
cloud_name: '',
api_key: '',
api_secret: ''
}); */
// UploadImageByCloudinary("aquaman.jpg");

console.log("** ** ** ** ** ** ** ** ** Uploads ** ** ** ** ** ** ** ** ** **");

// File upload
cloudinary.v2.uploader.upload('pizza.jpg', { tags: 'basic_sample' }, function (err, image) {
console.log();
console.log("** File Upload");
if (err) { console.warn(err); }
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("pizza", err, image);
});
module.exports = {UploadImageByCloudinary:UploadImageByCloudinary};
21 changes: 12 additions & 9 deletions controller/download/download_img.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const Vision = require("../../models/vision");
const util = require("util");
const download = require('image-downloader')


class DownLoad {
Expand All @@ -10,7 +11,7 @@ class DownLoad {
//mke folder of given genre
let FS_MKDIR = util.promisify(fs.mkdir);
try{
await FS_MKDIR(path.join("C:/Users/shubham/Desktop/", genre))
await FS_MKDIR(path.join("/home/utsav/Desktop", genre))
.then((user) => {
console.log('Directory created successfully!')
})
Expand All @@ -26,27 +27,29 @@ class DownLoad {

for(let i = 0;i<allimagesData.length;i++){
let img_name = allimagesData[i]["img_name"].substr(allimagesData[0]["img_name"].indexOf("-")+1);
let img_buffer = allimagesData[i]["img_buffer"];
// let img_buffer = allimagesData[i]["img_buffer"];

const options = {
url: img_name,
dest: "/home/utsav/Desktop/" + genre // will be saved to /path/to/dest/image.jpg
}

let FS_WRITEFILE = util.promisify(fs.writeFile);
try{
await FS_WRITEFILE(path.join("C:/Users/shubham/Desktop/"+genre,img_name), img_buffer,"base64")
.then(() => {
// console.log('file saved to ', img_name)
})
const filename = download.image(options);
// console.log(filename);
}
catch(err){
console.log(err);
console.log("err while downloading");
}
}
}

handleRequest(req, res) {
console.log(req)
// console.log(req)
const user_id = req.params.user_id;
const genreList = req.params.genres.split(',');
console.log(user_id,genreList);
// console.log(user_id,genreList);
try{
Vision.find({user_id : user_id})
.then(async (user)=>{
Expand Down
38 changes: 21 additions & 17 deletions controller/image/addImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ const fs = require("fs");
const Vision = require("../../models/vision");
const {get_features} = require('../../predictGeneral');
// const {get_nsfw_features} = require('../../predictNSFW');

const {UploadImageByCloudinary} = require("../../cloudinaryUpload");
const util = require('util');


//for multiple file

class AddImage {
Expand All @@ -130,47 +129,51 @@ class AddImage {

async process_image(user,element,req){

// console.log(user);
// console.log(element);
let promises = [];
promises.push(UploadImageByCloudinary(element.path));
promises.push(get_features(element.path));
const promiseResult = await Promise.all(promises);
// console.log(promiseResult);

let features = await get_features(element.path);
let feature_list = features.slice(0,10);;
const cloudinaryResult = promiseResult[0];
let features = promiseResult[1];
let feature_list = features.slice(0,10);
// let nsfw_features = await get_nsfw_features(element.path);
// console.log(nsfw_features);
// console.log(nsfw_features[0].value);
// console.log(nsfw_features[1].value);

await this.getStuff(element.path).then(async(data)=>{

let image_mapping = user[0].image;
let image_mapping = user[0].image;
// console.log(image_mapping);

// image_mapping.push({img_name : element.filename,img_label : feature_list,img_data:data});
image_mapping.push({img_name : element.filename,img_label : feature_list});
image_mapping.push({img_name : cloudinaryResult,img_label : feature_list});


//for labels
let label_mapping = user[0].image_labels;
if(label_mapping.length == 0){
label_mapping.push({name : "All" ,label : {img_name : element.filename,img_buffer : data}});
label_mapping.push({name : "All" ,label : {img_name : cloudinaryResult}});
}
else{
let found = label_mapping.find((ele)=>{return (ele.name === 'All')});
if(found !== undefined){
found.label.push({img_name : element.filename,img_buffer : data});
found.label.push({img_name : cloudinaryResult});
}
}

feature_list.forEach(function (item) {

let found = label_mapping.find((ele)=>{return (ele.name === item.name)});
if(found !== undefined){
found.label.push({img_name : element.filename,img_buffer : data});
found.label.push({img_name : cloudinaryResult});
}
else{
label_mapping.push({name : item["name"] ,label : {img_name : element.filename,img_buffer : data}});
label_mapping.push({name : item["name"] ,label : {img_name : cloudinaryResult}});
}
});

// console.log(image_mapping,label_mapping);
const user_id = req.body.user_id;
Vision.updateOne({user_id : user_id},{
$set: { 'image': image_mapping ,
Expand All @@ -188,14 +191,14 @@ class AddImage {


const user_id = req.body.user_id;
// console.log(req.files);
// console.log(req.body);

try{
Vision.find({user_id : user_id})
.then(async(user)=>{

// console.log(user);
let promise = [];
req.files.forEach((element)=>{
req.files.forEach(async (element)=>{
promise.push(this.process_image(user,element,req)
.catch((err)=>{
console.log(err,"error while inserting image/s");
Expand All @@ -208,6 +211,7 @@ class AddImage {

let found_label = user[0].image_labels;
let found_filter_label = found_label.find((ele)=>{return (ele.name === "All")});
// console.log(found_filter_label);
return res.status(200).json(found_filter_label["label"]);

})
Expand Down
2 changes: 1 addition & 1 deletion controller/image/getAllImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GetAllImage{
let label_mapping = user[0].image_labels;
let found = label_mapping.find((ele)=>{return (ele.name === req.params.label)});
// console.log(found.label);
if(found === undefined){return res.status(404).send("Label is not found");}
if(found === undefined){return res.status(200).json({"image" : []});}
let found_label = found.label;
// return res.status(200).send(found_label);
return res.status(200).json({"image":found_label})
Expand Down
4 changes: 2 additions & 2 deletions controller/user/logIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LogIn{
// console.log(email + password);
User.find({ email: email }, (err, user) => {
if (err || user.length === 0)
return res.status(200).json({msg : 'Invalid Email',status : false});
return res.status(200).json({msg : "Incorrect Username or Password!",status : false});
else if (user.length > 0) {
//Comparing password
bcrypt.compare(password, user[0].password, (_err, result) => {
Expand All @@ -30,7 +30,7 @@ class LogIn{
});

} else
return res.status(200).json({msg : 'Incorrect Password!',status : false});
return res.status(200).json({msg : 'Incorrect Username or Password!',status : false});
});
}
}).catch((error) => {return res.status(500).json({msg : error})});
Expand Down
2 changes: 1 addition & 1 deletion controller/user/updateDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class UpdateDetail{
// console.log(user[0].password);
// console.log(res !== user[0].password);
if(result == false){
return res.status(200).json({"msg" : "Incorrect current password"});
return res.status(200).json({"msg" : "Incorrect Username or Password!"});
}
else{
//hash New password
Expand Down
24 changes: 9 additions & 15 deletions controller/user/uploadProfileImage.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
const util = require('util');
const fs = require("fs");
const User = require("../../models/user");

const {UploadImageByCloudinary} = require("../../cloudinaryUpload");
const cloudinaryUpload = require('../../cloudinaryUpload');

class UploadProfileImage {

handleRequest(req, res) {
// console.log(req.body);
let FS_READFILE = util.promisify(fs.readFile);
async handleRequest(req, res) {
// console.log(req.body);
const cloudinaryResult = await UploadImageByCloudinary(req.file.path) ;
// let FS_READFILE = util.promisify(fs.readFile);
try{
FS_READFILE(req.file.path)
.then((data)=>{
const user_id = req.body.user_id;
User.updateOne({_id : user_id},{
$set: { "user_image" : data}
}).then((user)=>{
console.log(req.file.filename);

return res.status(200).json({"user_image" : data,msg : "Profile Image Updated"});
});
});
const user_id = req.body.user_id;
User.updateOne({_id : user_id},{$set: { "user_image" : cloudinaryResult}}).exec();
return res.status(200).json({"user_image" : cloudinaryResult,msg : "Profile Image Updated"});
}
catch(err){
console.log('error while adding profile image');
Expand Down
2 changes: 1 addition & 1 deletion controller/user/userDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class UserDetail{
// console.log(req.params);
User.find({_id : req.params.user_id})
.then((user) =>{
// console.log(user);
// console.log(user);
return res.status(200).json({"username" : user[0].username,"email" : user[0].email ,
"user_image" : user[0].user_image});
}
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ export const PROFILE_UPDATE_ERROR = "PROFILE_UPDATE_ERROR";
export const FORGOT_VERIFY_SUCCESS = "FORGOT_VERIFY_SUCCESS";
export const FORGOT_VERIFY_ERROR = "FORGOT_VERIFY_ERROR";
export const FORGOT_UPDATE_SUCCESS = "FORGOT_UPDATE_SUCCESS";
export const FORGOT_UPDATE_ERROR = "FORGOT_UPDATE_ERROR";
export const FORGOT_UPDATE_ERROR = "FORGOT_UPDATE_ERROR";

//Loading
export const SHOW_LOADER = "SHOW_LOADER";
export const HIDE_LOADER = "HIDE_LOADER";
export const SHOW_PAGE_LOADER = "SHOW_PAGE_LOADER";
export const HIDE_PAGE_LOADER = "HIDE_PAGE_LOADER";
4 changes: 2 additions & 2 deletions frontend/src/actions/authAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export const userDetail = ()=>{
try{
// console.log("heyhey");
const result = await Axios.get("/api/users/userdetail/" + localStorage.getItem("name"));
console.log("getawait userdetail");
console.log("hello",result);
// console.log("getawait userdetail");
// console.log("hello",result);
dispatch({ type: GET_USER_DETAIL_SUCCESS, payload: result.data });
}
catch(error){
Expand Down
Loading

0 comments on commit ac3b720

Please sign in to comment.