Thursday, 27 April 2017

Download archive using GitLab API and Node.js

I would like to download (not clone) archive from my GitLab repository, but I get these error

incorrect header check (Zlib._handle.onerror)

This is my function:

var fs = require('fs');
var url = require('url');
var https = require('https');
var path = require('path');
var targz = require('tar.gz');

function downloadFile(source, destination, name) {
    var options = {
        host: url.parse(source).host,
        port: 443,
        path: url.parse(source).pathname
    };

    var file = fs.createWriteStream(destination + path.sep + name);

    https.get(options, function(res) {
        res.on('data', function(data) {
            file.write(data);
        }).on('end', function() {
            file.end();
            console.log('File ' + name + ' downloaded to ' + destination);

            targz().extract(destination + '/' + name, destination)
                .then(function(){
                    console.log('Job done!');
                })
                .catch(function(err){
                    console.log('Something is wrong ', err.stack);
                });
        });
    });
}

The file which is download is type of tar.gz. I try to set some headers but unsuccessful. Source param is like: https://gitlab.com/api/v3/projects/:ID/repository/archive?token=XXYYZZ

Any help please?



via quarky

No comments:

Post a Comment