From 28561ad5e08a0ce56592b1175cd3a8c4064dcf9a Mon Sep 17 00:00:00 2001 From: Matthew Schuelke Date: Mon, 23 Jan 2023 12:12:24 -0600 Subject: [PATCH] Support Alternate File Upload Name (#290) --- DESCRIPTION | 2 +- R/form.R | 8 ++++++-- man/multipart.Rd | 4 +++- src/form.c | 18 ++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 07f9185c..ffc3ee7d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,7 @@ Suggests: VignetteBuilder: knitr Depends: R (>= 3.0.0) -RoxygenNote: 7.2.1 +RoxygenNote: 7.2.3 Encoding: UTF-8 Language: en-US Roxygen: list(markdown = TRUE) diff --git a/R/form.R b/R/form.R index d3e964c1..d3fbb992 100644 --- a/R/form.R +++ b/R/form.R @@ -6,15 +6,16 @@ #' #' @param path a string with a path to an existing file on disk #' @param type MIME content-type of the file. +#' @param name a string with the file name to use for the upload #' @export #' @name multipart #' @rdname multipart -form_file <- function(path, type = NULL){ +form_file <- function(path, type = NULL, name = NULL) { path <- enc2native(normalizePath(path[1], mustWork = TRUE)) if(!is.null(type)){ stopifnot(is.character(type)) } - structure(list(path = path, type = type), class = "form_file") + structure(list(path = path, type = type, name = name), class = "form_file") } #' @export @@ -32,6 +33,9 @@ form_data <- function(value, type = NULL){ #' @export print.form_file <- function(x, ...){ txt <- paste("Form file:", basename(x$path)) + if(!is.null(x$name)) { + txt <- sprintf("%s => %s", txt, x$name) + } if(!is.null(x$type)){ txt <- sprintf("%s (type: %s)", txt, x$type) } diff --git a/man/multipart.Rd b/man/multipart.Rd index 1fdff979..e5b4e498 100644 --- a/man/multipart.Rd +++ b/man/multipart.Rd @@ -6,7 +6,7 @@ \alias{form_data} \title{POST files or data} \usage{ -form_file(path, type = NULL) +form_file(path, type = NULL, name = NULL) form_data(value, type = NULL) } @@ -15,6 +15,8 @@ form_data(value, type = NULL) \item{type}{MIME content-type of the file.} +\item{name}{a string with the file name to use for the upload} + \item{value}{a character or raw vector to post} } \description{ diff --git a/src/form.c b/src/form.c index 0db1d8de..d2a788b5 100644 --- a/src/form.c +++ b/src/form.c @@ -19,12 +19,22 @@ struct curl_httppost* make_form(SEXP form){ } else if(isVector(val) && Rf_length(val)){ if(isString(VECTOR_ELT(val, 0))){ //assume a form_file upload - const char * path = CHAR(asChar(VECTOR_ELT(val, 0))); - if(isString(VECTOR_ELT(val, 1))){ + const char *path = CHAR(asChar(VECTOR_ELT(val, 0))); + if(isString(VECTOR_ELT(val, 1))) { const char *content_type = CHAR(asChar(VECTOR_ELT(val, 1))); - curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_CONTENTTYPE, content_type, CURLFORM_END); + if(isString(VECTOR_ELT(val, 2))) { + const char *file_name = CHAR(asChar(VECTOR_ELT(val, 2))); + curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_CONTENTTYPE, content_type, CURLFORM_FILENAME, file_name, CURLFORM_END); + } else { + curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_CONTENTTYPE, content_type, CURLFORM_END); + } } else { - curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_END); + if(isString(VECTOR_ELT(val, 2))) { + const char *file_name = CHAR(asChar(VECTOR_ELT(val, 2))); + curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_FILENAME, file_name, CURLFORM_END); + } else { + curl_formadd(&post, &last, CURLFORM_COPYNAME, name, CURLFORM_FILE, path, CURLFORM_END); + } } } else { //assume a form_value upload