Skip to content

Commit

Permalink
Support Alternate File Upload Name (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mad-statter committed Jan 23, 2023
1 parent 56bc6d5 commit 28561ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -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)
8 changes: 6 additions & 2 deletions R/form.R
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion man/multipart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions src/form.c
Expand Up @@ -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
Expand Down

0 comments on commit 28561ad

Please sign in to comment.