Commit 7bb99aff by Johannes Zellner

Use 222 status code to indicate folder listing and use stdout only for data

1 parent b9ad5d91
...@@ -15,6 +15,7 @@ var express = require('express'), ...@@ -15,6 +15,7 @@ var express = require('express'),
cookieParser = require('cookie-parser'), cookieParser = require('cookie-parser'),
lastMile = require('connect-lastmile'), lastMile = require('connect-lastmile'),
multipart = require('./src/multipart'), multipart = require('./src/multipart'),
mkdirp = require('mkdirp'),
auth = require('./src/auth.js'), auth = require('./src/auth.js'),
files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
...@@ -49,6 +50,9 @@ var server = app.listen(3000, function () { ...@@ -49,6 +50,9 @@ var server = app.listen(3000, function () {
var host = server.address().address; var host = server.address().address;
var port = server.address().port; var port = server.address().port;
var basePath = path.resolve(__dirname, process.argv[2] || 'files');
mkdirp.sync(basePath);
console.log('Surfer listening at http://%s:%s', host, port); console.log('Surfer listening at http://%s:%s', host, port);
console.log('Using base path', path.resolve(__dirname, process.argv[2] || 'files')); console.log('Using base path', basePath);
}); });
\ No newline at end of file
...@@ -10,6 +10,7 @@ var superagent = require('superagent'), ...@@ -10,6 +10,7 @@ var superagent = require('superagent'),
readlineSync = require('readline-sync'), readlineSync = require('readline-sync'),
async = require('async'), async = require('async'),
fs = require('fs'), fs = require('fs'),
request = require('request'),
url = require('url'), url = require('url'),
path = require('path'); path = require('path');
...@@ -27,7 +28,7 @@ function checkConfig() { ...@@ -27,7 +28,7 @@ function checkConfig() {
gQuery = { username: config.username(), password: config.password() }; gQuery = { username: config.username(), password: config.password() };
console.log('Using server %s', config.server().yellow); console.error('Using server %s', config.server().yellow);
} }
function collectFiles(filesOrFolders) { function collectFiles(filesOrFolders) {
...@@ -110,20 +111,40 @@ function put(filePath, otherFilePaths, options) { ...@@ -110,20 +111,40 @@ function put(filePath, otherFilePaths, options) {
function get(filePath) { function get(filePath) {
checkConfig(); checkConfig();
superagent.get(config.server() + API + filePath).query(gQuery).end(function (error, result) { // if no argument provided, fetch root
if (error && error.status === 401) return console.log('Login failed'); filePath = filePath || '/';
if (error && error.status === 404) return console.log('No such file or directory');
if (error) return console.log('Failed', result ? result.body : error); request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) {
if (error) return console.error(error);
if (result.statusCode === 401) return console.log('Login failed');
if (result.statusCode === 404) return console.log('No such file or directory');
if (result.body && result.body.entries) { // 222 indicates directory listing
if (result.statusCode === 222) {
console.log('Files:'); console.log('Files:');
result.body.entries.forEach(function (entry) { JSON.parse(body).entries.forEach(function (entry) {
console.log('\t %s', entry); console.log('\t %s', entry);
}); });
} else { } else {
console.log(result.text); console.log(body);
} }
}); });
// var req = superagent.get(config.server() + API + filePath);
// req.query(gQuery);
// req.end(function (error, result) {
// if (error && error.status === 401) return console.log('Login failed');
// if (error && error.status === 404) return console.log('No such file or directory');
// if (error) return console.log('Failed', result ? result.body : error);
// if (result.body && result.body.entries) {
// console.log('Files:');
// result.body.entries.forEach(function (entry) {
// console.log('\t %s', entry);
// });
// } else {
// req.pipe(process.stdout);
// }
// });
} }
function del(filePath) { function del(filePath) {
......
...@@ -19,7 +19,7 @@ program.command('put <file> [files...]') ...@@ -19,7 +19,7 @@ program.command('put <file> [files...]')
.description('Put a file') .description('Put a file')
.action(actions.put); .action(actions.put);
program.command('get') program.command('get [file]')
.description('Get a file or directory') .description('Get a file or directory')
.action(actions.get); .action(actions.get);
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
"passport": "^0.2.2", "passport": "^0.2.2",
"passport-ldapjs": "^1.0.2", "passport-ldapjs": "^1.0.2",
"readline-sync": "^1.2.19", "readline-sync": "^1.2.19",
"request": "^2.61.0",
"safetydance": "0.0.16", "safetydance": "0.0.16",
"superagent": "^1.2.0", "superagent": "^1.2.0",
"underscore": "^1.8.3" "underscore": "^1.8.3"
......
...@@ -54,7 +54,7 @@ function copyFile(source, target, cb) { ...@@ -54,7 +54,7 @@ function copyFile(source, target, cb) {
} }
function getAbsolutePath(filePath) { function getAbsolutePath(filePath) {
var absoluteFilePath = path.resolve(gBasePath, filePath); var absoluteFilePath = path.resolve(path.join(gBasePath, filePath));
if (absoluteFilePath.indexOf(gBasePath) !== 0) return null; if (absoluteFilePath.indexOf(gBasePath) !== 0) return null;
return absoluteFilePath; return absoluteFilePath;
...@@ -71,7 +71,7 @@ function get(req, res, next) { ...@@ -71,7 +71,7 @@ function get(req, res, next) {
debug('get', absoluteFilePath); debug('get', absoluteFilePath);
if (result.isFile()) return res.sendFile(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')); return next(new HttpError(500, 'unsupported type'));
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!