Update file types supported by drive_download()#465
Conversation
|
Converted to draft - I'm going to do a bit more work around extensions and defaults |
* If mimetype does not have an extension in mime_tbl.csv NA is appended to the filename
|
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 I did however discover that if a mimetype does not have an extension in 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 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 |
|
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 |
| import,application/vnd.google-apps.document,text/plain,NA | ||
| import,application/vnd.google-apps.document,text/richtext,NA | ||
| import,application/vnd.google-apps.document,text/rtf,NA | ||
| import,application/vnd.google-apps.document,text/x-markdown,NA |
There was a problem hiding this comment.
text/x-markdown appears to be a rather weird/legacy MIME type, but if it's one of the official ones (and it is), I agree we need to plan for it. This is a note to myself.
The test won't work as written anymore, using the "text/x-markdown" MIME type. Instead of modifying it, I tested the filepath utility itself.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
Thanks! As for my commits, this was mostly an effort to recollect how these MIME type / file extension tables are constructed. The way I had rigged this was not terribly clear. I think it's somewhat improved -- it'll have to do for now. And we've got tests for upload/download. |
|
Great, thanks so much for finishing it off! |
|
@ateucher thanks for your PR!. It would have been awesome if the code were not lost in the process of upload + download, but I guess it's a google issue and not an implementation issue. pak::pak("tidyverse/googledrive")
#> ℹ Loading metadata database
#> ✔ Loading metadata database ... done
#>
#>
#> ✔ All system requirements are already installed.
#>
#> ℹ No downloads are needed
#> ✔ 1 pkg + 24 deps: kept 24 [4.8s]library(googledrive)
tfile <- tempfile(fileext = ".md")
code <- r"(
# Title
```r
1+1
```
text
)"
writeLines(code, tfile, sep = "\n")
cat(paste0(readLines(tfile), "\n"))
#>
#>
#> # Title
#>
#> ```r
#> 1+1
#> ```
#>
#> text
#> options(gargle_oauth_email = Sys.getenv("GOOGLE_EMAIL"))
drive_auth_configure(
path = Sys.getenv("GOOGLE_API_CREDENTIALS_OAUTH20_CLIENT_ID_DESTOP_FILE")
)
drive_upload(tfile, type = "document")
#> Local file:
#> • '/tmp/Rtmp2Lg9bf/file27bff5042c5a8.md'
#> Uploaded into Drive file:
#> • 'file27bff5042c5a8.md' <id: 1aX6tY9RIyMiq_RB1LSDeai7judGcVhGNKi4JhEdD7f8>
#> With MIME type:
#> • 'application/vnd.google-apps.document'
drive_download(
basename(tfile),
path = "markdown-googledrive.md",
overwrite = TRUE
)
#> File downloaded:
#> • 'file27bff5042c5a8.md' <id: 1aX6tY9RIyMiq_RB1LSDeai7judGcVhGNKi4JhEdD7f8>
#> Saved locally as:
#> • 'markdown-googledrive.md'
cat(paste0(readLines("markdown-googledrive.md"), "\n"))
#> # Title
#>
#> 1+1
#>
#> textCreated on 2025-09-10 with reprex v2.1.1.9000 |
|
If I send this markdown: then re-download it, I get: The code fences are retained. But the language identifier for Screen.Recording.2025-09-10.at.3.26.47.PM.movIt would be great to include R there, since it is more popular than some of these languages. But it's beyond the scope of what googledrive can do. |
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
.mdextension fortext/markdownmimetype indata-raw/extension-mime-type-defaults.csv. Then I just randata-raw/mime-types.Rto pull in the Google MIME types and create the csv files ininst/extdata/data/.This does work now:
Created on 2025-07-09 with reprex v2.1.1