Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drive_upload doesn't use file extension when type is NULL #207

Closed
jarodmeng opened this issue Jan 19, 2018 · 5 comments
Closed

drive_upload doesn't use file extension when type is NULL #207

jarodmeng opened this issue Jan 19, 2018 · 5 comments
Labels

Comments

@jarodmeng
Copy link

drive_upload() documentation says that file extension is used to determine type when type is NULL (see here).

This line of code determines the type included in params. It doesn't take into consideration the file extension. When type is NULL, drive_mime_type simply doesn't return anything.

params[["mimeType"]] <- drive_mime_type(type)

I might be interpreting the documentation in a wrong way. Does it mean that the extension would be used by Google Drive rather than the googledrive package to determine the mime type? I was expecting to see something similar to what drive_download() has.

ext <- file_ext_safe(path)
export_type <- type %||% ext %||% get_export_mime_type(mime_type)
export_type <- drive_mime_type(export_type)
@jennybc
Copy link
Member

jennybc commented Jan 19, 2018

Hmm ... can you create an example to illustrate your concern? My mind is far away from this right now. But I think you may be correct that if we don't send a MIME type, then we're relying on Google Drive to determine if from file extension. In any case, would be easier to discuss in the context of "here's what I expect" vs. "here's what actually happens".

@LucyMcGowan
Copy link
Member

I believe this statement is based on how httr::upload_file() handles when MIME types aren't given. It uses the mime::guess_type() function, which looks up the MIME type based on the extensions of the given filename.

@jarodmeng
Copy link
Author

Here's my use case.

I'm using Google's Colaboratory in Drive. Colab notebook is usually stored with file extension ipynb and has mime type application/vnd.google.colab.

This mime type is not included in googledrive's mime_tbl.csv or mime's mimemap and mimeextra.

What I want to achieve is when I drive_upload an ipynb file, it's recognized as a Colab notebook automatically with the mime type application/vnd.google.colab. At the moment, I need to manually pass type = "application/vnd.google.colab" to drive_upload().

My initial thought is that if drive_upload() looks for a mime type based on a file's extension using the drive_mime_type() function, then I can simply add a line to the mime_tbl.csv file mapping ipynb file to application/vnd.google.colab. During the research process, I discovered that drive_upload() actually doesn't pass the file's extension to drive_mime_type().

@LucyMcGowan You're right that httr::upload_file() takes care of the mime type guessing. I think I will add a mapping between ipynb and application/vnd.google.colab to mime's mimemap and send Yihui a PR.

In the meantime, I'm wondering if I should also send a PR to googledrive to add the mapping in mime_tbl.csv since it's actually not being used in drive_upload.

@jennybc
Copy link
Member

jennybc commented Jul 26, 2019

I think we were relying on some combination of the Google Drive API and httr::upload_file() to guess the MIME type. Here's a quote from the docs on the files.create endpoint:

The MIME type of the file. Google Drive will attempt to automatically detect an appropriate value from uploaded content if no value is provided.

But it's clear that neither Drive nor httr is expecting MIME type application/vnd.google.colaboratory or application/vnd.google.colab.

I think the first thing to do is to teach googledrive about this MIME type, so that a direct specification works. I'm hesitant to explicitly set MIME type based on file extension, in general, because then I own that logic/data, whereas I think it really belongs upstream.

@jennybc
Copy link
Member

jennybc commented Jul 26, 2019

You can now upload Colaboratory notebooks like so:

drive_upload("foo.ipynb", type = "colab")
drive_upload("foo.ipynb", type = "application/vnd.google.colab")

It still won't work automatically based on file extension because googledrive doesn't actually have any of that logic. That would require the Drive API to recognize Colab notebooks or a PR into the mimemap object in the mime package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants