Browse Source

implemented send image to the goImgServer and save the url in the user avatar

master
arnaucode 7 years ago
parent
commit
3f2ec7a6e3
4 changed files with 333 additions and 214 deletions
  1. +299
    -211
      controllers/userController.js
  2. +2
    -1
      package.json
  3. +5
    -2
      server.js
  4. +27
    -0
      testSendImage.js

+ 299
- 211
controllers/userController.js

@ -1,8 +1,8 @@
//File: controllers/userController.js //File: controllers/userController.js
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var userModel = mongoose.model('userModel'); var userModel = mongoose.model('userModel');
var notificationModel = mongoose.model('notificationModel');
var travelModel = mongoose.model('travelModel');
var notificationModel = mongoose.model('notificationModel');
var travelModel = mongoose.model('travelModel');
/* */ /* */
@ -15,6 +15,8 @@ app.set('superSecret', config.secret); // secret variable
var crypto = require('crypto'); var crypto = require('crypto');
/* */ /* */
var request = require('request');
//POST - Insert a new User in the DB //POST - Insert a new User in the DB
exports.signup = function(req, res) { exports.signup = function(req, res) {
@ -38,7 +40,7 @@ exports.signup = function(req, res) {
user.save(function(err, user) { user.save(function(err, user) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
exports.login(req, res);
exports.login(req, res);
}); });
}; };
@ -47,56 +49,58 @@ exports.signup = function(req, res) {
exports.login = function(req, res) { exports.login = function(req, res) {
// find the user // find the user
userModel.findOne({ userModel.findOne({
username: req.body.username
})
.select('+password')
.exec(function(err, user) {
if (err) throw err;
if (!user) {
res.json({
success: false,
message: 'Authentication failed. User not found.'
});
} else if (user) {
username: req.body.username
})
.select('+password')
.exec(function(err, user) {
req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
if (err) throw err;
// check if password matches
if (user.password != req.body.password) {
if (!user) {
res.json({ res.json({
success: false, success: false,
message: 'Authentication failed. Wrong password.'
message: 'Authentication failed. User not found.'
}); });
} else {
} else if (user) {
// if user is found and password is right
// create a token
var token = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
//expiresInMinutes: 1440 // expires in 24 hours
//expiresIn: '60m'
});
user.token = token;
user.save(function(err, user) {
if (err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
console.log(user);
// return the information including token as JSON
user.password = "";
req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
// check if password matches
if (user.password != req.body.password) {
res.json({ res.json({
success: true,
message: 'Enjoy your token!',
token: token,
user: user
success: false,
message: 'Authentication failed. Wrong password.'
});
} else {
// if user is found and password is right
// create a token
var token = jwt.sign({
foo: 'bar'
}, app.get('superSecret'), {
//expiresInMinutes: 1440 // expires in 24 hours
//expiresIn: '60m'
});
user.token = token;
user.save(function(err, user) {
if (err) return res.send(500, err.message);
//res.status(200).jsonp(travel);
console.log(user);
// return the information including token as JSON
user.password = "";
res.json({
success: true,
message: 'Enjoy your token!',
token: token,
user: user
});
}); });
});
}
}
}
}
});
});
}; };
//GET - Return all Users in the DB //GET - Return all Users in the DB
@ -104,209 +108,293 @@ exports.getAllUsers = function(req, res) {
userModel.find() userModel.find()
.limit(Number(req.query.pageSize)) .limit(Number(req.query.pageSize))
.skip(Number(req.query.pageSize) * Number(req.query.page)) .skip(Number(req.query.pageSize) * Number(req.query.page))
.exec(function (err, users) {
.exec(function(err, users) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
res.status(200).jsonp(users); res.status(200).jsonp(users);
}); });
}; };
exports.getUserById = function (req, res) {
userModel.findOne({_id: req.params.userid})
.lean()
.populate('travels', 'title from to date type')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
res.status(200).jsonp(user);
}
});
exports.getUserById = function(req, res) {
userModel.findOne({
_id: req.params.userid
})
.lean()
.populate('travels', 'title from to date type')
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({
success: false,
message: 'User not found.'
});
} else if (user) {
res.status(200).jsonp(user);
}
});
}; };
exports.getUserByToken = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.lean()
.populate('travels', 'title from to date')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
exports.getUserByToken = function(req, res) {
userModel.findOne({
'token': req.headers['x-access-token']
})
.lean()
.populate('travels', 'title from to date')
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({
success: false,
message: 'User not found.'
});
} else if (user) {
res.status(200).jsonp(user);
}
});
res.status(200).jsonp(user);
}
});
}; };
exports.getTravelsByUserId = function (req, res) {
travelModel.find(
{
exports.getTravelsByUserId = function(req, res) {
travelModel.find({
user: req.params.userid user: req.params.userid
}
)
.lean()
.exec(function (err, travels) {
if (err) return res.send(500, err.message);
travelModel.find(
{
joins: req.params.userid
}
)
})
.lean() .lean()
.exec(function (err, joins) {
.exec(function(err, travels) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
res.json({
travels: travels,
joins: joins
});
travelModel.find({
joins: req.params.userid
})
.lean()
.exec(function(err, joins) {
if (err) return res.send(500, err.message);
res.json({
travels: travels,
joins: joins
});
});
}); });
});
}; };
exports.getUserLikes = function (req, res) {
userModel.findOne({_id: req.params.userid})
.lean()
.populate('likes', 'username avatar description')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
res.status(200).jsonp(user.likes);
}
});
exports.getUserLikes = function(req, res) {
userModel.findOne({
_id: req.params.userid
})
.lean()
.populate('likes', 'username avatar description')
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({
success: false,
message: 'User not found.'
});
} else if (user) {
res.status(200).jsonp(user.likes);
}
});
}; };
exports.getNotifications = function (req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.lean()
.populate('notifications')
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'User not found.'});
} else if (user) {
//res.status(200).jsonp(user.notifications);
notificationModel.find({'user': user._id})
.lean()
.exec(function (err, notifications) {
if (err) return res.send(500, err.message);
if (!notifications) {
res.json({success: false, message: 'No pendent notifications.'});
} else if (notifications) {
res.status(200).jsonp(notifications);
}
});
}
});
exports.getNotifications = function(req, res) {
userModel.findOne({
'token': req.headers['x-access-token']
})
.lean()
.populate('notifications')
.exec(function(err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({
success: false,
message: 'User not found.'
});
} else if (user) {
//res.status(200).jsonp(user.notifications);
notificationModel.find({
'user': user._id
})
.lean()
.exec(function(err, notifications) {
if (err) return res.send(500, err.message);
if (!notifications) {
res.json({
success: false,
message: 'No pendent notifications.'
});
} else if (notifications) {
res.status(200).jsonp(notifications);
}
});
}
});
}; };
exports.updateUser = function (req, res) {
userModel.update({'token': req.headers['x-access-token']}, req.body,
function (err) {
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
function postImage(req, res, filename, fileImg) {
url = "http://127.0.0.1:3050/image";
var importFile = function(fileImg) {
var decodedFile = new Buffer(fileImg, 'base64');
var r = request.post(url, function(err, httpResponse, body) {
if (err) {
console.log(err);
}
//console.log(body);
updateUserWithNewImages(req, res, body);
});
var form = r.form();
form.append('file', decodedFile, {
filename: filename + '.png'
});
}
importFile(fileImg);
}
function updateUserWithNewImages(req, res, imgUrl){
//adding random number to the url, to force ionic reload the image
req.body.avatar = imgUrl+ "?" + getRandomInt(1, 9999);
userModel.update({
'token': req.headers['x-access-token']
}, req.body,
function(err) {
if (err) return console.log(err); if (err) return console.log(err);
exports.getUserByToken(req, res); exports.getUserByToken(req, res);
}); });
}
exports.updateUser = function(req, res) {
if (req.body.newAvatar) {
urlImg = postImage(req, res, "avatar_"+req.body.username, req.body.newAvatar);
}
/*if (req.body.newFaircoin) {
urlImg = postImage(req, res, "fairdir_"+req.body.username,req.body.newFaircoin);
}*/
if (!req.body.newAvatar){
updateUserWithNewImages(req, res, req.body.avatar);
}
/*userModel.update({
'token': req.headers['x-access-token']
}, req.body,
function(err) {
if (err) return console.log(err);
exports.getUserByToken(req, res);
});*/
}; };
//DELETE - Delete a user with specified ID //DELETE - Delete a user with specified ID
exports.deleteUser = function(req, res) { exports.deleteUser = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.exec(function(err, user) {
user.remove(function(err) {
if (err) return res.send(500, err.message);
res.status(200).jsonp("deleted");
userModel.findOne({
'token': req.headers['x-access-token']
}) })
});
.exec(function(err, user) {
user.remove(function(err) {
if (err) return res.send(500, err.message);
res.status(200).jsonp("deleted");
})
});
}; };
exports.likeUser = function(req, res) { exports.likeUser = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.exec(function (err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({success: false, message: 'no user with that token, login again'});
} else if (userL) {
userModel.findOne({
_id: req.params.userid,
likes: {'$ne': userL._id}
})
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'Like not posible, user not exist, or like was already done'});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user "+userL.username+" adds a like to you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id,
user: user._id
});
notification.save(function(err, notification) {
userModel.findOne({
'token': req.headers['x-access-token']
})
.exec(function(err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({
success: false,
message: 'no user with that token, login again'
});
} else if (userL) {
userModel.findOne({
_id: req.params.userid,
likes: {
'$ne': userL._id
}
})
.exec(function(err, user) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
user.likes.push(userL._id);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
if (!user) {
res.json({
success: false,
message: 'Like not posible, user not exist, or like was already done'
});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user " + userL.username + " adds a like to you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id,
user: user._id
});
notification.save(function(err, notification) {
if (err) return res.send(500, err.message);
user.likes.push(userL._id);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
});
} //end of else if user
}); });
}//end of else if user
});
}//end of else if userL
});
} //end of else if userL
});
}; };
exports.unlikeUser = function(req, res) { exports.unlikeUser = function(req, res) {
userModel.findOne({'token': req.headers['x-access-token']})
.exec(function (err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({success: false, message: 'no user with that token, login again'});
} else if (userL) {
userModel.findOne({
'token': req.headers['x-access-token']
})
.exec(function(err, userL) {
if (err) return res.send(500, err.message);
if (!userL) {
res.json({
success: false,
message: 'no user with that token, login again'
});
} else if (userL) {
userModel.findOne({
_id: req.params.userid,
likes: userL._id
})
.exec(function (err, user) {
if (err) return res.send(500, err.message);
if (!user) {
res.json({success: false, message: 'Unlike not posible'});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user "+userL.username+" removes like on you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id,
user: user._id
});
notification.save(function(err, notification) {
userModel.findOne({
_id: req.params.userid,
likes: userL._id
})
.exec(function(err, user) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
var indexOf= user.likes.indexOf(userL._id);
user.likes.splice(indexOf, 1);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
if (!user) {
res.json({
success: false,
message: 'Unlike not posible'
});
} else if (user) {
//res.status(200).jsonp(user);
var notification = new notificationModel({
concept: "like",
message: "user " + userL.username + " removes like on you",
date: new Date(),
icon: 'like.png',
link: "users/" + user._id,
user: user._id
});
notification.save(function(err, notification) {
if (err) return res.send(500, err.message);
var indexOf = user.likes.indexOf(userL._id);
user.likes.splice(indexOf, 1);
user.notifications.push(notification._id);
user.save(function(err, user) {
if (err) return res.send(500, err.message);
exports.getUserById(req, res);
});
});
} //end of else if user
}); });
}//end of else if user
});
}//end of else if userL
});
} //end of else if userL
});
}; };
/* fav */ /* fav */
exports.addFav = function(req, res) { exports.addFav = function(req, res) {

+ 2
- 1
package.json

@ -20,7 +20,8 @@
"jsonwebtoken": "latest", "jsonwebtoken": "latest",
"method-override": "^2.1.2", "method-override": "^2.1.2",
"mongoose": "latest", "mongoose": "latest",
"mongoose-unique-validator": "^1.0.2",
"morgan": "latest", "morgan": "latest",
"mongoose-unique-validator": "^1.0.2"
"request": "^2.81.0"
} }
} }

+ 5
- 2
server.js

@ -18,10 +18,13 @@ mongoose.connect(config.database, function(err, res) {
app.set('superSecret', config.secret); // secret variable app.set('superSecret', config.secret); // secret variable
// Middlewares // Middlewares
app.use(bodyParser.urlencoded({
/*app.use(bodyParser.urlencoded({
extended: false extended: false
})); }));
app.use(bodyParser.json());
app.use(bodyParser.json());*/
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(methodOverride()); app.use(methodOverride());
// use morgan to log requests to the console // use morgan to log requests to the console

+ 27
- 0
testSendImage.js

@ -0,0 +1,27 @@
var request = require('request');
function postImage(fileImg) {
url = "http://127.0.0.1:3050/image";
//var fileImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
var fileImg = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
var importFile = function(aux, fileImg) {
var decodedFile = new Buffer(fileImg, 'base64');
var r = request.post(url, function(err, httpResponse, body) {
if (err) {
console.log(err);
}
console.log(body);
console.log(aux);
});
var form = r.form();
form.append('file', decodedFile, {
filename: 'temp.png'
});
return (r);
}
a = importFile("a",fileImg);
}
postImage("girada.png");

Loading…
Cancel
Save