@ -2,16 +2,53 @@ var mongoose = require('mongoose');
var passport = require ( 'passport' ) ;
var config = require ( '../config/database' ) ;
require ( '../config/passport' ) ( passport ) ;
//require('../auth/facebook')(passport);
var express = require ( 'express' ) ;
var jwt = require ( 'jsonwebtoken' ) ;
var router = express . Router ( ) ;
var User = require ( "../models/user" ) ;
var FileMongo = require ( "../models/FileMongo" ) ;
var FolderMongo = require ( "../models/FolderMongo" ) ;
var Book = require ( "../models/book" ) ;
var fs = require ( 'fs' ) ;
var multer = require ( 'multer' ) ;
var upload = multer ( { dest : './public/' } ) ;
var passportFacebook = require ( '../auth/facebook' ) ;
var passportGoogle = require ( '../auth/google' ) ;
var passportGitHub = require ( '../auth/github' ) ;
//var router = express.Router([options]);
getStringExtention = function ( monFile ) {
return ( monFile . name . indexOf ( '.' ) > 0 ) ? '.' + monFile . name . split ( '.' ) . pop ( ) . toLowerCase ( ) : '' ;
} ;
/* 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 )
}
} ) ;
/* GET home page. */
router . get ( '/' , function ( req , res , next ) {
res . send ( 'Express RESTful API' ) ;
@ -38,27 +75,169 @@ router.post('/signup', function(req, res) {
/* SAVE FILE */
router . post ( '/uploadFileMongo' , function ( req , res , next ) {
console . log ( 'req.body: :: ' + req . body . uid ) ;
FileMongo . create ( req . body , function ( err , post ) {
if ( err ) {
console . log ( 'err: :: ' + err ) ;
return next ( err ) ;
}
res . json ( post ) ;
} ) ;
} ) ;
/* 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 ) ;
} ) ;
} ) ;
/* SAVE FOLDER */
router . post ( '/createFolder' , function ( req , res , next ) {
FolderMongo . create ( req . body , function ( err , post ) {
if ( err ) {
return next ( err ) ;
}
res . json ( post ) ;
} ) ;
} ) ;
/* DELETE FILE */
router . post ( '/deleteFileMongo' , function ( req , res , next ) {
console . log ( 'nana : ' + JSON . stringify ( req . body ) ) ;
FileMongo . remove ( { _id : req . body . _id , name : req . body . name , owner : req . body . owner } , function ( err , post ) {
if ( err ) {
console . log ( 'nana err : ' + err ) ;
return next ( err ) ;
}
//Delete file multer dans Public
console . log ( 'this.getStringExtention(req.body) : ' + getStringExtention ( req . body ) ) ;
fs . unlink ( './public/' + req . body . _id + getStringExtention ( req . body ) ) ;
FileMongo . remove ( req . body , function ( err , post ) {
res . json ( post ) ;
} ) ;
} ) ;
/* 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 ) ;
} ) ;
} ) ;
/* 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 ) ;
} ) ;
} ) ;
/* 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 ) ;
} ) ;
} ) ;
/* 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 */
router . post ( '/getMainFolder' , function ( req , res ) {
FolderMongo . findOne ( {
path : req . body . path ,
owner : req . body . owner
} , function ( err , folder ) {
res . json ( folder ) ;
} ) ;
} ) ;
/* Get tout les folders du mainFolder */
router . post ( '/getFolderList' , function ( req , res ) {
FolderMongo . find ( {
parent : req . body . mainPath ,
owner : req . body . owner
} , function ( err , folder ) {
res . json ( folder ) ;
} ) ;
} ) ;
/* Get tout les folders du user */
router . post ( '/getFolderAppList' , function ( req , res ) {
FolderMongo . find ( {
owner : req . body . owner
} , function ( err , folder ) {
res . json ( folder ) ;
} ) ;
} ) ;
/* Get tout les files du mainFolder */
router . post ( '/getFileList' , function ( req , res ) {
FileMongo . find ( {
path : req . body . mainPath ,
owner : req . body . owner
} , function ( err , files ) {
res . json ( files ) ;
} ) ;
} ) ;
/* Login */
router . post ( '/signin' , function ( req , res ) {
User . findOne ( {
username : req . body . username
@ -71,7 +250,7 @@ router.post('/signin', function(req, res) {
// check if password matches
user . comparePassword ( req . body . password , function ( err , isMatch ) {
if ( isMatch && ! err ) {
console . log ( 'req.user.username log ggggg :: : ' + JSON . stringify ( user ) ) ;
console . log ( 'req.user.username log : ' + JSON . stringify ( user ) ) ;
// 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
@ -85,117 +264,120 @@ router.post('/signin', function(req, res) {
} ) ;
router . post ( '/book' , passport . authenticate ( 'jwt' , { session : false } ) , function ( req , res ) {
var token = getToken ( req . headers ) ;
if ( token ) {
/* Multer upload */
router . post ( '/upload' , multer ( { storage : storage , limits : { fileSize : 30000000000 } } ) . array ( "public[]" , 12 ) , function ( req , res , next ) {
res . send ( req . files ) ;
} ) ;
var newBook = new Book ( {
isbn : req . body . isbn ,
title : req . body . title ,
author : req . body . author ,
publisher : req . body . publisher
} ) ;
/* Get utilisateur courant */
router . get ( '/getCurrentUser' , passport . authenticate ( 'jwt' , { session : false } ) , function ( req , res ) {
var token = getToken ( req . headers ) ;
newBook . save ( function ( err ) {
if ( err ) {
return res . json ( { success : false , msg : 'Save book failed.' } ) ;
}
res . json ( { success : true , msg : 'Successful created new book.' } ) ;
} ) ;
if ( token ) {
res . json ( req . user ) ;
} else {
return res . status ( 403 ) . send ( { success : false , msg : 'Unauthorized.' } ) ;
}
} ) ;
/ *
router . post ( '/book/file' , function ( req , res ) {
if ( req . files ) {
//var file = req.files.filename;
//var filename = req.files.filename.name;
console . log ( req . files ) ;
}
} ) ;
* /
const storage = multer . diskStorage ( {
destination : function ( req , file , cb ) {
cb ( null , './public/' )
} ,
filename : function ( req , file , cb ) {
cb ( null , file . originalname )
}
} ) ;
//post qui va stocker la photo du user ajoute dans le register , multer permet de definir le dossier serveur dans lequel seront stockes les photos
router . post ( '/upload' , multer ( { storage : storage , limits : { fileSize : 30000000000 } } ) . array ( "public[]" , 12 ) , function ( req , res , next ) {
console . log ( "req.files[0].fieldname: " + req . files [ 0 ] . fieldname ) ; //ok
console . log ( "req.files[0].originalname: " + req . files [ 0 ] . originalname ) ; //ok
console . log ( "req.files.filename: " + req . files [ 0 ] . filename ) ; //ok
console . log ( "req.files.path: " + req . files [ 0 ] . path ) ; //ok
console . log ( "req.files.size: " + req . files [ 0 ] . size ) ; //ok
res . send ( req . files ) ;
////////////
} ) ;
router . get ( '/allUserFile' , passport . authenticate ( 'jwt' , { session : false } ) , function ( req , res ) {
var token = getToken ( req . headers ) ;
if ( token ) {
console . log ( 'oooooo : ' + req . user . _id + ' ::: kaka : ' + req . user . username ) ;
FileMongo . find ( {
idUser : req . user . _id ,
owner : req . user . username
} , function ( err , file ) {
console . log ( 'userrrr baa : ' + JSON . stringify ( file ) ) ;
res . json ( file ) ;
} ) ;
} else {
return res . status ( 403 ) . send ( { success : false , msg : 'Unauthorized.' } ) ;
}
/* 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 ( ) ; }
res . redirect ( '/api/login' ) ;
}
router . get ( '/book' , passport . authenticate ( 'jwt' , { session : false } ) , function ( req , res ) {
var token = getToken ( req . headers ) ;
/* FACEBOOK ROUTER */
router . get ( '/facebook' , passportFacebook . authenticate ( 'facebook' ) ) ;
if ( token ) {
console . log ( 'oooooo : ' + req . user . _id + ' ::: kaka : ' + req . user . username ) ;
FileMongo . find ( {
idUser : req . user . _id ,
owner : req . user . username
} , function ( err , file ) {
console . log ( 'userrrr baa : ' + JSON . stringify ( file ) ) ;
///////////////////
router . get ( '/facebook/callback' ,
passportFacebook . authenticate ( 'facebook' , { failureRedirect : 'api/' } ) ,
function ( req , res ) {
console . log ( 'faceeeeboookk !!' ) ;
// Successful authentication, redirect home.
res . redirect ( '/api/main' ) ;
} ) ;
console . log ( 'req.user.username yiiiiii ::: ' + req . user . username ) ;
res . json ( req . user ) ;
/ *
Book . find ( function ( err , books ) {
if ( err ) return next ( err ) ;
res . json ( books ) ;
} ) ; * /
} else {
return res . status ( 403 ) . send ( { success : false , msg : 'Unauthorized.' } ) ;
}
} ) ;
// 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');
// // }
//
//
//
// });
router . get ( '/book/file' , function ( req , res ) {
// router.get('/facebook',
// passportFacebook.authenticate('facebook', { scope: ['read_stream', 'publish_actions'] })
// );
res . sendFile ( _ _dirname + '/api/book' ) ;
/* GOOGLE ROUTER */
router . get ( '/google' , passportGoogle . authenticate ( 'google' , { scope : 'https://www.google.com/m8/feeds' } ) ) ;
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' ] } )
) ;
/* GITHUB ROUTER */
router . get ( '/github' , passportGitHub . authenticate ( 'github' , { scope : [ 'user:email' ] } ) ) ;
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' ] } )
) ;
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 ;
}
} ;
module . exports = router ;