Browse Source

Debut update file

Loquicom 5 years ago
parent
commit
8ad5939f5a
4 changed files with 25 additions and 12 deletions
  1. 7 9
      node/src/db.js
  2. 16 0
      node/src/router.js
  3. 1 1
      node/src/sql.js
  4. 1 2
      readme.md

+ 7 - 9
node/src/db.js

@@ -101,12 +101,12 @@ Db.prototype.listFile = function (username) {
     });
 };
 
-Db.prototype.fileExist = function (username, filename) {
-    if (typeof username !== 'string' || typeof filename !== 'string') {
+Db.prototype.fileExist = function (username, fileId) {
+    if (typeof username !== 'string' || typeof fileId !== 'string') {
         return false;
     }
     return new Promise((resolve, reject) => {
-        this.db.all(sql.fileExist, [username, filename], (err, rows) => {
+        this.db.all(sql.fileExist, [username, fileId], (err, rows) => {
             if (err) {
                 if (global.verbose) {
                     console.error(err);
@@ -184,15 +184,13 @@ Db.prototype.addFile = function (username, filename, data) {
     });
 };
 
-Db.prototype.updateFile = function (username, filename, data) {
-    if (typeof username !== 'string' || typeof filename !== 'string' || typeof data !== 'string') {
+Db.prototype.updateFile = function (username, fileId, data) {
+    if (typeof username !== 'string' || typeof fileId !== 'string' || typeof data !== 'string') {
         return false;
     }
-    this.fileExist(username, filename).then((result) => {
+    this.fileExist(username, fileId).then((result) => {
         if (result) {
-            this.getFile(username, filename).then((file) => {
-                this._execute(sql.updateFile, [data, file.hash]);
-            });
+            this._execute(sql.updateFile, [data, fileId]);
         }
     });
     return true;

+ 16 - 0
node/src/router.js

@@ -262,7 +262,23 @@ const router = class Router {
         }]);
 
         this.app.put('/save/:file', [this.verbose, this.verifyAuth, (req, res) => {
+            if (req.body.data === undefined || req.params.file === undefined) {
+                res.json(error(ERR_REQUEST));
+                return;
+            }
+            // Si les données sont dans un fichier
+            if (global.storage === 'file') {
 
+            }
+            // Sinon on modifie la base
+            else {
+                let result = db.updateFile(req.body.user, req.params.file, req.body.data);
+                if (result === false) {
+                    res.json(error(ERR_SERV));
+                    return;
+                }
+                res.json(success({fileId: req.params.file}));
+            }
         }]);
 
         /*this.app.post('/save/:file?', [this.verbose, this.verifyAuth, (req, res) => {

+ 1 - 1
node/src/sql.js

@@ -43,7 +43,7 @@ module.exports.fileExist = 'SELECT count(*) as nb FROM FILE f '
     + 'INNER JOIN USERFILE uf on f.fi_id = uf.fi_id '
     + 'INNER JOIN USER u on uf.us_id = u.us_id '
     + 'WHERE us_name = lower(?) '
-    + 'AND fi_name = lower(?);';
+    + 'AND fi_hash = ?;';
 
 module.exports.getFile = 'SELECT fi_hash as hash, fi_name as name, fi_data as data FROM FILE f '
     + 'INNER JOIN USERFILE uf on f.fi_id = uf.fi_id '

+ 1 - 2
readme.md

@@ -208,8 +208,7 @@ Reponse :
 ```json
 {
     "success": true,
-    "fileid": "string",
-    "filename": "string"
+    "fileid": "string"
 }
 ```