-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (44 loc) · 1.61 KB
/
index.js
File metadata and controls
60 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var async = require('async');
var request = require('request');
var fs = require('fs');
var spawn = require('child_process').spawn;
var uuid = require('node-uuid');
var nconf = require('nconf');
module.exports.generateZipFromUrls = function (data, concurrency, next) {
var downloads = [];
var q = async.queue(function (item, callback) {
var filename = nconf.get('reportDownloadDirectory') + (item.filename || (uuid.v4() + '.xlsx'));
downloads.push(filename);
request(item.url).pipe(fs.createWriteStream(filename)).on('close', callback);
}, concurrency);
q.drain = function () {
var zipFilename = uuid.v4() + '.zip';
var filesList = downloads.slice(0);
// put the zip file at the front of the array
// since the zip command requires the dest zip
// to be the first parameter
filesList.unshift(nconf.get('reportDownloadDirectory') + zipFilename);
//We now create a child process to zip the files.
var zip = spawn('zip', filesList);
zip.on('error', function (err) {
return next(err);
});
//on its completion, output the url for the file to
//the browser and delete the temporary files.
zip.on('close', function (code) {
if (code !== 0) {
return next(new Error('Zip process failed, exit code: ' + code));
}
// delete our the downloaded excel files
for (var i = 0; i < downloads.length; i++) {
fs.unlink(downloads[i], function (err) {
if (err) {
console.log('Error deleting file: ' + err.code);
}
});
}
return next(null, zipFilename);
});
};
q.push(data);
};