]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - src/files.js
Fix file deletion and add dry-run option
[perso/Immae/Projets/Nodejs/Surfer.git] / src / files.js
index c725e4bac5e6a22882defcaac083ca5e632d9fcb..2214449042f5d9cb52a0a849c8f17a8820bc1281 100644 (file)
@@ -2,7 +2,6 @@
 
 var fs = require('fs'),
     path = require('path'),
-    ejs = require('ejs'),
     rm = require('del'),
     debug = require('debug')('files'),
     mkdirp = require('mkdirp'),
@@ -54,17 +53,17 @@ function copyFile(source, target, cb) {
     });
 }
 
-function render(view, options) {
-    return ejs.render(fs.readFileSync(view, 'utf8'), options);
-}
-
 function getAbsolutePath(filePath) {
-    var absoluteFilePath = path.resolve(gBasePath, filePath);
+    var absoluteFilePath = path.resolve(path.join(gBasePath, filePath));
 
     if (absoluteFilePath.indexOf(gBasePath) !== 0) return null;
     return absoluteFilePath;
 }
 
+function removeBasePath(filePath) {
+    return filePath.slice(gBasePath.length);
+}
+
 function get(req, res, next) {
     var filePath = req.params[0];
     var absoluteFilePath = getAbsolutePath(filePath);
@@ -76,7 +75,7 @@ function get(req, res, next) {
         debug('get', absoluteFilePath);
 
         if (result.isFile()) return res.sendFile(absoluteFilePath);
-        if (result.isDirectory()) return res.status(200).send({ entries: fs.readdirSync(absoluteFilePath) });
+        if (result.isDirectory()) return res.status(222).send({ entries: fs.readdirSync(absoluteFilePath) });
 
         return next(new HttpError(500, 'unsupported type'));
     });
@@ -109,16 +108,29 @@ function put(req, res, next) {
 
 function del(req, res, next) {
     var filePath = req.params[0];
+    var recursive = !!req.query.recursive;
+    var dryRun = !!req.query.dryRun;
+
     var absoluteFilePath = getAbsolutePath(filePath);
     if (!absoluteFilePath) return next(new HttpError(404, 'Not found'));
-    if (absoluteFilePath.slice(gBasePath.length) === '') return next(new HttpError(403, 'Forbidden'));
+
+    // absoltueFilePath has to have the base path prepended
+    if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(404, 'Not found'));
 
     fs.stat(absoluteFilePath, function (error, result) {
         if (error) return next(new HttpError(404, error));
 
-        rm(absoluteFilePath, function (error, result) {
-            if (error) return next(new HttpError(500, 'Unable to remove'));
+        if (result.isDirectory() && !recursive) return next(new HttpError(403, 'Is directory'));
+
+        // add globs to get file listing
+        if (result.isDirectory()) absoluteFilePath += '/**';
+
+        rm(absoluteFilePath, { dryRun: dryRun }).then(function (result) {
+            result = result.map(removeBasePath);
             next(new HttpSuccess(200, { entries: result }));
+        }, function (error) {
+            console.error(error);
+            next(new HttpError(500, 'Unable to remove'));
         });
     });
 }