From 68a9af50d84bf00c58574e3708c559dc252f8123 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Wed, 10 Jul 2019 18:11:47 +0530 Subject: added upload function --- plumber.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/plumber.R b/plumber.R index 0f091c8..099661c 100644 --- a/plumber.R +++ b/plumber.R @@ -68,3 +68,27 @@ robust.system <- function (cmd) { unlink(c(stdoutFile, stderrFile)) return(retval) } + + +#* @post /upload +upload <- function(req, res){ + cat("---- New Request ----\n") + # the path where you want to write the uploaded files + file_path <- "/data/home/r_on_cloud/R_on_Cloud_Web_API/" + # strip the filename out of the postBody + file_name <- gsub('\"', "", substr(req$postBody[2], 55, 1000)) + # need the length of the postBody so we know how much to write out + file_length <- length(req$postBody)-1 + # first five lines of the post body contain metadata so are ignored + file_content <- req$postBody[5:file_length] + # build the path of the file to write + file_to_write <- paste0(file_path, file_name) + # write file out with no other checks at this time + write(file_content, file = file_to_write) + # print logging info to console + cat("File", file_to_write, "uploaded\n") + # return file path &name to user + ro <- file_to_write + r<- list(status = "SUCCESS", code = "200", output = ro) + return (r) +} -- cgit From 60028c490bb863a40bd6333e0c94406b8bb4e706 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Wed, 10 Jul 2019 18:13:16 +0530 Subject: added new package --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d431377..2954027 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ R version 3.4.4 install.packages("readr") install.packages("futile.logger") install.packages("tryCatchLog") + install.packages("ggplot2") ------------------------------------------ > library(plumber) > r <- plumb("plumber.R") # Where 'plumber.R' is the location of the file shown above -- cgit From ace97e1bf64c0fe3d6226d27dc83aa0a49a81e94 Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Mon, 15 Jul 2019 17:53:10 +0530 Subject: updated upload file function --- plumber.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plumber.R b/plumber.R index 099661c..23031d8 100644 --- a/plumber.R +++ b/plumber.R @@ -5,7 +5,6 @@ library(readr) library(futile.logger) library(tryCatchLog) library(ggplot2) - # creare R directory dir.create(file.path("/tmp/R"), showWarnings = FALSE) @@ -23,6 +22,7 @@ function(code="", session_id="", R_file_id="") { # create session directory for user dir.create(file.path("/tmp/R/", session_id), showWarnings = FALSE) + setwd(file.path("/tmp/R/", session_id)) InputFile <- paste("/tmp/R/",session_id,"/", R_file_id,".R", sep="") OutputFile <- paste("/tmp/R/",session_id,"/", R_file_id,".txt", sep="") RunInputFile <- paste("Rscript", InputFile, sep=" ") @@ -73,12 +73,14 @@ robust.system <- function (cmd) { #* @post /upload upload <- function(req, res){ cat("---- New Request ----\n") + session_id <- gsub('\"', "", substr(req$postBody[length(req$postBody)-1], 1, 1000)) + dir.create(file.path("/tmp/R/", session_id), showWarnings = FALSE) # the path where you want to write the uploaded files - file_path <- "/data/home/r_on_cloud/R_on_Cloud_Web_API/" + file_path <- paste("/tmp/R/",session_id,"/", sep="") # strip the filename out of the postBody file_name <- gsub('\"', "", substr(req$postBody[2], 55, 1000)) # need the length of the postBody so we know how much to write out - file_length <- length(req$postBody)-1 + file_length <- length(req$postBody)-5 # first five lines of the post body contain metadata so are ignored file_content <- req$postBody[5:file_length] # build the path of the file to write -- cgit