|  | @@ -1,3 +1,4 @@
 | 
											
												
													
														|  | 
 |  | +const fs = require('fs');
 | 
											
												
													
														|  |  const auth = require('./auth');
 |  |  const auth = require('./auth');
 | 
											
												
													
														|  |  const db = require('./db');
 |  |  const db = require('./db');
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -7,14 +8,15 @@ const ERR_AUTH = 2;
 | 
											
												
													
														|  |  const ERR_UNKNOW = 3;
 |  |  const ERR_UNKNOW = 3;
 | 
											
												
													
														|  |  const ERR_TOKEN = 4;
 |  |  const ERR_TOKEN = 4;
 | 
											
												
													
														|  |  const ERR_SERV = 5;
 |  |  const ERR_SERV = 5;
 | 
											
												
													
														|  | 
 |  | +const ERR_FILE = 6;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  // Fonctions de traitement pour les routes
 |  |  // Fonctions de traitement pour les routes
 | 
											
												
													
														|  |  function verifyAuth(req, res, next) {
 |  |  function verifyAuth(req, res, next) {
 | 
											
												
													
														|  | -    if(req.body.user === undefined || req.body.token === undefined) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (req.body.user === undefined || req.body.token === undefined) {
 | 
											
												
													
														|  |          res.json(error(ERR_REQUEST));
 |  |          res.json(error(ERR_REQUEST));
 | 
											
												
													
														|  |          return;
 |  |          return;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  | -    if(auth.isActivated() && !auth.verify(req.body.user, req.body.token)) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (auth.isActivated() && !auth.verify(req.body.user, req.body.token)) {
 | 
											
												
													
														|  |          res.json(error(ERR_TOKEN));
 |  |          res.json(error(ERR_TOKEN));
 | 
											
												
													
														|  |          return;
 |  |          return;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -22,11 +24,11 @@ function verifyAuth(req, res, next) {
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  function verbose(req, res, next) {
 |  |  function verbose(req, res, next) {
 | 
											
												
													
														|  | -    if(global.verbose) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (global.verbose) {
 | 
											
												
													
														|  |          const nbProp = Object.keys(req.body);
 |  |          const nbProp = Object.keys(req.body);
 | 
											
												
													
														|  |          console.log(`\nCall ${req.route.path} with ${nbProp.length} parameter(s)`);
 |  |          console.log(`\nCall ${req.route.path} with ${nbProp.length} parameter(s)`);
 | 
											
												
													
														|  | -        if(nbProp.length > 0) {
 |  | 
 | 
											
												
													
														|  | -            for(prop in req.body) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +        if (nbProp.length > 0) {
 | 
											
												
													
														|  | 
 |  | +            for (prop in req.body) {
 | 
											
												
													
														|  |                  console.log(`   ${prop}: ${req.body[prop]}`);
 |  |                  console.log(`   ${prop}: ${req.body[prop]}`);
 | 
											
												
													
														|  |              }
 |  |              }
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
										
											
												
													
														|  | @@ -40,7 +42,7 @@ function error(code) {
 | 
											
												
													
														|  |          success: false,
 |  |          success: false,
 | 
											
												
													
														|  |          code: code
 |  |          code: code
 | 
											
												
													
														|  |      };
 |  |      };
 | 
											
												
													
														|  | -    switch(code) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    switch (code) {
 | 
											
												
													
														|  |          case ERR_REQUEST:
 |  |          case ERR_REQUEST:
 | 
											
												
													
														|  |              answer.message = 'Bad request';
 |  |              answer.message = 'Bad request';
 | 
											
												
													
														|  |              break;
 |  |              break;
 | 
											
										
											
												
													
														|  | @@ -56,6 +58,8 @@ function error(code) {
 | 
											
												
													
														|  |          case ERR_SERV:
 |  |          case ERR_SERV:
 | 
											
												
													
														|  |              answer.message = 'Server error';
 |  |              answer.message = 'Server error';
 | 
											
												
													
														|  |              break;
 |  |              break;
 | 
											
												
													
														|  | 
 |  | +        case ERR_FILE:
 | 
											
												
													
														|  | 
 |  | +            answer.message = 'File not found';
 | 
											
												
													
														|  |          default:
 |  |          default:
 | 
											
												
													
														|  |              answer.message = 'Unknow error';
 |  |              answer.message = 'Unknow error';
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -63,10 +67,9 @@ function error(code) {
 | 
											
												
													
														|  |  }
 |  |  }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  function success(data) {
 |  |  function success(data) {
 | 
											
												
													
														|  | -    if(data === undefined || data === null) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (data === undefined || data === null) {
 | 
											
												
													
														|  |          return {success: true};
 |  |          return {success: true};
 | 
											
												
													
														|  | -    }
 |  | 
 | 
											
												
													
														|  | -    else if(typeof data !== 'object') {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    } else if (typeof data !== 'object') {
 | 
											
												
													
														|  |          return {success: true, data: data};
 |  |          return {success: true, data: data};
 | 
											
												
													
														|  |      } else {
 |  |      } else {
 | 
											
												
													
														|  |          data.success = true;
 |  |          data.success = true;
 | 
											
										
											
												
													
														|  | @@ -80,7 +83,7 @@ app.get('/authentication', [verbose, (req, res) => {
 | 
											
												
													
														|  |  }]);
 |  |  }]);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  app.post('/register', [verbose, (req, res) => {
 |  |  app.post('/register', [verbose, (req, res) => {
 | 
											
												
													
														|  | -    if(req.body.user === undefined || req.body.password === undefined) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (req.body.user === undefined || req.body.password === undefined) {
 | 
											
												
													
														|  |          res.json(error(ERR_REQUEST));
 |  |          res.json(error(ERR_REQUEST));
 | 
											
												
													
														|  |          return;
 |  |          return;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -90,15 +93,15 @@ app.post('/register', [verbose, (req, res) => {
 | 
											
												
													
														|  |  }]);
 |  |  }]);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  app.post('/login', [verbose, (req, res) => {
 |  |  app.post('/login', [verbose, (req, res) => {
 | 
											
												
													
														|  | -    if(req.body.user === undefined || req.body.password === undefined) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (req.body.user === undefined || req.body.password === undefined) {
 | 
											
												
													
														|  |          res.json(error(ERR_REQUEST));
 |  |          res.json(error(ERR_REQUEST));
 | 
											
												
													
														|  |          return;
 |  |          return;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |      db.getUser(req.body.user).then((user) => {
 |  |      db.getUser(req.body.user).then((user) => {
 | 
											
												
													
														|  | -        if(user === undefined) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +        if (user === undefined) {
 | 
											
												
													
														|  |              res.json(error(ERR_UNKNOW));
 |  |              res.json(error(ERR_UNKNOW));
 | 
											
												
													
														|  |          } else {
 |  |          } else {
 | 
											
												
													
														|  | -            if(auth.passwordVerify(req.body.password, user.pass)) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +            if (auth.passwordVerify(req.body.password, user.pass)) {
 | 
											
												
													
														|  |                  res.json(success({token: auth.generateToken(req.body.user)}));
 |  |                  res.json(success({token: auth.generateToken(req.body.user)}));
 | 
											
												
													
														|  |              } else {
 |  |              } else {
 | 
											
												
													
														|  |                  res.json(error(ERR_AUTH));
 |  |                  res.json(error(ERR_AUTH));
 | 
											
										
											
												
													
														|  | @@ -108,7 +111,7 @@ app.post('/login', [verbose, (req, res) => {
 | 
											
												
													
														|  |  }]);
 |  |  }]);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  app.post('/token', [verbose, (req, res) => {
 |  |  app.post('/token', [verbose, (req, res) => {
 | 
											
												
													
														|  | -    if(req.body.user === undefined || req.body.token === undefined) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +    if (req.body.user === undefined || req.body.token === undefined) {
 | 
											
												
													
														|  |          res.json(error(ERR_REQUEST));
 |  |          res.json(error(ERR_REQUEST));
 | 
											
												
													
														|  |          return;
 |  |          return;
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
										
											
												
													
														|  | @@ -117,8 +120,7 @@ app.post('/token', [verbose, (req, res) => {
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  app.post('/list', [verbose, verifyAuth, (req, res) => {
 |  |  app.post('/list', [verbose, verifyAuth, (req, res) => {
 | 
											
												
													
														|  |      db.listFile(req.body.user).then((list) => {
 |  |      db.listFile(req.body.user).then((list) => {
 | 
											
												
													
														|  | -        //console.log(req);
 |  | 
 | 
											
												
													
														|  | -        if(list === false) {
 |  | 
 | 
											
												
													
														|  | 
 |  | +        if (list === false) {
 | 
											
												
													
														|  |              res.json(error(ERR_SERV));
 |  |              res.json(error(ERR_SERV));
 | 
											
												
													
														|  |          } else {
 |  |          } else {
 | 
											
												
													
														|  |              res.json(success({
 |  |              res.json(success({
 | 
											
										
											
												
													
														|  | @@ -129,6 +131,33 @@ app.post('/list', [verbose, verifyAuth, (req, res) => {
 | 
											
												
													
														|  |      });
 |  |      });
 | 
											
												
													
														|  |  }]);
 |  |  }]);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +app.get('/get/:file', [verbose, verifyAuth, (req, res) => {
 | 
											
												
													
														|  | 
 |  | +    db.getFile(req.body.user, req.params.file).then((file) => {
 | 
											
												
													
														|  | 
 |  | +        // Erreur
 | 
											
												
													
														|  | 
 |  | +        if (file === false) {
 | 
											
												
													
														|  | 
 |  | +            res.json(error(ERR_SERV));
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        // Création reponse commune
 | 
											
												
													
														|  | 
 |  | +        let result = {
 | 
											
												
													
														|  | 
 |  | +            fileid: file.fi_hash,
 | 
											
												
													
														|  | 
 |  | +            filename: file.fi_name
 | 
											
												
													
														|  | 
 |  | +        };
 | 
											
												
													
														|  | 
 |  | +        // Recupération données fichier
 | 
											
												
													
														|  | 
 |  | +        if (global.storage === 'database') {
 | 
											
												
													
														|  | 
 |  | +            result.data = file.data;
 | 
											
												
													
														|  | 
 |  | +            res.json(success(result));
 | 
											
												
													
														|  | 
 |  | +        } else {
 | 
											
												
													
														|  | 
 |  | +            if (!fs.existsSync(file.data)) {
 | 
											
												
													
														|  | 
 |  | +                res.json(error(ERR_FILE));
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            fs.readFile(file.data, (err, data) => {
 | 
											
												
													
														|  | 
 |  | +                result.data = data;
 | 
											
												
													
														|  | 
 |  | +                res.json(success(result));
 | 
											
												
													
														|  | 
 |  | +            });
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +    });
 | 
											
												
													
														|  | 
 |  | +}]);
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  /*
 |  |  /*
 | 
											
												
													
														|  |  app.get('/', function (req, res) {
 |  |  app.get('/', function (req, res) {
 | 
											
												
													
														|  |      res.send('Hello World!');
 |  |      res.send('Hello World!');
 | 
											
										
											
												
													
														|  | @@ -141,4 +170,4 @@ app.get('/test/:val?', function (req, res) {
 | 
											
												
													
														|  |  app.get(/.*aze$/, function (req, res) {
 |  |  app.get(/.*aze$/, function (req, res) {
 | 
											
												
													
														|  |      res.send('URL end with aze');
 |  |      res.send('URL end with aze');
 | 
											
												
													
														|  |  })
 |  |  })
 | 
											
												
													
														|  | -*/
 |  | 
 | 
											
												
													
														|  | 
 |  | +*/
 |