|
|
@ -13,6 +13,31 @@ var fs = require('fs');
|
|
|
|
var multer = require('multer');
|
|
|
|
var multer = require('multer');
|
|
|
|
var upload = multer({ dest: './public/' });
|
|
|
|
var upload = multer({ dest: './public/' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
/* GET home page. */
|
|
|
|
router.get('/', function(req, res, next) {
|
|
|
|
router.get('/', function(req, res, next) {
|
|
|
|
res.send('Express RESTful API');
|
|
|
|
res.send('Express RESTful API');
|
|
|
@ -39,7 +64,6 @@ router.post('/signup', function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
/* SAVE FILE */
|
|
|
|
/* SAVE FILE */
|
|
|
|
router.post('/uploadFileMongo', function(req, res, next) {
|
|
|
|
router.post('/uploadFileMongo', function(req, res, next) {
|
|
|
|
//console.log('req.body: :: ' + req.body.uid);
|
|
|
|
|
|
|
|
FileMongo.create(req.body, function (err, post) {
|
|
|
|
FileMongo.create(req.body, function (err, post) {
|
|
|
|
if (err){
|
|
|
|
if (err){
|
|
|
|
return next(err);
|
|
|
|
return next(err);
|
|
|
@ -82,6 +106,39 @@ router.post('/deleteFolderMongo', function(req, res, next) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Rename FILE */
|
|
|
|
|
|
|
|
router.post('/renameFileMongo', function(req, res, next) {
|
|
|
|
|
|
|
|
console.log("New File data req.body", JSON.stringify(req.body));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
router.post('/getMainFolder', function(req, res) {
|
|
|
|
FolderMongo.findOne({
|
|
|
|
FolderMongo.findOne({
|
|
|
|
path: req.body.path,
|
|
|
|
path: req.body.path,
|
|
|
@ -92,7 +149,7 @@ router.post('/getMainFolder', function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get tout les folders du mainFolder */
|
|
|
|
router.post('/getFolderList', function(req, res) {
|
|
|
|
router.post('/getFolderList', function(req, res) {
|
|
|
|
FolderMongo.find({
|
|
|
|
FolderMongo.find({
|
|
|
|
parent: req.body.mainPath,
|
|
|
|
parent: req.body.mainPath,
|
|
|
@ -103,6 +160,7 @@ router.post('/getFolderList', function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get tout les files du mainFolder */
|
|
|
|
router.post('/getFileList', function(req, res) {
|
|
|
|
router.post('/getFileList', function(req, res) {
|
|
|
|
FileMongo.find({
|
|
|
|
FileMongo.find({
|
|
|
|
path: req.body.mainPath,
|
|
|
|
path: req.body.mainPath,
|
|
|
@ -113,21 +171,7 @@ router.post('/getFileList', function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/allUserFile', passport.authenticate('jwt', { session: false}), function(req, res) {
|
|
|
|
/* Login */
|
|
|
|
var token = getToken(req.headers);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
|
|
FileMongo.find({
|
|
|
|
|
|
|
|
idUser: req.user._id,
|
|
|
|
|
|
|
|
owner: req.user.username
|
|
|
|
|
|
|
|
}, function(err, file) {
|
|
|
|
|
|
|
|
res.json(file);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return res.status(403).send({success: false, msg: 'Unauthorized.'});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/signin', function(req, res) {
|
|
|
|
router.post('/signin', function(req, res) {
|
|
|
|
User.findOne({
|
|
|
|
User.findOne({
|
|
|
|
username: req.body.username
|
|
|
|
username: req.body.username
|
|
|
@ -154,43 +198,13 @@ router.post('/signin', function(req, res) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/book', passport.authenticate('jwt', { session: false}), function(req, res) {
|
|
|
|
/* Multer upload */
|
|
|
|
var token = getToken(req.headers);
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var newBook = new Book({
|
|
|
|
|
|
|
|
isbn: req.body.isbn,
|
|
|
|
|
|
|
|
title: req.body.title,
|
|
|
|
|
|
|
|
author: req.body.author,
|
|
|
|
|
|
|
|
publisher: req.body.publisher
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newBook.save(function(err) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return res.json({success: false, msg: 'Save book failed.'});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({success: true, msg: 'Successful created new book.'});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return res.status(403).send({success: false, msg: 'Unauthorized.'});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
router.post('/upload' , multer({storage: storage, limits: {fileSize: 30000000000}}).array("public[]", 12) ,function(req,res,next){
|
|
|
|
res.send(req.files);
|
|
|
|
res.send(req.files);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Get utilisateur courant */
|
|
|
|
router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), function(req, res) {
|
|
|
|
router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), function(req, res) {
|
|
|
|
var token = getToken(req.headers);
|
|
|
|
var token = getToken(req.headers);
|
|
|
|
|
|
|
|
|
|
|
@ -201,24 +215,4 @@ router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
router.get('/book/file', function(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.sendFile(__dirname + '/api/book');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
module.exports = router;
|
|
|
|