Skip to content

Instantly share code, notes, and snippets.

@jchannon
Last active August 29, 2015 13:57
Show Gist options
  • Save jchannon/9609118 to your computer and use it in GitHub Desktop.
Save jchannon/9609118 to your computer and use it in GitHub Desktop.
'use strict';
var path = require('path');
var AdmZip = require('adm-zip');
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
var glob = require('glob');
module.exports = (function () {
var aTimer;
var aFtpClient;
var processListing = function (directoryItems) {
console.log('foreach');
var itemsToDownload = [];
directoryItems.forEach(function (element, index, array) {
//Ignore directories
if (element.type === 'd') {
console.log('directory ' + element.name);
return;
}
//Ignore non zips
if (path.extname(element.name) !== '.zip') {
console.log('ignoring ' + element.name);
return;
}
//Download zip
itemsToDownload.push({
source: element.name,
destination: element.name
});
//aftpSystem.downloadFile(element.name, element.name);
});
console.log('after foreach');
return itemsToDownload;
};
var processItem = function (object) {
return aFtpClient.getAsync(object.source);
};
var downloadItem = function (object) {
object.forEach(function (element, index, array) {
element.result.once('close', function () {
//c.end();
console.log('closed');
});
element.result.pipe(fs.createWriteStream(process.cwd() + "/zips/" + element.input.destination));
console.log('downloaded: ' + element.input.destination);
});
};
var getFiles = function () {
console.log('got files');
return fs.readdirAsync(process.cwd() + "/zips/").then(function (files) {
return files.filter(function (filename) {
return path.extname(filename) === '.zip';
});
});
};
// var getFiles = function () {
// console.log('got files');
// return Promise.promisifyAll(glob('*.zip'));
// };
// var getFiles = function () {
// console.log('got files');
// NEED TO CHANGE REQUIRE TO USE THIS APPROACH
// return Promise.promisifyAll(new Glob('*.zip'));
// };
// var getFiles = function () {
// console.log('got files');
// return new Promise(function (resolve, reject) {
// glob('*.zip')
// });
// };
// var getFiles = function () {
// console.log('got files');
// return Promise.method(glob('*.zip'));
// };
var unzipFile = function (filename) {
console.log('unzipping: ' + process.cwd() + "/zips/" + filename);
var zip = new AdmZip(process.cwd() + "/zips/" + filename);
zip.extractAllTo(process.cwd() + "/zips/extracted", /*overwrite*/ true);
};
var downloadFiles = function () {
console.log('downloading files');
aFtpClient.
listAsync().
then(processListing).
map(function (object) {
return processItem(object).then(function (processResult) {
return {
input: object,
result: processResult
};
});
}).
then(downloadItem).
then(getFiles).
then(function (filename) {
console.log('on: ' + filename);
return unzipFile(filename)
}).
catch (TypeError, function (e) {
console.dir(e);
}).
catch (Promise.RejectionError, function (e) {
console.error("unable to read file, because: ", e.message);
});
console.log('after downloading files');
};
var elapsed = function () {
console.log('elapsed');
downloadFiles();
//Currently not correct place to process files as they could still be downloading
//processFiles();
//Currently in wrong place to start
//aTimer.start();
};
var mmsProcessor = function (timer, ftpClient) {
aTimer = timer(elapsed);
aTimer.start();
aFtpClient = Promise.promisifyAll(ftpClient);
};
return mmsProcessor;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment