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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/utils-paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ file_ext_safe <- function(x) {

## add an extension if it is not already present
apply_extension <- function(path, ext) {
if (is.na(ext) || ext == "") {
return(path)
}
ext_orig <- file_ext_safe(path)
if (!identical(ext, ext_orig)) {
path <- paste(path, ext, sep = ".")
Expand Down
3 changes: 2 additions & 1 deletion data-raw/extension-mime-type-defaults.csv
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ text/csv,csv
application/vnd.oasis.opendocument.presentation,odp
text/richtext,rtx
application/zip,zip
image/svg+xml,svg
image/svg+xml,svg
text/markdown,md
31 changes: 21 additions & 10 deletions inst/extdata/data/mime_tbl.csv
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
mime_type,ext,description,human_type,default
application/epub+zip,NA,NA,NA,NA
application/epub+zip,epub,NA,epub,FALSE
application/msword,doc,NA,doc,TRUE
application/msword,dot,NA,dot,FALSE
application/pdf,pdf,NA,pdf,TRUE
application/rtf,rtf,NA,rtf,TRUE
application/vnd.google-apps.audio,NA,NA,audio,NA
application/vnd.google-apps.document,NA,Google Docs,document,NA
application/vnd.google-apps.drawing,NA,Google Drawing,drawing,NA
application/vnd.google-apps.drive-sdk,NA,3rd party shortcut,drive-sdk,NA
application/vnd.google-apps.drawing,NA,Google Drawings,drawing,NA
application/vnd.google-apps.drive-sdk,NA,Third-party shortcut,drive-sdk,NA
application/vnd.google-apps.file,NA,Google Drive file,file,NA
application/vnd.google-apps.folder,NA,Google Drive folder,folder,NA
application/vnd.google-apps.form,NA,Google Forms,form,NA
application/vnd.google-apps.fusiontable,NA,Google Fusion Tables,fusiontable,NA
application/vnd.google-apps.jam,NA,Google Jamboard,jam,NA
application/vnd.google-apps.mail-layout,NA,Email layout,mail-layout,NA
application/vnd.google-apps.map,NA,Google My Maps,map,NA
application/vnd.google-apps.photo,NA,NA,photo,NA
application/vnd.google-apps.photo,NA,Google Photos,photo,NA
application/vnd.google-apps.presentation,NA,Google Slides,presentation,NA
application/vnd.google-apps.script,NA,Google Apps Scripts,script,NA
application/vnd.google-apps.script,NA,Google Apps Script,script,NA
application/vnd.google-apps.script+json,NA,NA,script+json,NA
application/vnd.google-apps.script+text/plain,NA,NA,script+text/plain,NA
application/vnd.google-apps.shortcut,NA,Shortcut,shortcut,NA
application/vnd.google-apps.site,NA,Google Sites,site,NA
application/vnd.google-apps.spreadsheet,NA,Google Sheets,spreadsheet,NA
application/vnd.google-apps.unknown,NA,NA,unknown,NA
application/vnd.google-apps.vid,NA,Google Vids,vid,NA
application/vnd.google-apps.video,NA,NA,video,NA
application/vnd.google.colab,ipynb,Colab notebook,colab,TRUE
application/vnd.ms-excel,xlb,NA,xlb,FALSE
application/vnd.ms-excel,xla,NA,xla,FALSE
application/vnd.ms-excel,xlc,NA,xlc,FALSE
application/vnd.ms-excel,xlm,NA,xlm,FALSE
application/vnd.ms-excel,xls,NA,xls,TRUE
application/vnd.ms-excel,xlt,NA,xlt,FALSE
application/vnd.ms-excel,xlw,NA,xlw,FALSE
application/vnd.ms-excel.sheet.macroenabled.12,NA,NA,NA,NA
application/vnd.ms-excel.template.macroenabled.12,NA,NA,NA,NA
application/vnd.ms-powerpoint,pps,NA,pps,FALSE
Expand All @@ -52,8 +57,9 @@ application/x-vnd.oasis.opendocument.presentation,NA,NA,NA,NA
application/x-vnd.oasis.opendocument.spreadsheet,NA,NA,NA,NA
application/x-vnd.oasis.opendocument.text,NA,NA,NA,NA
application/zip,zip,NA,zip,TRUE
image/bmp,NA,NA,NA,NA
image/bmp,bmp,NA,bmp,FALSE
image/gif,gif,NA,gif,TRUE
image/jpeg,jfif,NA,jfif,FALSE
image/jpeg,jpe,NA,jpe,FALSE
image/jpeg,jpeg,NA,jpeg,TRUE
image/jpeg,jpg,NA,jpg,FALSE
Expand All @@ -68,12 +74,17 @@ text/csv,csv,NA,csv,TRUE
text/html,htm,NA,htm,FALSE
text/html,html,NA,html,TRUE
text/html,shtml,NA,shtml,FALSE
text/plain,asc,NA,asc,FALSE
text/markdown,markdown,NA,markdown,FALSE
text/markdown,md,NA,md,TRUE
text/plain,brf,NA,brf,FALSE
text/plain,pot,NA,pot,FALSE
text/plain,srt,NA,srt,FALSE
text/plain,text,NA,text,FALSE
text/plain,txt,NA,txt,TRUE
text/richtext,rtx,NA,rtx,TRUE
text/richtext,NA,NA,NA,NA
text/rtf,NA,NA,NA,NA
text/tab-separated-values,tsv,NA,tsv,TRUE
text/x-markdown,NA,NA,NA,NA
video/mp4,m4v,NA,m4v,FALSE
video/mp4,mp4,NA,mp4,FALSE
video/mp4,mpg4,NA,mpg4,FALSE
6 changes: 6 additions & 0 deletions inst/extdata/data/translate_mime_types.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ export,application/vnd.google-apps.document,application/vnd.oasis.opendocument.t
export,application/vnd.google-apps.document,application/vnd.openxmlformats-officedocument.wordprocessingml.document,TRUE
export,application/vnd.google-apps.document,application/zip,FALSE
export,application/vnd.google-apps.document,text/html,FALSE
export,application/vnd.google-apps.document,text/markdown,FALSE
export,application/vnd.google-apps.document,text/plain,FALSE
export,application/vnd.google-apps.document,text/x-markdown,FALSE
export,application/vnd.google-apps.drawing,application/pdf,FALSE
export,application/vnd.google-apps.drawing,image/jpeg,FALSE
export,application/vnd.google-apps.drawing,image/png,TRUE
export,application/vnd.google-apps.drawing,image/svg+xml,FALSE
export,application/vnd.google-apps.form,application/zip,TRUE
export,application/vnd.google-apps.jam,application/pdf,FALSE
export,application/vnd.google-apps.mail-layout,text/plain,FALSE
export,application/vnd.google-apps.presentation,application/pdf,FALSE
export,application/vnd.google-apps.presentation,application/vnd.oasis.opendocument.presentation,FALSE
export,application/vnd.google-apps.presentation,application/vnd.openxmlformats-officedocument.presentationml.presentation,TRUE
Expand All @@ -26,6 +29,7 @@ export,application/vnd.google-apps.spreadsheet,application/x-vnd.oasis.opendocum
export,application/vnd.google-apps.spreadsheet,application/zip,FALSE
export,application/vnd.google-apps.spreadsheet,text/csv,FALSE
export,application/vnd.google-apps.spreadsheet,text/tab-separated-values,FALSE
export,application/vnd.google-apps.vid,video/mp4,FALSE
import,application/vnd.google-apps.document,application/msword,NA
import,application/vnd.google-apps.document,application/pdf,NA
import,application/vnd.google-apps.document,application/rtf,NA
Expand All @@ -45,9 +49,11 @@ import,application/vnd.google-apps.document,image/png,NA
import,application/vnd.google-apps.document,image/x-bmp,NA
import,application/vnd.google-apps.document,image/x-png,NA
import,application/vnd.google-apps.document,text/html,NA
import,application/vnd.google-apps.document,text/markdown,NA
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
import,application/vnd.google-apps.drawing,application/x-msmetafile,NA
import,application/vnd.google-apps.presentation,application/vnd.ms-powerpoint,NA
import,application/vnd.google-apps.presentation,application/vnd.ms-powerpoint.presentation.macroenabled.12,NA
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/drive_download.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@
Saved locally as:
* '{download_filename}'

# drive_download() respects `path` when `type` specified and no default extension

Code
write_utf8(drive_download_message)
Output
File downloaded:
* '{file_to_download}' <id: {FILE_ID}>
Saved locally as:
* '{download_filename}'

31 changes: 31 additions & 0 deletions tests/testthat/test-drive_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,34 @@ test_that("drive_download() converts using default MIME type, if necessary", {

expect_true(file.exists(file.path(tmpdir, download_filename)))
})

test_that("drive_download() respects `path` when `type` specified and no default extension", {
skip_if_no_token()
skip_if_offline()

file_to_download <- nm_("DESC-doc")
tmpdir <- withr::local_tempdir(file_to_download)
download_filename <- paste0(file_to_download, ".md")
local_drive_loud_and_wide()

drive_download_message <- capture.output(
withr::with_dir(
tmpdir,
drive_download(
file = file_to_download,
type = "text/x-markdown",
path = download_filename
)
),
type = "message"
)
drive_download_message <- drive_download_message %>%
scrub_filepath(download_filename) %>%
scrub_filepath(file_to_download) %>%
scrub_file_id()
expect_snapshot(
write_utf8(drive_download_message)
)

expect_true(file.exists(file.path(tmpdir, download_filename)))
})