Commit 85d4e090 authored by Abhishek Patil's avatar Abhishek Patil

propfind implimented not working properly intermediate commit

Signed-off-by: Abhishek Patil's avatarAbhishek Patil <abhishek@zeroth.me>
parent 4cc1f511
...@@ -5,6 +5,7 @@ exports.logout = logout; ...@@ -5,6 +5,7 @@ exports.logout = logout;
exports.put = put; exports.put = put;
exports.get = get; exports.get = get;
exports.del = del; exports.del = del;
exports.info = info;
var superagent = require('superagent'), var superagent = require('superagent'),
config = require('./config.js'), config = require('./config.js'),
...@@ -55,6 +56,22 @@ function collectFiles(filesOrFolders, options) { ...@@ -55,6 +56,22 @@ function collectFiles(filesOrFolders, options) {
return tmp; return tmp;
} }
function getDestinationPath(file, destination) {
var relativeFilePath;
if (path.isAbsolute(file)) {
relativeFilePath = path.basename(file);
} else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir
relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
} else { // relative but somewhere else
relativeFilePath = path.basename(file);
}
var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
return destinationPath;
}
function login(uri, options) { function login(uri, options) {
var tmp = url.parse(uri); var tmp = url.parse(uri);
if (!tmp.slashes) tmp = url.parse('https://' + uri); if (!tmp.slashes) tmp = url.parse('https://' + uri);
...@@ -204,6 +221,57 @@ function get(filePath) { ...@@ -204,6 +221,57 @@ function get(filePath) {
// }); // });
} }
function info(filePath) {
checkConfig();
// if no argument provided, fetch root
filePath = filePath || '/';
// var options =
superagent.propfind(config.server() + API + filePath).query(gQuery).end(function(error, result){
if (result && result.statusCode === 401) return console.log('Login failed');
if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow);
if (error) return console.error(error);
// 222 indicates directory listing
if (result.statusCode === 222) {
var files = safe.JSON.parse(result.body);
if (!files || files.entries.length === 0) {
console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow);
} else {
console.log('Entries:');
files.entries.forEach(function (entry) {
console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath + ' - ' + entry.checksum);
});
}
} else {
// console.log(result);
var files = result.body;
// console.log(files);
console.log('\t %s - %s', files.filePath , files.checksum);
}
});
/*request.propfind(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) {
if (result && result.statusCode === 401) return console.log('Login failed');
if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow);
if (error) return console.error(error);
// 222 indicates directory listing
if (result.statusCode === 222) {
var files = safe.JSON.parse(body);
if (!files || files.entries.length === 0) {
console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow);
} else {
console.log('Entries:');
files.entries.forEach(function (entry) {
console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath + ' - ' + entry.checksum);
});
}
} else {
console.log('\t %s - %s', entry.filePath , entry.checksum);
}
});*/
}
function del(filePath, options) { function del(filePath, options) {
checkConfig(); checkConfig();
...@@ -228,3 +296,4 @@ function del(filePath, options) { ...@@ -228,3 +296,4 @@ function del(filePath, options) {
} }
}); });
} }
...@@ -26,6 +26,10 @@ program.command('get [file|dir]') ...@@ -26,6 +26,10 @@ program.command('get [file|dir]')
.description('Get a file or directory listing') .description('Get a file or directory listing')
.action(actions.get); .action(actions.get);
program.command('info [file|dir]')
.description('Get a file or directory listing')
.action(actions.info);
program.command('del <file>') program.command('del <file>')
.option('-r --recursive', 'Recursive delete directories.', false) .option('-r --recursive', 'Recursive delete directories.', false)
.option('-d --dry-run', 'Only list files to delete.', false) .option('-d --dry-run', 'Only list files to delete.', false)
......
...@@ -130,6 +130,11 @@ ...@@ -130,6 +130,11 @@
"from": "caseless@>=0.12.0 <0.13.0", "from": "caseless@>=0.12.0 <0.13.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz" "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz"
}, },
"checksum": {
"version": "0.1.1",
"from": "checksum@latest",
"resolved": "https://registry.npmjs.org/checksum/-/checksum-0.1.1.tgz"
},
"co": { "co": {
"version": "4.6.0", "version": "4.6.0",
"from": "co@>=4.6.0 <5.0.0", "from": "co@>=4.6.0 <5.0.0",
...@@ -650,6 +655,11 @@ ...@@ -650,6 +655,11 @@
"from": "once@>=1.3.0 <2.0.0", "from": "once@>=1.3.0 <2.0.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz" "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz"
}, },
"optimist": {
"version": "0.3.7",
"from": "optimist@>=0.3.5 <0.4.0",
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz"
},
"parseurl": { "parseurl": {
"version": "1.3.1", "version": "1.3.1",
"from": "parseurl@>=1.3.1 <1.4.0", "from": "parseurl@>=1.3.1 <1.4.0",
...@@ -995,6 +1005,11 @@ ...@@ -995,6 +1005,11 @@
"from": "verror@1.3.6", "from": "verror@1.3.6",
"resolved": "https://registry.npmjs.org/verror/-/verror-1.3.6.tgz" "resolved": "https://registry.npmjs.org/verror/-/verror-1.3.6.tgz"
}, },
"wordwrap": {
"version": "0.0.3",
"from": "wordwrap@>=0.0.2 <0.1.0",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz"
},
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"from": "wrappy@>=1.0.0 <2.0.0", "from": "wrappy@>=1.0.0 <2.0.0",
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"async": "^1.5.2", "async": "^1.5.2",
"bcryptjs": "^2.3.0", "bcryptjs": "^2.3.0",
"body-parser": "^1.15.0", "body-parser": "^1.15.0",
"checksum": "^0.1.1",
"colors": "^1.1.2", "colors": "^1.1.2",
"commander": "^2.8.1", "commander": "^2.8.1",
"compression": "^1.5.0", "compression": "^1.5.0",
......
...@@ -30,6 +30,7 @@ router.get ('/api/files/*', auth.verify, files.get); ...@@ -30,6 +30,7 @@ router.get ('/api/files/*', auth.verify, files.get);
router.post ('/api/files/*', auth.verify, multipart, files.post); router.post ('/api/files/*', auth.verify, multipart, files.post);
router.put ('/api/files/*', auth.verify, files.put); router.put ('/api/files/*', auth.verify, files.put);
router.delete('/api/files/*', auth.verify, files.del); router.delete('/api/files/*', auth.verify, files.del);
router.propfind('/api/files/*', files.propfind);
router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); }); router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
// welcome screen in case / does not serve up any file yet // welcome screen in case / does not serve up any file yet
......
...@@ -6,6 +6,7 @@ var async = require('async'), ...@@ -6,6 +6,7 @@ var async = require('async'),
rm = require('del'), rm = require('del'),
debug = require('debug')('files'), debug = require('debug')('files'),
mkdirp = require('mkdirp'), mkdirp = require('mkdirp'),
checksum = require('checksum'),
HttpError = require('connect-lastmile').HttpError, HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess; HttpSuccess = require('connect-lastmile').HttpSuccess;
...@@ -18,7 +19,8 @@ exports = module.exports = function (basePath) { ...@@ -18,7 +19,8 @@ exports = module.exports = function (basePath) {
get: get, get: get,
put: put, put: put,
post: post, post: post,
del: del del: del,
propfind: propfind
}; };
}; };
...@@ -199,3 +201,80 @@ function del(req, res, next) { ...@@ -199,3 +201,80 @@ function del(req, res, next) {
}); });
}); });
} }
function propfind(req, res, next) {
var filePath = decodeURIComponent(req.params[0]);
console.log('in propfind filePath ', filePath)
var absoluteFilePath = getAbsolutePath(filePath);
console.log('in propfind absoluteFilePath ', absoluteFilePath)
if (!absoluteFilePath) return next(new HttpError(403, 'Path not allowed'));
fs.stat(absoluteFilePath, function (error, result) {
if (error) return next(new HttpError(404, error));
console.log("absoluteFilePath error : ", error);
debug('get', absoluteFilePath);
if (!result.isDirectory() && !result.isFile()) return next(new HttpError(500, 'unsupported type'));
if (result.isFile()) {
checksum.file(absoluteFilePath, function(error, hash) {
console.log("checksum 1 error : ", error);
if (error) return next(new HttpError(404, error));
return res.status(200)
.send({
isDirectory: result.isDirectory(),
isFile: result.isFile(),
atime: result.atime,
mtime: result.mtime,
ctime: result.ctime,
birthtime: result.birthtime,
size: result.size,
filePath: filePath,
checksum: hash
});
});
} else {
async.map(fs.readdirSync(absoluteFilePath), function (filePath, callback) {
fs.stat(path.join(absoluteFilePath, filePath), function (error, result) {
if (error) return callback(error);
if(result.isDirectory()) {
callback(null, {
isDirectory: result.isDirectory(),
isFile: result.isFile(),
atime: result.atime,
mtime: result.mtime,
ctime: result.ctime,
birthtime: result.birthtime,
size: result.size,
filePath: filePath
});
} else {
checksum.file(path.join(absoluteFilePath, filePath), function(error, hash){
console.log("checksum 2 error : ", absoluteFilePath, error);
if (error) return next(new HttpError(404, error));
callback(null, {
isDirectory: result.isDirectory(),
isFile: result.isFile(),
atime: result.atime,
mtime: result.mtime,
ctime: result.ctime,
birthtime: result.birthtime,
size: result.size,
filePath: filePath,
checksum: hash
});
})
}
});
}, function (error, results) {
if (error) return next(new HttpError(500, error));
res.status(222).send({ entries: results });
});
}
});
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment