Created
February 8, 2017 16:32
-
-
Save clkao/cd5975e4a136269f092bb001b95627cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var JSFtp = require('jsftp'); | |
var ndjson = require('ndjson') | |
var path = require("path"); | |
var async = require("async"); | |
var URL = require('url'); | |
var FTP_DIR_TYPE = 1; | |
var base = process.argv[2];//"ftp://ftp02.portlandoregon.gov/Parks" | |
if (!base) throw "must have ftp url" | |
var url = new URL.parse(base); | |
var serialize = ndjson.serialize() | |
serialize.pipe(process.stdout); | |
var ftp = new JSFtp({host: url.hostname}); | |
var root = url.pathname; | |
ftp.on('lsr-entry', function(data) { | |
if (data.type !== FTP_DIR_TYPE) { | |
url.pathname = data.path; | |
data.url = url.format(); | |
delete data.path; | |
delete data.type; | |
serialize.write(data); | |
} | |
}).on('lsr-end', function() { serialize.end() }); | |
lsr(ftp, root, function(err, data) { | |
}); | |
function lsr(ftp, root, callback) { | |
var queue = [{ | |
type: FTP_DIR_TYPE, | |
path: root | |
}]; | |
async.doWhilst( | |
function iter(clb) { | |
cwd = queue.shift().path; | |
ftp.ls(cwd, function(err, data) { | |
if(err) return clb(err); | |
data.map(function(item) { | |
var entry = { | |
type: item.type, | |
path: path.join(cwd, item.name), | |
size: parseInt(item.size), | |
time: new Date(1464624000000) | |
}; | |
ftp.emit('lsr-entry', entry) | |
if (entry.type === FTP_DIR_TYPE) { | |
queue.push(entry); | |
} | |
}) | |
setTimeout(clb, 1000); | |
}) | |
}, function test(arg) { | |
return queue.length; | |
}, function done(err) { | |
if(err) return callback(err); | |
ftp.emit('lsr-end'); | |
callback(null); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment