Warm tip: This article is reproduced from serverfault.com, please click

Can an integration with Google Drive API handle custom MIMETypes?

发布于 2020-11-20 23:32:46

I have been looking at the Google Drive API https://developers.google.com/drive/api/v3/enable-sdk curious if I could use it as a cloud storage for files for a custom web app. The MIMETypes make sense when they are the standard ones but I haven't been able to figure out what happens with non-standard MIMETypes, is it just of unknown file type (application/octet-stream) or something else?

I also noticed that in the section about integrating with the Drive UI, you can specify MIMETypes associated with your app (or just file types the app can actually open and interact with) and Google Drive will suggest that this file type can be opened with the app, but does this not work with custom MIMETypes?

I have't been able to test around with it as I would need my server setup, currently just getting an idea of my options and if I would need to settle for swapping my custom files to say a PDF file :\

Additional Context So for example let's say I had an app that had Google Drive API enabled and wanted to use Drive UI as well. I was reading their documentation for supported format types and that sometimes custom application/CUSTOM_TYPE MIMETypes when being sent via their file upload API call like:

 POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media
 Content-Type=application/CUSTOM_TYPE
 ...

Let's say it isn't a standard format that Google Drive supports and I read that sometimes custom MIMETypes end up being changed to application/octet-stream, which is problematic if you set up the Drive UI to have google recognize your app wants to open application/CUSTOM_TYPE and not application/octet-stream.

I am not sure how to combat this. There are options to convert this custom format into something that is supported by Google but say the app wants a very specific data so being able to open a standard format, like a pdf, isn't super helpful because it may not be the type of data the app is looking for, thus a custom type would solve that issue... if it actually works with Google Drive that is.

Questioner
Maplefury
Viewed
0
Maplefury 2021-02-10 01:48:01

After delving into this, I realized that Google Drive UI does allow for custom mimeTypes. The one caveat is that I only know this for sure works with custom mimeTypes that are based on xml or json.

I am going to describe how the app uses/transfers the custom files with our own storage and then how I got it to work with Google Drive and the Drive UI.

Using custom files within the app. To transfer data around the app/database the files are a format of json or xml but the mimeTypes are of the form application/custom+xml or application/custom+json. These files also have a custom fileType that is used internally, which when we move around files is a part of the mimeType in the request when POSTing it to on of the various endpoints. This looks like application/custom+xml; fileType=subCustomType. Also because this is technically just based off of xml or json the mimeType could also just be text/xml or application/json.

Getting custom files uploaded to Google Drive. Note the app has used several ways to send a file to google drive, whether with a library or sending requests to the REST endpoints using access tokens, I won't be focusing on that aspect, just the specifics about the file itself.

  1. Since the files are based off xml/json the file contents are turned into a blob with a type of text/xml or application/json. And a file reader is used to read it as a binary string to be used in a multipart upload.
  2. In my experimentation the mimeType in the form of application/custom+xml; fileType=subCustomType gets truncated to application/custom_xml, so to still associated that info with the file, I utilized the properties attribute on the google file metadata to specify what custom file type it was as well. Note if this is data is app specific and private to the app this metadata can be stored in the appProperties instead. So the metadata for file, in JSON format ends up looking something like: var metadata = {"name": file_.getName() + '.custom' , 'mimeType': 'application/customFormat+xml', "properties": {'customInternalFileType' : 'subScript'}} Note that this is JSON stringified and added into the multipart upload with the file content.
  3. The file extension wasn't used internally but will be used later when configuring the Drive UI integration as having a custom file extension(s) help identify to Google what files this app wants to open. Once the request is complete the file should be within the drive with the specific name, extension, etc. and should have content stored within.

Configuring Google Drive UI now that we have a custom mimeType and file extension we are interested we can set up the UI integration. Note that this doesn't give helpful errors, so I will point out what I learned when trying to get the changes to be saved. But here is a screenshot of what it looks like with correct info about the 'open URL' and the custom mimeTypes and file extensions. (note I don't have the ability to embed images so here is a link instead). Google Drive UI Integration Screenshot

Things to watch out for:

  1. The Open URL (and New URL if you want to also configure that) need to be valid domains and cannot be localhost, trust me I tried, it doesn't work.
  2. For file extensions you need to be the format of '/' having one side and not the other doesn't appear to work.
  3. For file extensions you do not need to put in the '.' that throws an error, just the name is sufficient.
  4. Not pictured in the screenshot but you need at least one application icon of a particular pixel x pixel value, typically easier to diagnose when it doesn't work, the picture won't be accepted if it isn't the correct size.

Once that is complete and given time for google to update itself, you should be able to mouse over a specific file for the app, and your app should be suggested to open it!