-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdrive_rm.R
85 lines (80 loc) · 2.54 KB
/
drive_rm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#' Delete files from Drive
#'
#' Caution: this will permanently delete your files! For a safer, reversible
#' option, see [drive_trash()].
#'
#' @seealso Wraps the `files.delete` endpoint:
#' * <https://developers.google.com/drive/api/v3/reference/files/delete>
#'
#' @param ... One or more Drive files, specified in any valid way, i.e. as a
#' [`dribble`], by name or path, or by file id or URL marked with [as_id()]. Or
#' any combination thereof. Elements are processed with [as_dribble()] and
#' row-bound prior to deletion.
#' @template verbose
#'
#' @return Logical vector, indicating whether the delete succeeded.
#' @export
#'
#' @examplesIf drive_has_token()
#' # Target one of the official example files to copy (then remove)
#' (src_file <- drive_example_remote("chicken.txt"))
#'
#' # Create a copy, then remove it by name
#' src_file %>%
#' drive_cp(name = "chicken-rm.txt")
#' drive_rm("chicken-rm.txt")
#'
#' # Create several more copies
#' x1 <- src_file %>%
#' drive_cp(name = "chicken-abc.txt")
#' drive_cp(src_file, name = "chicken-def.txt")
#' x2 <- src_file %>%
#' drive_cp(name = "chicken-ghi.txt")
#'
#' # Remove the copies all at once, specified in different ways
#' drive_rm(x1, "chicken-def.txt", as_id(x2))
drive_rm <- function(..., verbose = deprecated()) {
warn_for_verbose(verbose)
dots <- list(...)
if (length(dots) == 0) {
dots <- list(NULL)
}
# explicitly select on var name to exclude 'path', if present
file <- map(dots, ~ as_dribble(.x)[c("name", "id", "drive_resource")])
file <- vec_rbind(!!!file)
# filter to the unique file ids (multiple parents mean drive_get() and
# therefore as_dribble() can return >1 row representing a single file)
file <- file[!duplicated(file$id), ]
if (no_file(file)) {
drive_bullets(c(
"!" = "No such file to delete."
))
return(invisible(file))
}
out <- map_lgl(file$id, delete_one)
if (any(out)) {
successes <- file[out, ]
drive_bullets(c(
"File{?s} deleted:{cli::qty(nrow(successes))}",
bulletize(gargle_map_cli(successes))
))
}
# I'm not sure this ever comes up IRL?
# Is it even possible that removal fails but there's no error?
if (any(!out)) {
failures <- file[!out, ]
drive_bullets(c(
"File{?s} NOT deleted:{cli::qty(nrow(failures))}",
bulletize(gargle_map_cli(failures))
))
}
invisible(out)
}
delete_one <- function(id) {
request <- request_generate(
endpoint = "drive.files.delete",
params = list(fileId = id)
)
response <- request_make(request)
gargle::response_process(response)
}