Quellcode durchsuchen

Ajout methode de verification du mode de stockage

Arthur Brandao vor 5 Jahren
Ursprung
Commit
325a208170
4 geänderte Dateien mit 87 neuen und 1 gelöschten Zeilen
  1. 4 1
      node/main.js
  2. 15 0
      node/src/db.js
  3. 2 0
      node/src/sql.js
  4. 66 0
      node/src/validate.js

+ 4 - 1
node/main.js

@@ -2,7 +2,7 @@
 
 // Gestion du signal d'arret (SIGINT = Ctrl+C)
 process.on('SIGINT', function () {
-    console.info("\nStopping the server");
+    console.info('\nStopping the server');
     process.exit();
 });
 
@@ -48,6 +48,9 @@ global.storage = config.storage;
 global.verbose = argv.verbose >= 1;
 global.sqlVerbose = argv.sql >= 1;
 
+// Validation stockage fichier
+const validator = require('./src/validate')(config);
+
 // Lancement du serveur
 const server = require('./src/server');
 server.https = config.https;

+ 15 - 0
node/src/db.js

@@ -247,6 +247,21 @@ Db.prototype.deleteFile = function (username, fileId) {
     });
 };
 
+Db.prototype.countFile = function () {
+    return new Promise((resolve, reject) => {
+        this.db.all(sql.countFile, (err, rows) => {
+            if (err) {
+                if (global.verbose) {
+                    console.error(err);
+                }
+                resolve(false);
+            } else {
+                resolve(rows[0].nb);
+            }
+        });
+    });
+};
+
 Db.prototype._execute = function (sql, params) {
     try {
         if (params !== undefined && params !== null) {

+ 2 - 0
node/src/sql.js

@@ -72,3 +72,5 @@ module.exports.renameFile = 'UPDATE FILE SET fi_name = lower(?) WHERE fi_hash =
 module.exports.deleteUserFile = 'DELETE FROM USERFILE WHERE fi_id = ?';
 
 module.exports.deleteFile = 'DELETE FROM FILE WHERE fi_id = ?';
+
+module.exports.countFile = 'SELECT count(*) as nb FROM FILE f ';

+ 66 - 0
node/src/validate.js

@@ -0,0 +1,66 @@
+const fs = require('fs');
+const db = require('./db');
+
+let converter;
+if (fs.existsSync('./src/convert/')) {
+    converter = require('./src/convert/');
+}
+
+let validator = null;
+
+class Validate {
+
+    constructor(config) {
+        this.config = config;
+        this.valid = undefined;
+        this.haveConverter = converter !== undefined;
+    }
+
+    /**
+     * See if the storage method matches the one in the config file
+     * @return boolean - File storage is valid
+     */
+    check() {
+        return new Promise(((resolve, reject) => {
+            db.countFile().then((nb) => {
+                // Impossible d'executer la requete SQL
+                if (nb === false) {
+                    throw 'Impossible to check the file storage';
+                }
+                // Aucun fichier en base tout est ok
+                else if (nb === 0) {
+                    this.valid = true;
+                    resolve(true);
+                } else {
+                    // Regarde si il y a des fichiers fdata
+                    let fdata = false;
+                    fs.readdir('./data/', (err, files) => {
+                        if (err) {
+                            if (global.verbose) {
+                                console.error(err);
+                            }
+                            throw 'Impossible to check the file storage';
+                        }
+                        files.forEach(file => {
+                            if (fdata) {
+                                return;
+                            }
+                            const split = file.split('.');
+                            fdata = split[split.length - 1] === 'fdata';
+                        });
+                        this.valid = (this.config.storage === 'database' && !fdata) || (this.config.storage === 'file' && fdata);
+                        resolve(this.valid);
+                    });
+                }
+            });
+        }));
+    }
+
+}
+
+module.exports = function (config) {
+    if (validator === null) {
+        validator = new Validate(config);
+    }
+    return validator;
+};