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'),
cookieParser = require('cookie-parser'),
lastMile = require('connect-lastmile'),
multipart = require('./src/multipart'),
mkdirp = require('mkdirp'),
auth = require('./src/auth.js'),
files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
......@@ -49,6 +50,9 @@ var server = app.listen(3000, function () {
var host = server.address().address;
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('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'),
readlineSync = require('readline-sync'),
async = require('async'),
fs = require('fs'),
request = require('request'),
url = require('url'),
path = require('path');
......@@ -27,7 +28,7 @@ function checkConfig() {
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) {
......@@ -110,20 +111,40 @@ function put(filePath, otherFilePaths, options) {
function get(filePath) {
checkConfig();
superagent.get(config.server() + API + filePath).query(gQuery).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 no argument provided, fetch root
filePath = filePath || '/';
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:');
result.body.entries.forEach(function (entry) {
JSON.parse(body).entries.forEach(function (entry) {
console.log('\t %s', entry);
});
} 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) {
......
......@@ -19,7 +19,7 @@ program.command('put <file> [files...]')
.description('Put a file')
.action(actions.put);
program.command('get')
program.command('get [file]')
.description('Get a file or directory')
.action(actions.get);
......
......@@ -35,6 +35,7 @@
"passport": "^0.2.2",
"passport-ldapjs": "^1.0.2",
"readline-sync": "^1.2.19",
"request": "^2.61.0",
"safetydance": "0.0.16",
"superagent": "^1.2.0",
"underscore": "^1.8.3"
......
......@@ -54,7 +54,7 @@ function copyFile(source, target, cb) {
}
function getAbsolutePath(filePath) {
var absoluteFilePath = path.resolve(gBasePath, filePath);
var absoluteFilePath = path.resolve(path.join(gBasePath, filePath));
if (absoluteFilePath.indexOf(gBasePath) !== 0) return null;
return absoluteFilePath;
......@@ -71,7 +71,7 @@ function get(req, res, next) {
debug('get', 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'));
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!