From 6205f024365666edd5b8493dc3d024ac3c71b08d Mon Sep 17 00:00:00 2001 From: your name Date: Mon, 24 Jun 2024 13:53:21 -0400 Subject: [PATCH] Add `upload_sp_items()` function --- NAMESPACE | 1 + NEWS.md | 2 ++ R/write_sharepoint.R | 82 +++++++++++++++++++++++++++++++++++++++++++ man/upload_sp_item.Rd | 3 ++ 4 files changed, 88 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 9e09ea6..75989a7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,6 +45,7 @@ export(sp_url_parse_path) export(sp_url_parse_query) export(update_sp_list_item) export(upload_sp_item) +export(upload_sp_items) export(write_sharepoint) import(rlang) importFrom(Microsoft365R,get_sharepoint_site) diff --git a/NEWS.md b/NEWS.md index cd750d9..8d0e348 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # sharepointr (development version) +* Add `upload_sp_items()` function (2024-06-24). + # sharepointr 0.1.0 * Initial version. diff --git a/R/write_sharepoint.R b/R/write_sharepoint.R index df9b506..220ff96 100644 --- a/R/write_sharepoint.R +++ b/R/write_sharepoint.R @@ -1,3 +1,12 @@ +#' @noRd +sp_url_as_src_dest <- function(url, src, call = caller_env()) { + sp_url_parts <- sp_url_parse(url, call = call) + file_path <- sp_url_parts[["file_path"]] |> + str_remove_slash() + + str_c_url(file_path, basename(src)) +} + #' Write an object to file and upload file to SharePoint #' #' @description @@ -166,6 +175,79 @@ upload_sp_item <- function(file = NULL, } } + upload_sp_src( + src = src, + dest = dest, + drive = drive, + overwrite = overwrite, + blocksize = blocksize, + recursive = recursive, + parallel = parallel, + call = call + ) +} + +#' @rdname upload_sp_item +#' @export +upload_sp_items <- function(file = NULL, + dest, + ..., + src = NULL, + call = caller_env()) { + if (!is_character(src)) { + if (!is_character(file)) { + cli_abort( + "One of {.arg file} or {.arg src} must be a character vector", + call = call + ) + } + + src <- file + } + + check_character(file, call = call) + + if (is_url(dest) && has_length(dest, 1)) { + dest <- map_chr( + src, + \(x) { + sp_url_as_src_dest( + url = dest, + src = x + ) + } + ) + } + + dest <- vctrs::vec_recycle( + dest, + size = length(src) + ) + + dest_list <- map2_chr( + src, dest, + \(x, y) { + upload_sp_src( + src = x, + dest = y, + ..., + call = call + ) + } + ) + + invisible(dest_list) +} + +#' @noRd +upload_sp_src <- function(src, + dest = NULL, + drive = NULL, + blocksize = 327680000, + recursive = FALSE, + parallel = FALSE, + overwrite = FALSE, + call = caller_env()) { if (dir.exists(src)) { cli_progress_step( msg = "Uploading directory to SharePoint drive", diff --git a/man/upload_sp_item.Rd b/man/upload_sp_item.Rd index 1c49408..2ec857e 100644 --- a/man/upload_sp_item.Rd +++ b/man/upload_sp_item.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/write_sharepoint.R \name{upload_sp_item} \alias{upload_sp_item} +\alias{upload_sp_items} \title{Upload a file or folder to a SharePoint Drive} \usage{ upload_sp_item( @@ -18,6 +19,8 @@ upload_sp_item( parallel = FALSE, call = caller_env() ) + +upload_sp_items(file = NULL, dest, ..., src = NULL, call = caller_env()) } \arguments{ \item{file}{Path for file or directory to upload. Optional if \code{src} is