1

We have a javascript browser app which uses the google drive api and the google drive picker to select the file:

   let request = window.gapi.client.drive.files.get({
     fileId: fileId,
     alt: 'media'
   });
   request.execute(file => {
     this.loadFile(file);
   });

We are using a custom mime type to select only the right files. The file is a plain text file with json content in utf-8 encoding.

When I execute the script above, this.loadFile receives the content with broken character encoding. When I download the file directly from google drive, I get a correctly encoded utf-8 file.

When I upload this file manually with the ending .json, drive sets the mime type to application/json. Loading that file then with the above method, the content is correctly encoded in the result.

Is there a way to use a custom mime type and specify to use utf-8 for it? E.g. can I register the mime type in Google Drive?

I do not see a parameter on the get api.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Simon
  • 4,395
  • 8
  • 33
  • 50
  • Just adding the `.json` extension to a file with the custom mime type does not solve the problem. – Simon May 25 '18 at 09:12
  • @DaImTo why is it a problem to highlight the actual question? Would be good to know. – Simon May 25 '18 at 13:20
  • Although I'm not sure whether this is the direct solution for your situation, for example, how about retrieving the file using XMLHttpRequest? Because I have ever experienced that the file which cannot be downloaded by gapi can be download XMLHttpRequest. If this was not what you want, I'm sorry. – Tanaike May 25 '18 at 21:53
  • Did you get to solve it? So far what I've been able to figure out is that the get requests are made as binary and when the type is text/* or application/json or application/json+* or the mime type contains a charset parameter, then it transforms the base64 into a UTF8 string, otherwise, it doesn't, in my case, the mime is something like application/x.custom.app+json which doesn't match any of the above so it gets mangled which is frustrating because even after saving the file with the correct MIME + charset parameter, the API doesn't honor it – Miguel Sánchez Villafán Sep 26 '22 at 03:30

1 Answers1

0

What I ended up doing with GAPI after a long debugging session was to:

  • Get the file metadata before getting the contents

  • Check if the mimeType was application/*+json

  • If so, check against the GAPI regexps to see if it would match the proper decoding condition

    [
        /;\s*charset\s*=\s*("utf-?8"|utf-?8)\s*(;|$)/i,
        /^(text\/[^\s;/""]+|application\/(json(\+[^\s;/""]*)?|([^\s;/""]*\+)?xml))\s*(;|$)/i,
        /;\s*charset\s*=/i
    ].some( regexp => regexp.test( fileMimeType ) );
    
  • If none of the above matched, then encode the body as base64 (Without utf8 support) using btoa and decode using a UTF-8 compatible base64 decoder (NOT atob). See https://stackoverflow.com/a/30106551/1714951, Otherwise, assume the decoding was done correctly by GAPI