2018-05-03 14:15:57 +00:00
|
|
|
var mongoose = require('mongoose');
|
|
|
|
var passport = require('passport');
|
|
|
|
var config = require('../config/database');
|
|
|
|
require('../config/passport')(passport);
|
2018-05-30 21:51:57 +00:00
|
|
|
//require('../auth/facebook')(passport);
|
2018-05-03 14:15:57 +00:00
|
|
|
var express = require('express');
|
|
|
|
var jwt = require('jsonwebtoken');
|
|
|
|
var router = express.Router();
|
|
|
|
var User = require("../models/user");
|
|
|
|
var FileMongo = require("../models/FileMongo");
|
2018-05-13 16:17:18 +00:00
|
|
|
var FolderMongo = require("../models/FolderMongo");
|
2018-05-03 14:15:57 +00:00
|
|
|
var Book = require("../models/book");
|
2018-05-14 09:06:01 +00:00
|
|
|
var fs = require('fs');
|
2018-05-03 14:15:57 +00:00
|
|
|
var multer = require('multer');
|
|
|
|
var upload = multer({ dest: './public/' });
|
|
|
|
|
2018-05-17 20:22:09 +00:00
|
|
|
var passportFacebook = require('../auth/facebook');
|
|
|
|
var passportGoogle = require('../auth/google');
|
|
|
|
var passportGitHub = require('../auth/github');
|
|
|
|
|
2018-05-30 21:51:57 +00:00
|
|
|
//var router = express.Router([options]);
|
2018-05-17 20:22:09 +00:00
|
|
|
|
2018-05-30 23:52:07 +00:00
|
|
|
getStringExtention = function(monFile){
|
|
|
|
return ( monFile.name.indexOf('.') > 0 ) ? '.' + monFile.name.split('.').pop().toLowerCase() : '';
|
|
|
|
};
|
2018-05-17 20:22:09 +00:00
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* creation Token */
|
|
|
|
getToken = function (headers) {
|
|
|
|
if (headers && headers.authorization) {
|
|
|
|
var parted = headers.authorization.split(' ');
|
|
|
|
if (parted.length === 2) {
|
|
|
|
return parted[1];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* config multer dossier cible et nom du file */
|
|
|
|
const storage = multer.diskStorage({
|
|
|
|
destination: function (req, file, cb) {
|
|
|
|
cb(null, './public/')
|
|
|
|
},
|
|
|
|
filename: function (req, file, cb) {
|
|
|
|
cb(null, file.originalname)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-03 14:15:57 +00:00
|
|
|
/* GET home page. */
|
|
|
|
router.get('/', function(req, res, next) {
|
2018-05-30 22:37:35 +00:00
|
|
|
res.send('Express RESTful API');
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/signup', function(req, res) {
|
2018-05-30 22:37:35 +00:00
|
|
|
if (!req.body.username || !req.body.password) {
|
|
|
|
res.json({success: false, msg: 'Please pass username and password.'});
|
|
|
|
} else {
|
|
|
|
var newUser = new User({
|
|
|
|
username: req.body.username,
|
|
|
|
password: req.body.password
|
|
|
|
});
|
|
|
|
// save the user
|
|
|
|
newUser.save(function(err) {
|
|
|
|
if (err) {
|
|
|
|
return res.json({success: false, msg: 'Username already exists.'});
|
|
|
|
}
|
|
|
|
res.json({success: true, msg: 'Successful created new user.'});
|
|
|
|
});
|
|
|
|
}
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/* SAVE FILE */
|
|
|
|
router.post('/uploadFileMongo', function(req, res, next) {
|
2018-05-30 22:37:35 +00:00
|
|
|
FileMongo.create(req.body, function (err, post) {
|
|
|
|
if (err){
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.json(post);
|
|
|
|
});
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
2018-05-18 10:38:26 +00:00
|
|
|
/* SAVE URL FILE */
|
|
|
|
router.post('/saveURLFileMongo', function(req, res, next) {
|
|
|
|
FileMongo.findOneAndUpdate({_id : req.body._id, owner: req.body.owner}, {url: req.body.url},{new: true}).then((data) =>{
|
|
|
|
if(data === null){
|
|
|
|
throw new Error('File Not Found');
|
|
|
|
}
|
|
|
|
res.json({ message: 'File updated!' });
|
|
|
|
}).catch( (error) => {
|
|
|
|
//Deal with all your errors here with your preferred error handle middleware / method
|
|
|
|
res.status(500).json({ message: 'Some Error!' });
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-13 16:17:18 +00:00
|
|
|
/* SAVE FOLDER */
|
|
|
|
router.post('/createFolder', function(req, res, next) {
|
|
|
|
FolderMongo.create(req.body, function (err, post) {
|
|
|
|
if (err){
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.json(post);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-30 23:52:07 +00:00
|
|
|
|
|
|
|
|
2018-05-03 14:15:57 +00:00
|
|
|
/* DELETE FILE */
|
|
|
|
router.post('/deleteFileMongo', function(req, res, next) {
|
2018-06-05 00:04:54 +00:00
|
|
|
console.log(JSON.stringify(req.body));
|
2018-05-30 23:52:07 +00:00
|
|
|
FileMongo.remove({_id : req.body._id, name: req.body.name, owner: req.body.owner}, function (err, post) {
|
2018-05-30 22:37:35 +00:00
|
|
|
if (err){
|
2018-06-05 00:04:54 +00:00
|
|
|
console.log('err deleteF ileMongo : ' + err);
|
2018-05-30 23:52:07 +00:00
|
|
|
|
2018-05-30 22:37:35 +00:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
//Delete file multer dans Public
|
2018-06-05 00:04:54 +00:00
|
|
|
//console.log('this.getStringExtention(req.body) : ' + getStringExtention(req.body));
|
2018-05-30 23:52:07 +00:00
|
|
|
fs.unlink('./public/' + req.body._id + getStringExtention(req.body));
|
|
|
|
|
2018-05-30 22:37:35 +00:00
|
|
|
res.json(post);
|
2018-05-14 09:06:01 +00:00
|
|
|
|
2018-05-30 22:37:35 +00:00
|
|
|
});
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
2018-05-14 09:06:01 +00:00
|
|
|
/* DELETE FOLDER */
|
|
|
|
router.post('/deleteFolderMongo', function(req, res, next) {
|
|
|
|
FolderMongo.remove({_id : req.body._id, name: req.body.name, owner: req.body.owner}, function (err, post) {
|
|
|
|
if (err){
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
res.json(post);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
2018-05-13 16:17:18 +00:00
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Rename FOLDER */
|
|
|
|
router.post('/renameFolderMongo', function(req, res, next) {
|
|
|
|
FolderMongo.findOneAndUpdate({_id : req.body._id, owner: req.body.owner}, {name: req.body.name},{new: true}).then((data) =>{
|
|
|
|
if(data === null){
|
|
|
|
throw new Error('Folder Not Found');
|
|
|
|
}
|
|
|
|
res.json({ message: 'Folder updated!' });
|
|
|
|
}).catch( (error) => {
|
|
|
|
//Deal with all your errors here with your preferred error handle middleware / method
|
|
|
|
res.status(500).json({ message: 'Some Error!' });
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-19 13:08:00 +00:00
|
|
|
/* Move Folder */
|
|
|
|
router.post('/moveFolder', function(req, res, next) {
|
|
|
|
FolderMongo.findOneAndUpdate({_id : req.body._id, owner: req.body.owner}, {path: req.body.path, parent: req.body.parent},{new: true}).then((data) =>{
|
|
|
|
if(data === null){
|
|
|
|
throw new Error('Folder Not Found');
|
|
|
|
}
|
|
|
|
res.json({ message: 'Folder updated!' });
|
|
|
|
}).catch( (error) => {
|
|
|
|
//Deal with all your errors here with your preferred error handle middleware / method
|
|
|
|
res.status(500).json({ message: 'Some Error!' });
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Move File */
|
|
|
|
router.post('/moveFile', function(req, res, next) {
|
|
|
|
FileMongo.findOneAndUpdate({_id : req.body._id, owner: req.body.owner}, {path: req.body.path},{new: true}).then((data) =>{
|
|
|
|
if(data === null){
|
|
|
|
throw new Error('File Not Found');
|
|
|
|
}
|
|
|
|
res.json({ message: 'File updated!' });
|
|
|
|
}).catch( (error) => {
|
|
|
|
//Deal with all your errors here with your preferred error handle middleware / method
|
|
|
|
res.status(500).json({ message: 'Some Error!' });
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
2018-05-14 13:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Rename FILE */
|
|
|
|
router.post('/renameFileMongo', function(req, res, next) {
|
|
|
|
FileMongo.findOneAndUpdate({_id : req.body._id, owner: req.body.owner}, {name: req.body.name},{new: true}).then((data) =>{
|
|
|
|
if(data === null){
|
|
|
|
throw new Error('File Not Found');
|
|
|
|
}
|
|
|
|
res.json({ message: 'File updated!' });
|
|
|
|
}).catch( (error) => {
|
|
|
|
//Deal with all your errors here with your preferred error handle middleware / method
|
|
|
|
res.status(500).json({ message: 'Some Error!' });
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Get Current folder */
|
2018-05-13 16:17:18 +00:00
|
|
|
router.post('/getMainFolder', function(req, res) {
|
|
|
|
FolderMongo.findOne({
|
|
|
|
path: req.body.path,
|
|
|
|
owner: req.body.owner
|
|
|
|
}, function(err, folder) {
|
|
|
|
res.json(folder);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Get tout les folders du mainFolder */
|
2018-05-13 16:17:18 +00:00
|
|
|
router.post('/getFolderList', function(req, res) {
|
|
|
|
FolderMongo.find({
|
|
|
|
parent: req.body.mainPath,
|
|
|
|
owner: req.body.owner
|
|
|
|
}, function(err, folder) {
|
|
|
|
res.json(folder);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-19 13:08:00 +00:00
|
|
|
/* Get tout les folders du user */
|
|
|
|
router.post('/getFolderAppList', function(req, res) {
|
|
|
|
FolderMongo.find({
|
|
|
|
owner: req.body.owner
|
|
|
|
}, function(err, folder) {
|
|
|
|
res.json(folder);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-31 13:25:36 +00:00
|
|
|
/* Get tout les files du user */
|
|
|
|
router.post('/getFileAppList', function(req, res) {
|
|
|
|
FileMongo.find({
|
|
|
|
owner: req.body.owner
|
|
|
|
}, function(err, file) {
|
|
|
|
res.json(file);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Get tout les files du mainFolder */
|
2018-05-13 16:17:18 +00:00
|
|
|
router.post('/getFileList', function(req, res) {
|
|
|
|
FileMongo.find({
|
|
|
|
path: req.body.mainPath,
|
|
|
|
owner: req.body.owner
|
|
|
|
}, function(err, files) {
|
|
|
|
res.json(files);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Login */
|
2018-05-03 14:15:57 +00:00
|
|
|
router.post('/signin', function(req, res) {
|
2018-05-30 22:37:35 +00:00
|
|
|
User.findOne({
|
|
|
|
username: req.body.username
|
|
|
|
}, function(err, user) {
|
|
|
|
if (err) throw err;
|
2018-05-03 14:15:57 +00:00
|
|
|
|
2018-05-30 22:37:35 +00:00
|
|
|
if (!user) {
|
|
|
|
res.status(401).send({success: false, msg: 'Authentication failed. User not found.'});
|
2018-05-03 14:15:57 +00:00
|
|
|
} else {
|
2018-05-30 22:37:35 +00:00
|
|
|
// check if password matches
|
|
|
|
user.comparePassword(req.body.password, function (err, isMatch) {
|
|
|
|
if (isMatch && !err) {
|
2018-06-05 00:04:54 +00:00
|
|
|
console.log('user logged : ' + JSON.stringify(user));
|
2018-05-30 22:37:35 +00:00
|
|
|
// if user is found and password is right create a token
|
|
|
|
var token = jwt.sign(user.toJSON(), config.secret);
|
|
|
|
// return the information including token as JSON
|
|
|
|
res.json({success: true, token: 'JWT ' + token});
|
|
|
|
} else {
|
|
|
|
res.status(401).send({success: false, msg: 'Authentication failed. Wrong password.'});
|
|
|
|
}
|
|
|
|
});
|
2018-05-03 14:15:57 +00:00
|
|
|
}
|
2018-05-30 22:37:35 +00:00
|
|
|
});
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
2018-05-31 13:25:36 +00:00
|
|
|
//////////
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Multer upload */
|
2018-05-08 20:31:43 +00:00
|
|
|
router.post('/upload' , multer({storage: storage, limits: {fileSize: 30000000000}}).array("public[]", 12) ,function(req,res,next){
|
2018-05-18 20:07:38 +00:00
|
|
|
res.send(req.files);
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
2018-05-14 13:36:54 +00:00
|
|
|
/* Get utilisateur courant */
|
2018-05-13 16:17:18 +00:00
|
|
|
router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), function(req, res) {
|
2018-05-30 22:37:35 +00:00
|
|
|
var token = getToken(req.headers);
|
2018-05-03 14:15:57 +00:00
|
|
|
|
2018-05-30 22:37:35 +00:00
|
|
|
if (token) {
|
|
|
|
res.json(req.user);
|
|
|
|
} else {
|
|
|
|
return res.status(403).send({success: false, msg: 'Unauthorized.'});
|
|
|
|
}
|
2018-05-03 14:15:57 +00:00
|
|
|
});
|
|
|
|
|
2018-05-17 20:22:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* GET users listing. */
|
|
|
|
router.get('/', ensureAuthenticated, function(req, res, next) {
|
|
|
|
res.render('user', { user: req.user });
|
|
|
|
});
|
|
|
|
|
|
|
|
function ensureAuthenticated(req, res, next) {
|
|
|
|
if (req.isAuthenticated()) { return next(); }
|
2018-05-30 21:51:57 +00:00
|
|
|
res.redirect('/api/login');
|
2018-05-17 20:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FACEBOOK ROUTER */
|
|
|
|
router.get('/facebook', passportFacebook.authenticate('facebook'));
|
|
|
|
|
2018-05-30 21:51:57 +00:00
|
|
|
|
|
|
|
router.get('/facebook/callback',
|
2018-06-06 21:36:23 +00:00
|
|
|
passportFacebook.authenticate('facebook', { failureRedirect: '/' }),
|
2018-05-30 21:51:57 +00:00
|
|
|
function(req, res) {
|
2018-05-17 20:22:09 +00:00
|
|
|
// Successful authentication, redirect home.
|
2018-06-06 21:36:23 +00:00
|
|
|
res.redirect('/main');
|
2018-05-17 20:22:09 +00:00
|
|
|
});
|
2018-06-06 21:36:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// router.get('/facebook/callback',
|
|
|
|
// passportFacebook.authenticate('facebook', { failureRedirect: '/' }),
|
|
|
|
// function(req, res) {
|
|
|
|
// console.log('faceeeeboookk !!');
|
|
|
|
// // Successful authentication, redirect home.
|
|
|
|
// res.redirect('/main');
|
|
|
|
// });
|
|
|
|
// // router.get('/facebook/callback/:id', passport.authenticate('facebook'), function(req, res, err, user, info){
|
|
|
|
// // console.log('faceeeeboookk !!');
|
|
|
|
// //
|
|
|
|
// // //console.log('faceeeeboookk !!' + JSON.stringify(req));
|
|
|
|
// // //console.log('faceeeeboookk !!' + JSON.stringify(res));
|
|
|
|
// // //console.log('faceeeeboookk !!' + next);
|
|
|
|
// //
|
|
|
|
// // // console.log('yaaaaaaTTTTa ' + JSON.stringify(user));
|
|
|
|
// // // if(err){
|
|
|
|
// // // console.log('errrrooor : ' + err);
|
|
|
|
// // // //if(res){
|
|
|
|
// // // //res.json({ error: err });
|
|
|
|
// // // return next(err);
|
|
|
|
// // // //}
|
|
|
|
// // //
|
|
|
|
// // // }
|
|
|
|
// // // console.log('req.user.username log : ' + err + info);
|
|
|
|
// // // // Successful authentication, redirect home.
|
|
|
|
// // // if(user){
|
|
|
|
// // // User.findOne({username: user.profile.displayName},function(err,result){
|
|
|
|
// // // if(!result){
|
|
|
|
// // // var newUser = new User({username: user.profile.displayName, id: user.profile.id, password: 'couille'});
|
|
|
|
// // // // save the user
|
|
|
|
// // // console.log('rererer : '+ JSON.stringify(newUser));
|
|
|
|
// // // newUser.save(function(err) {
|
|
|
|
// // // if (err) {
|
|
|
|
// // // console.log('err already exists.' + err);
|
|
|
|
// // // }
|
|
|
|
// // // console.log('Successful created new user.');
|
|
|
|
// // // return req.res.redirect('/main');
|
|
|
|
// // //
|
|
|
|
// // // });
|
|
|
|
// // // }else{
|
|
|
|
// // // return next(err);
|
|
|
|
// // // }
|
|
|
|
// // // });
|
|
|
|
// // // }else{
|
|
|
|
// // // return req.res.redirect('/login');
|
|
|
|
// // // }
|
|
|
|
// //
|
|
|
|
// //
|
|
|
|
// //
|
|
|
|
// // });
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-17 20:22:09 +00:00
|
|
|
|
|
|
|
/* GOOGLE ROUTER */
|
|
|
|
router.get('/google', passportGoogle.authenticate('google', { scope: 'https://www.google.com/m8/feeds' }));
|
|
|
|
|
2018-05-30 21:51:57 +00:00
|
|
|
router.get('/google/callback', passportGoogle.authenticate('google', { successRedirect : '/', failureRedirect: '/login' }), function(req, res) {
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
router.get('/google',
|
|
|
|
passport.authenticate('google', { scope: ['read_stream', 'publish_actions'] })
|
|
|
|
);
|
2018-05-17 20:22:09 +00:00
|
|
|
|
|
|
|
/* GITHUB ROUTER */
|
|
|
|
router.get('/github', passportGitHub.authenticate('github', { scope: [ 'user:email' ] }));
|
|
|
|
|
2018-05-30 21:51:57 +00:00
|
|
|
router.get('/auth/github/callback', passportGitHub.authenticate('github', { successRedirect : '/', failureRedirect: '/login' }), function(req, res) {
|
|
|
|
// Successful authentication, redirect home.
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
router.get('/github',
|
|
|
|
passport.authenticate('github', { scope: ['read_stream', 'publish_actions'] })
|
|
|
|
);
|
2018-05-17 20:22:09 +00:00
|
|
|
|
|
|
|
|
2018-05-03 14:15:57 +00:00
|
|
|
module.exports = router;
|