R/upload_item_to_sharepoint.R

Defines functions upload_item_to_sharepoint

Documented in upload_item_to_sharepoint

#' Upload one file or folder to a SharePoint drive
#'
#' Given a local file path, the file is uploaded to
#'
#' @param local_file_path The relative or absolute path to an existing local
#'   file to upload.
#' @param drive_path A SharePoint folder, as returned by `get_item` or
#'   `create_folder` run on a SharePoint Drive.
#' @return NULL
#' @export
upload_item_to_sharepoint <- function(local_file_path,
                                      dst,
                                      local_root_rel = "inst/reports") {
  local_root_abs <- here::here(local_root_rel)

  # Catch file does not exist
  if (!fs::file_exists(local_file_path)) {
    "File {} does not exist, skipping" %>%
      glue::glue() %>%
      wastdr::wastdr_msg_warn()
    return()
  }

  # Destination path shall exclude abs path up to local_root_rel
  dest_pth <- local_file_path %>%
    fs::path_dir() %>%
    stringr::str_replace(local_root_abs, "")

  dest_fn <- "{dest_pth}/{fs::path_file(local_file_path)}" %>%
    glue::glue() %>%
    as.character()

  "Target {dest_fn}" %>%
    glue::glue() %>%
    wastdr::wastdr_msg_info()

  # Catch file already exists
  if ("ms_drive_item" %in% try(class(dst$get_item(dest_fn)), silent = TRUE)) {
    "Skip existing {dest_fn}" %>%
      glue::glue() %>%
      wastdr::wastdr_msg_success()
    return()
  }

  if (fs::is_dir(local_file_path)) {
    x <- dst$create_folder(dest_fn)
    "Created folder {dest_fn}" %>%
      glue::glue() %>%
      wastdr::wastdr_msg_success()
  } else {
    x <- dst$upload(src = local_file_path, dest = dest_fn)
    "Created file {dest_fn}" %>%
      glue::glue() %>%
      wastdr::wastdr_msg_success()
  }
}
dbca-wa/etlTurtleNesting documentation built on Nov. 18, 2022, 8:03 a.m.