Skip to content

Update file types supported by drive_download() #465

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ateucher
Copy link

@ateucher ateucher commented Jul 9, 2025

Since Google Docs now supports exporting to Markdown, it seemed like a good idea to enable that support in drive_download().

The only code I changed manually was to add the default .md extension for text/markdown mimetype in data-raw/extension-mime-type-defaults.csv. Then I just ran data-raw/mime-types.R to pull in the Google MIME types and create the csv files in inst/extdata/data/.

This does work now:

library(googledrive)

drive_auth(email = "andy@andyteucher.ca")

drive_download(
  "https://docs.google.com/document/d/1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU",
  path = "my_document.md",
)
#> File downloaded:
#> • 'Test for google drive' <id: 1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU>
#> Saved locally as:
#> • 'my_document.md'

Created on 2025-07-09 with reprex v2.1.1

@ateucher ateucher marked this pull request as draft July 10, 2025 16:51
@ateucher
Copy link
Author

Converted to draft - I'm going to do a bit more work around extensions and defaults

ateucher added 2 commits July 10, 2025 10:43
* If mimetype does not have an extension in mime_tbl.csv NA is appended to the filename
@ateucher
Copy link
Author

ateucher commented Jul 10, 2025

I don't think there is any more to do around adding default extensions to mimetypes. All of the changes here come from changes in mime::mimemap and the importFormats and exportFormats in Google's about endpoint and it seems sensible to just accept them.

I did however discover that if a mimetype does not have an extension in inst/extdata/data/mime_tbl.csv, .NA is appended to the given filename if that mimetype is specified in type. Eg.,

drive_download(
  "https://docs.google.com/document/d/1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU",
  type = "text/x-markdown",
  path = "my_document.md",
  overwrite = TRUE
)
#> File downloaded:
#> • 'Test for google drive' <id: 1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU>
#> Saved locally as:
#> • 'my_document.md.NA'

I added a fix and a test for this, so now it does not add .NA:

drive_download(
  "https://docs.google.com/document/d/1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU",
  type = "text/x-markdown",
  path = "my_document.md",
  overwrite = TRUE
)
#> File downloaded:
#> • 'Test for google drive' <id: 1EK6WZyEUfdy6sj3GjLII20Ok7qTcmRD-vv-osfHnkPU>
#> Saved locally as:
#> • 'my_document.md'

Created on 2025-07-10 with reprex v2.1.1

@ateucher ateucher marked this pull request as ready for review July 10, 2025 18:44
@ateucher
Copy link
Author

ateucher commented Jul 10, 2025

The opposite also works:

tfile <- tempfile(fileext = ".md")
writeLines(
  "# Title

  text
  ",
  tfile
)

drive_auth("andy@andyteucher.ca")

drive_upload(tfile, type = "document")
#> Local file:
#> • '/var/folders/_f/n9fw7ctx3fqf2ty9ylw502g80000gn/T//Rtmpk9yyHy/file82652c912409.md'
#> Uploaded into Drive file:
#> • 'file82652c912409.md' <id: 1APiJm3DzCRdq2WH8_X2SE5xCDYxyOU1lVYjVmoa30ns>
#> With MIME type:
#> • 'application/vnd.google-apps.document'

Created on 2025-07-10 with reprex v2.1.1

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

Successfully merging this pull request may close these issues.

1 participant