| 1 |
# For HTML5-capable browsers, file uploads happen through a series of requests. |
| 2 |
# |
| 3 |
# 1. Client tells server that one or more files are about to be uploaded; the |
| 4 |
# server responds with a "job ID" that the client should use for the rest of |
| 5 |
# the upload. |
| 6 |
# |
| 7 |
# 2. For each file (sequentially): |
| 8 |
# a. Client tells server the name, size, and type of the file. |
| 9 |
# b. Client sends server a small-ish blob of data. |
| 10 |
# c. Repeat 2b until the entire file has been uploaded. |
| 11 |
# d. Client tells server that the current file is done. |
| 12 |
# |
| 13 |
# 3. Repeat 2 until all files have been uploaded. |
| 14 |
# |
| 15 |
# 4. Client tells server that all files have been uploaded, along with the |
| 16 |
# input ID that this data should be associated with. |
| 17 |
# |
| 18 |
# Unfortunately this approach will not work for browsers that don't support |
| 19 |
# HTML5 File API, but the fallback approach we would like to use (multipart |
| 20 |
# form upload, i.e. traditional HTTP POST-based file upload) doesn't work with |
| 21 |
# the websockets package's HTTP server at the moment. |
| 22 |
|
| 23 |
# @description Returns a file's extension, with a leading dot, if one can be |
| 24 |
# found. A valid extension contains only alphanumeric characters. If there is |
| 25 |
# no extension, or if it contains non-alphanumeric characters, an empty |
| 26 |
# string is returned. |
| 27 |
# @param x character vector giving file paths. |
| 28 |
# @return The extension of \code{x}, with a leading dot, if one was found. |
| 29 |
# Otherwise, an empty character vector. |
| 30 |
maybeGetExtension <- function(x) { |
| 31 |
ext <- tools::file_ext(x) |
| 32 |
ifelse(ext == "", ext, paste0(".", ext)) |
| 33 |
} |
| 34 |
|
| 35 |
FileUploadOperation <- R6Class( |
| 36 |
'FileUploadOperation', |
| 37 |
portable = FALSE, |
| 38 |
class = FALSE, |
| 39 |
public = list( |
| 40 |
.parent = NULL, |
| 41 |
.id = character(0), |
| 42 |
.files = data.frame(), |
| 43 |
.dir = character(0), |
| 44 |
.currentFileInfo = list(), |
| 45 |
.currentFileData = NULL, |
| 46 |
.pendingFileInfos = list(), |
| 47 |
|
| 48 |
initialize = function(parent, id, dir, fileInfos) { |
| 49 |
.parent <<- parent |
| 50 |
.id <<- id |
| 51 |
.files <<- data.frame(name=character(), |
| 52 |
size=numeric(), |
| 53 |
type=character(), |
| 54 |
datapath=character(), |
| 55 |
stringsAsFactors=FALSE) |
| 56 |
.dir <<- dir |
| 57 |
.pendingFileInfos <<- fileInfos |
| 58 |
}, |
| 59 |
fileBegin = function() { |
| 60 |
if (length(.pendingFileInfos) < 1) |
| 61 |
stop("fileBegin called too many times") |
| 62 |
|
| 63 |
file <- .pendingFileInfos[[1]] |
| 64 |
.currentFileInfo <<- file |
| 65 |
.pendingFileInfos <<- tail(.pendingFileInfos, -1) |
| 66 |
|
| 67 |
fileBasename <- basename(.currentFileInfo$name) |
| 68 |
filename <- file.path(.dir, paste0(as.character(length(.files$name)), maybeGetExtension(fileBasename))) |
| 69 |
row <- data.frame(name=fileBasename, size=file$size, type=file$type, |
| 70 |
datapath=filename, stringsAsFactors=FALSE) |
| 71 |
|
| 72 |
if (length(.files$name) == 0) |
| 73 |
.files <<- row |
| 74 |
else |
| 75 |
.files <<- rbind(.files, row) |
| 76 |
|
| 77 |
.currentFileData <<- file(filename, open='wb') |
| 78 |
}, |
| 79 |
fileChunk = function(rawdata) { |
| 80 |
writeBin(rawdata, .currentFileData) |
| 81 |
}, |
| 82 |
fileEnd = function() { |
| 83 |
close(.currentFileData) |
| 84 |
}, |
| 85 |
finish = function() { |
| 86 |
if (length(.pendingFileInfos) > 0) |
| 87 |
stop("File upload job was stopped prematurely") |
| 88 |
.parent$onJobFinished(.id) |
| 89 |
return(.files) |
| 90 |
} |
| 91 |
) |
| 92 |
) |
| 93 |
|
| 94 |
#' @include map.R |
| 95 |
FileUploadContext <- R6Class( |
| 96 |
'FileUploadContext', |
| 97 |
class = FALSE, |
| 98 |
private = list( |
| 99 |
basedir = character(0), |
| 100 |
operations = 'Map', |
| 101 |
ids = character(0) # Keep track of all ids used for file uploads |
| 102 |
), |
| 103 |
public = list( |
| 104 |
initialize = function(dir=tempdir()) { |
| 105 |
private$basedir <- dir |
| 106 |
private$operations <- Map$new() |
| 107 |
}, |
| 108 |
createUploadOperation = function(fileInfos) { |
| 109 |
while (TRUE) { |
| 110 |
id <- createUniqueId(12) |
| 111 |
private$ids <- c(private$ids, id) |
| 112 |
dir <- file.path(private$basedir, id) |
| 113 |
if (!dir.create(dir)) |
| 114 |
next |
| 115 |
|
| 116 |
op <- FileUploadOperation$new(self, id, dir, fileInfos) |
| 117 |
private$operations$set(id, op) |
| 118 |
return(id) |
| 119 |
} |
| 120 |
}, |
| 121 |
getUploadOperation = function(jobId) { |
| 122 |
private$operations$get(jobId) |
| 123 |
}, |
| 124 |
onJobFinished = function(jobId) { |
| 125 |
private$operations$remove(jobId) |
| 126 |
}, |
| 127 |
# Remove the directories containing file uploads; this is to be called when |
| 128 |
# a session ends. |
| 129 |
rmUploadDirs = function() { |
| 130 |
# Make sure all_paths is underneath the tempdir() |
| 131 |
if (!grepl(normalizePath(tempdir()), normalizePath(private$basedir), fixed = TRUE)) { |
| 132 |
stop("Won't remove upload path ", private$basedir, |
| 133 |
"because it is not under tempdir(): ", tempdir()) |
| 134 |
} |
| 135 |
|
| 136 |
all_paths <- file.path(private$basedir, private$ids) |
| 137 |
unlink(all_paths, recursive = TRUE) |
| 138 |
} |
| 139 |
) |
| 140 |
) |