Search code examples
node.jshttpmeteorgithub-apijszip

Error when downloading a file with Meteor from Github assets API


I'm stuck with a bug in my Meteor application. What I'm trying to do is to get a release asset file from github, and unzip it. I was able to download it from a standard browser.

The result from my Meteor request contain a Buffer, that I'm able to save to a binary if I want to, but is different from the binary I got from my browser ( I compared the hex code for each files, and even the size is different).

When I'm trying to open the archive file downloaded through Meteor (with windows zip program or with JSZip) It tells me that the file is corrupted.

Here is the code I used to download the file :

HTTP.call('GET',asset.url,{ // asset.url is a valid one
    params:{
        'access_token':token
    },
    headers: {
        'Accept':"application/octet-stream",
        'User-Agent':"My app",
    }
},function( error, result ) {
    if(error)console.log(error);
    else{
        console.log('file downloaded !');
        var app_archive = new JSZip(); // I'm using JSZip for decompressing the stream
        app_archive.load(new Buffer(result)); // fail here
        package_file = app_archive.file('package.json');
        console.log(package_file);
    }
});

and here is the Meteor console output :

=> Meteor server restarted
I20160313-16:56:43.975(-5)? file created !
I20160313-16:56:44.105(-5)? Exception in callback of async function: Error: Corr
upted zip : can't find end of central directory
I20160313-16:56:44.106(-5)?     at Object.ZipEntries.readEndOfCentral (C:\Users\
jimmy\AppData\Local\.meteor\packages\udondan_jszip\2.4.0_1\npm\node_modules\jszi
p\lib\zipEntries.js:135:19)
I20160313-16:56:44.108(-5)?     at Object.ZipEntries.load (C:\Users\jimmy\AppDat
a\Local\.meteor\packages\udondan_jszip\2.4.0_1\npm\node_modules\jszip\lib\zipEnt
ries.js:197:14)
I20160313-16:56:44.114(-5)?     at Object.ZipEntries (C:\Users\jimmy\AppData\Loc
al\.meteor\packages\udondan_jszip\2.4.0_1\npm\node_modules\jszip\lib\zipEntries.
js:21:14)
I20160313-16:56:44.116(-5)?     at Object.module.exports [as load] (C:\Users\jim
my\AppData\Local\.meteor\packages\udondan_jszip\2.4.0_1\npm\node_modules\jszip\l
ib\load.js:11:18)
I20160313-16:56:44.117(-5)?     at server/FabMo-App-Store.js:122:19
I20160313-16:56:44.119(-5)?     at runWithEnvironment (packages/meteor/dynamics_
nodejs.js:110:1)

I think It may be related to an encoding issue, but I tried almost every encoding format without any success. I'm open to any suggestion.


Solution

  • Finally made it working by using the request package

    Here is the code :

    request({
      method : "GET",
      url : asset.url,
      headers:{
        'Accept':"application/octet-stream",
        'User-Agent':"My App",
        'token':token
      },
      encoding: null // <- this one is important !
    }, function (error, response, body) {
      if(error ||  response.statusCode !== 200) {
        // handle error
      }
    
      var app_archive = new JSZip();
      app_archive.load(body);
      package_file = app_archive.file('package.json').asText();
      console.log(package_file);
    });