]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - src/files.js
Send more folder listing details
[perso/Immae/Projets/Nodejs/Surfer.git] / src / files.js
index d12782d8c001f5fceefe1b151b1214ce3f13dd94..520127dc7826502dbea5baeddabc531c0924935f 100644 (file)
@@ -1,6 +1,7 @@
 'use strict';
 
-var fs = require('fs'),
+var async = require('async'),
+    fs = require('fs'),
     path = require('path'),
     rm = require('del'),
     debug = require('debug')('files'),
@@ -60,6 +61,10 @@ function getAbsolutePath(filePath) {
     return absoluteFilePath;
 }
 
+function removeBasePath(filePath) {
+    return filePath.slice(gBasePath.length);
+}
+
 function get(req, res, next) {
     var filePath = req.params[0];
     var absoluteFilePath = getAbsolutePath(filePath);
@@ -70,10 +75,28 @@ function get(req, res, next) {
 
         debug('get', absoluteFilePath);
 
+        if (!result.isDirectory() && !result.isFile()) return next(new HttpError(500, 'unsupported type'));
         if (result.isFile()) return res.sendFile(absoluteFilePath);
-        if (result.isDirectory()) return res.status(222).send({ entries: fs.readdirSync(absoluteFilePath) });
 
-        return next(new HttpError(500, 'unsupported type'));
+        async.map(fs.readdirSync(absoluteFilePath), function (filePath, callback) {
+            fs.stat(path.join(absoluteFilePath, filePath), function (error, result) {
+                if (error) return callback(error);
+
+                callback(null, {
+                    isDirectory: result.isDirectory(),
+                    isFile: result.isFile(),
+                    atime: result.atime,
+                    mtime: result.mtime,
+                    ctime: result.ctime,
+                    birthtime: result.birthtime,
+                    size: result.size,
+                    filePath: filePath
+                });
+            });
+        }, function (error, results) {
+            if (error) return next(new HttpError(500, error));
+            res.status(222).send({ entries: results });
+        });
     });
 }
 
@@ -104,16 +127,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'));
         });
     });
 }