Skip to content
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# googledrive (development version)

* `as_id()` (and hence `drive_download()`) work for Google Colab links (#459, @MichaelChirico).

# googledrive 2.1.1

* `drive_auth(subject =)` is a new argument that can be used with
Expand Down
7 changes: 4 additions & 3 deletions R/drive_id-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ pillar_shaft.drive_id <- function(x, ...) {

## we anticipate file-id-containing URLs in these forms:
## /d/FILE_ID Drive file
## /drive/FILE_ID Drive file, from Colab
## /folders/FILE_ID Drive folder
## id=FILE_ID uploaded blob
id_regexp <- "(/d/|/folders/|id=)[^/?]+"
id_regexp <- "(/d/|/drive/(?!folders/|u/)|/folders/|id=)[^/]+"

is_drive_url <- function(x) grepl("^http", x) & grepl(id_regexp, x)

Expand All @@ -162,10 +163,10 @@ get_one_id <- function(x) {
return(x)
}

id_loc <- regexpr(id_regexp, x)
id_loc <- regexpr(id_regexp, x, perl = TRUE)
if (id_loc == -1) {
NA_character_
} else {
gsub("/d/|/folders/|id=", "", regmatches(x, id_loc))
gsub("/d/|/drive/|/folders/|id=", "", regmatches(x, id_loc))
}
}
17 changes: 17 additions & 0 deletions tests/testthat/test-drive_id-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,20 @@ test_that("you can't insert invalid strings into a drive_id", {
expect_true(is_drive_id(x))
expect_snapshot(x[2] <- "", error = TRUE)
})

test_that("colab paths (with resource keys) are parsed", {
expect_identical(
as_id("https://colab.research.google.com/drive/1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu?usp=sharing"),
as_id("1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu")
)
# without resoucekey
expect_identical(
as_id("https://colab.research.google.com/drive/1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu#scrollTo=Re4OQJePO-3g"),
as_id("1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu")
)
# as copied from the Drive UI (Share>Copy Link)
expect_identical(
as_id("https://colab.research.google.com/drive/1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu?resourcekey=0-IfkPRsAwVV1-noFH9jD37Q&usp=drive_link"),
as_id("1Dcf35JDcpxXjahcSHVJtQFkBaLhhQWLu")
)
})
Loading