Skip to content

Instantly share code, notes, and snippets.

@jhgaylor
Last active January 18, 2018 10:31
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jhgaylor/8345292 to your computer and use it in GitHub Desktop.
Save jhgaylor/8345292 to your computer and use it in GitHub Desktop.
Download files to the filesystem on the meteor server
//given the urls of files to download, store them on the filesystem
function download_all_files (urls, base_destination, job_id, cb) {
var url = urls.shift();
var file_path = path.join(base_destination, job_id);
// the path to the file without the filename
var path_to_file_folder = path.dirname(file_path);
// the method to store a downloaded file to the fs
// makes an http request and writes the response to a file
function download_url_to_fs () {
var file = fs.createWriteStream(file_path);
var request = https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close();
if(urls.length > 0){
download_all_files(urls, base_destination, job_id, cb);
} else {
cb();
}
});
});
}
// bind download_url_to_fs to a meteor fiber
var bound_download_url_to_fs = Meteor.bindEnvironment(download_url_to_fs, function (e) {
throw e;
});
// verify the required paths exist or create it
// and then download the file to from http to the js
mkdirp(path_to_file_folder, 0777, bound_download_url_to_fs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment