X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Ffiles.js;h=2214449042f5d9cb52a0a849c8f17a8820bc1281;hb=898fe7c8bb0b04eea2e91d92f275c95713e8ec79;hp=c2a4e0f570e76032ee0ca17046637a2621dae355;hpb=d755925f749b88157e0935a7fa3c3ed94480292e;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/src/files.js b/src/files.js index c2a4e0f..2214449 100644 --- a/src/files.js +++ b/src/files.js @@ -60,6 +60,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); @@ -104,18 +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')); // absoltueFilePath has to have the base path prepended - if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(403, 'Forbidden')); + 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')); }); }); }