Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a binary file from Google Drive using node.js

I'm running into a problem getting a binary from Drive using the API, I keep going in circles.

Here are the relevant code bits:

// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  if (err) {
    console.log('Error loading client secret file: ' + err);
    return;
  }
  // Authorize a client with the loaded credentials, then call the
  // Drive API.
  oauth.authorize(JSON.parse(content), dasm.init, driveapi.getFile)
});

driveapi.getFile:

function getFile(auth, cb) {
  var service = google.drive('v3');
  service.files.get({
    auth: auth,
    pageSize: 20,
    fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
    alt: 'media'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    cb(response)
  });
}

Now, response appears to be coming back as a string. When I try to convert to hex it goes nuts. Is there any way to to take response and get it into a Buffer? Or is it corrupted the sec I get it from service.files.get ?

By nuts, I mean that

console.log(
        arrData[0].charCodeAt(0).toString(2),
        '-',
        arrData[1].charCodeAt(0).toString(2),
        '-',
        arrData[2].charCodeAt(0).toString(2),
        '-',
        arrData[3].charCodeAt(0).toString(2),
        '-',
        arrData[4].charCodeAt(0).toString(2)
    )

= 1001101 - 1011010 - 1111111111111101 - 0 - 11 (I'm using binary to try to see what is broken)

The correct hex would be 4D 5A 90 00 03

Edit: For those who are confused, like I was, how 90 became fffd it's the Unicode replacement character that gets displayed when the value doesn't map to an ASCII char.

like image 337
Drazisil Avatar asked Apr 24 '16 17:04

Drazisil


People also ask

Does JavaScript support binary data?

You can send JavaScript typed arrays as binary data as well. This is building a 512-byte array of 8-bit integers and sending it; you can use any binary data you'd like, of course.

What is binary data in Nodejs?

Binary is simply a set or a collection of 1 and 0 . Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries: 10, 01, 001, 1110, 00101011.


1 Answers

Was able to solve this, finally. Google APIs use the request module, and you can apply any options that it accepts. For reference, you will need to set [encoding: null]2, as any other option will pass the response though toString, thus ruining it if you are working with binary data.

Working code is located below:

function getFile(auth, cb) {
  var service = google.drive({
    version: 'v3', 
    encoding: null
  });
  service.files.get({
    auth: auth,
    fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
    alt: 'media'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    cb(response)
  });
}
like image 170
Drazisil Avatar answered Oct 17 '22 23:10

Drazisil