aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--cli/actions.js21
-rwxr-xr-xcli/surfer.js2
-rw-r--r--package.json2
-rw-r--r--src/files.js21
4 files changed, 37 insertions, 9 deletions
diff --git a/cli/actions.js b/cli/actions.js
index d10e154..8a8624e 100644
--- a/cli/actions.js
+++ b/cli/actions.js
@@ -180,16 +180,27 @@ function get(filePath) {
180 // }); 180 // });
181} 181}
182 182
183function del(filePath) { 183function del(filePath, options) {
184 checkConfig(); 184 checkConfig();
185 185
186 var query = safe.JSON.parse(safe.JSON.stringify(gQuery));
187 query.recursive = options.recursive;
188 query.dryRun = options.dryRun;
189
186 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); 190 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
187 superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) { 191 superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) {
188 if (error && error.status === 401) return console.log('Login failed'); 192 if (error && error.status === 401) return console.log('Login failed'.red);
189 if (error && error.status === 404) return console.log('No such file or directory'); 193 if (error && error.status === 404) return console.log('No such file or directory');
190 if (error && error.status === 403) return console.log('No such file or directory'); 194 if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);
191 if (error) return console.log('Failed', result ? result.body : error); 195 if (error) return console.log('Failed', result ? result.body : error);
192 196
193 console.log('Success. Removed %s files.', result.body.entries.length); 197 if (options.dryRun) {
198 console.log('This would remove %s files:', result.body.entries.length);
199 result.body.entries.forEach(function (entry) {
200 console.log('\t %s', entry);
201 });
202 } else {
203 console.log('Success. Removed %s files.', result.body.entries.length);
204 }
194 }); 205 });
195} 206}
diff --git a/cli/surfer.js b/cli/surfer.js
index 278724d..4c096d3 100755
--- a/cli/surfer.js
+++ b/cli/surfer.js
@@ -24,6 +24,8 @@ program.command('get [file]')
24 .action(actions.get); 24 .action(actions.get);
25 25
26program.command('del <file>') 26program.command('del <file>')
27 .option('-r --recursive', 'Recursive delete directories.', false)
28 .option('-d --dry-run', 'Only list files to delete.', false)
27 .description('Delete a file or directory') 29 .description('Delete a file or directory')
28 .action(actions.del); 30 .action(actions.del);
29 31
diff --git a/package.json b/package.json
index cf47f54..5fab285 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,7 @@
29 "connect-timeout": "^1.6.2", 29 "connect-timeout": "^1.6.2",
30 "cookie-parser": "^1.4.1", 30 "cookie-parser": "^1.4.1",
31 "debug": "^2.2.0", 31 "debug": "^2.2.0",
32 "del": "^1.2.0", 32 "del": "^2.2.0",
33 "ejs": "^2.4.1", 33 "ejs": "^2.4.1",
34 "express": "^4.13.4", 34 "express": "^4.13.4",
35 "express-session": "^1.13.0", 35 "express-session": "^1.13.0",
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
63function removeBasePath(filePath) {
64 return filePath.slice(gBasePath.length);
65}
66
63function get(req, res, next) { 67function 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
105function del(req, res, next) { 109function 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}