google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

utf8 encoding on responses corrupts data

Open rsxdalv opened this issue 9 years ago • 11 comments

Currently I don't see any ways to disable this behaviour, but binary data that is sent gets corrupted, i.e., downloading a public spreadsheet file is impossible, because it's corrupted as soon as you get it.

drive.files.export({
    auth: API_KEY,
    fileId: "<mySpreadSheet>",
    mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}, (err: any, result: any) => {

at this point result is utf8.

Workaround: make a https request and don't set the encoding to utf8. Bonus points for being lazy and finding the url via debugging this library.

rsxdalv avatar Aug 16 '17 14:08 rsxdalv

@rsxdalv Thanks for bringing this up. Unfortunately, the drive API does not support export of any other documents except Google Docs. Hence, you will not be able to export a public spreadsheet. However, if you have created a Google sheet document, you will be able to export that.

Here is the documentation for your reference.

https://developers.google.com/drive/v3/reference/files/export

bantini avatar Sep 07 '17 19:09 bantini

@bantini Could you clarify the relation here? I'm using the Drive API directly via https to bypass encoding issues and it's working just fine. Yes, I am using them on google spreadsheets (which at the moment I export in excel-compatible format). I suppose that my original message was suggestive of a wider use case, but that wasn't the point - the point was that what does in fact work without encoding (that being exporting of a google sheet document) being set to utf8, cannot be currently accomplished the high level API.

rsxdalv avatar Sep 07 '17 20:09 rsxdalv

@rsxdalv So, if I understand the scenario correctly, you have created a google spreadsheet with data that is not utf8.

If you make an https request via the url, the file can be exported correctly? Are you setting the encoding for the response at your side? https://www.googleapis.com/drive/v3/files/fileId/export

However, if you make a request using the nodejs client library (drive.files.export), the file that you get is corrupted? What do you do with the result in this case? Do you pipe it to a writeStream?

bantini avatar Sep 07 '17 20:09 bantini

@bantini The data is binary. An export is a binary data stream of a ready file, which is not a text file, the spreadsheet's contents don't affect that. It's binary as a result of compression.

Yes, the solution that I am using is a simple buffer concatenation from the binary stream that turns into a complete file, which instead of saving I directly pass onto a library. In the case of binary, there's no assumed encoding until the file is passed to the library/opened by excel or google sheets.

The library sets the response encoding to utf8, which is an encoding that doesn't support every binary string, and automatically converts them into ones it accepts. Thus corrupting the data.

I am unaware, looking at the documentation, of any write-stream options for these requests. Furthermore, I feel that the encoding being dependent on the type of interface used is a bit of black magic, as I for one have no use for a streaming API in my case.

rsxdalv avatar Sep 07 '17 20:09 rsxdalv

@rsxdalv Does this work for you?

    var dest = fs.createWriteStream('temp_file.xlsx');
    drive.files.export({
      auth: auth,
      fileId: file_id,
      mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    })
    .on('end', function() {
      console.log('Done');
    })
    .on('error', function(err) {
      console.log('Error during download', err);
    })
   .pipe(dest);

bantini avatar Sep 07 '17 21:09 bantini

@bantini This doesn't corrupt the data, so the file is intact. I'll see if I can use the data event to avoid the encoding, or make a in-memory write stream.

rsxdalv avatar Sep 08 '17 06:09 rsxdalv

I think I'm having this issue, my spreadsheet is being downloaded and seems to be about the right size (30Kb) - but the xlsx file cannot be opened in Excel or even Google Sheets. When I manually export the sheet from sheets.google.com the file is 32Kb.

$ unzip data.xlsx
Archive:  data.xlsx
  End-of-central-directory signature not found

@rsxdalv Did you find a workaround ?

cloakedninjas avatar Sep 25 '17 17:09 cloakedninjas

Depends on your use case. If you just want to save the file, then a pipe (undocumented) as suggested by bantini works. Myself, I simply made a http request in which I don't set the encoding as utf-8.

rsxdalv avatar Sep 25 '17 18:09 rsxdalv

Was anything actually fixed or this issue just closed?

rsxdalv avatar Oct 18 '17 20:10 rsxdalv

Didn't the pipe solve the issue for file downloads? If not, can you be more specific on what didn't work on the file download? Accordingly, I can reopen this issue, or create a new one.

bantini avatar Oct 18 '17 20:10 bantini

Ok, so I wrote this to satisfy my use case:

import * as google from "googleapis";

const drive = google.drive("v3");

import { writeFileSync } from "fs";

const buffers = new Array<Buffer>();

drive.files.export({
    auth: key,
    fileId: "<file id>",
    mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
    .on('data', (buffer: Buffer) => {
        buffers.push(buffer);
    })
    .on('end', function () {
        const result = Buffer.concat(buffers);
        writeFileSync("./test.xlsx", result);
        console.log('Done');
    })
    .on('error', (err: any) => {
        console.log('Error during download', err);
    });

Hopefully my woes will act as a documentation for poor souls who didn't guess that the output has an encoding set to utf8 that corrupts binary data, and that they need to use the (at least back then, my guess is still now) implicit pipe API with omitting the callback.

So yes, this can be closed

rsxdalv avatar Oct 19 '17 12:10 rsxdalv