diff options
author | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 13:04:12 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 13:04:12 +0100 |
commit | 898fe7c8bb0b04eea2e91d92f275c95713e8ec79 (patch) | |
tree | 4ece088cfaa7a1ea29942a73315a71a61f84d4de /src | |
parent | d755925f749b88157e0935a7fa3c3ed94480292e (diff) | |
download | Surfer-898fe7c8bb0b04eea2e91d92f275c95713e8ec79.tar.gz Surfer-898fe7c8bb0b04eea2e91d92f275c95713e8ec79.tar.zst Surfer-898fe7c8bb0b04eea2e91d92f275c95713e8ec79.zip |
Fix file deletion and add dry-run option
Diffstat (limited to 'src')
-rw-r--r-- | src/files.js | 21 |
1 files changed, 18 insertions, 3 deletions
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) { | |||
60 | return absoluteFilePath; | 60 | return absoluteFilePath; |
61 | } | 61 | } |
62 | 62 | ||
63 | function removeBasePath(filePath) { | ||
64 | return filePath.slice(gBasePath.length); | ||
65 | } | ||
66 | |||
63 | function get(req, res, next) { | 67 | function get(req, res, next) { |
64 | var filePath = req.params[0]; | 68 | var filePath = req.params[0]; |
65 | var absoluteFilePath = getAbsolutePath(filePath); | 69 | var absoluteFilePath = getAbsolutePath(filePath); |
@@ -104,18 +108,29 @@ function put(req, res, next) { | |||
104 | 108 | ||
105 | function del(req, res, next) { | 109 | function del(req, res, next) { |
106 | var filePath = req.params[0]; | 110 | var filePath = req.params[0]; |
111 | var recursive = !!req.query.recursive; | ||
112 | var dryRun = !!req.query.dryRun; | ||
113 | |||
107 | var absoluteFilePath = getAbsolutePath(filePath); | 114 | var absoluteFilePath = getAbsolutePath(filePath); |
108 | if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); | 115 | if (!absoluteFilePath) return next(new HttpError(404, 'Not found')); |
109 | 116 | ||
110 | // absoltueFilePath has to have the base path prepended | 117 | // absoltueFilePath has to have the base path prepended |
111 | if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(403, 'Forbidden')); | 118 | if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(404, 'Not found')); |
112 | 119 | ||
113 | fs.stat(absoluteFilePath, function (error, result) { | 120 | fs.stat(absoluteFilePath, function (error, result) { |
114 | if (error) return next(new HttpError(404, error)); | 121 | if (error) return next(new HttpError(404, error)); |
115 | 122 | ||
116 | rm(absoluteFilePath, function (error, result) { | 123 | if (result.isDirectory() && !recursive) return next(new HttpError(403, 'Is directory')); |
117 | if (error) return next(new HttpError(500, 'Unable to remove')); | 124 | |
125 | // add globs to get file listing | ||
126 | if (result.isDirectory()) absoluteFilePath += '/**'; | ||
127 | |||
128 | rm(absoluteFilePath, { dryRun: dryRun }).then(function (result) { | ||
129 | result = result.map(removeBasePath); | ||
118 | next(new HttpSuccess(200, { entries: result })); | 130 | next(new HttpSuccess(200, { entries: result })); |
131 | }, function (error) { | ||
132 | console.error(error); | ||
133 | next(new HttpError(500, 'Unable to remove')); | ||
119 | }); | 134 | }); |
120 | }); | 135 | }); |
121 | } | 136 | } |