Skip to content

Commit 471fa2d

Browse files
committed
Tweaks for upload/download
1 parent 117674f commit 471fa2d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

R/gridfs.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fs_object <- function(fs, client, orig){
8282
find <- function(filter = '{}', options = '{}'){
8383
mongo_gridfs_find(fs, filter, options)
8484
}
85-
upload <- function(path, name = basename(path), content_type = NULL, metadata = NULL){
85+
upload <- function(path, name = path, content_type = NULL, metadata = NULL){
8686
mongo_gridfs_upload(fs, name, path, content_type, metadata)
8787
}
8888
download <- function(name, path = name){
@@ -130,6 +130,9 @@ mongo_gridfs_find <- function(fs, filter, opts){
130130
mongo_gridfs_upload <- function(fs, name, path, type, metadata){
131131
stopifnot(is.character(name))
132132
path <- normalizePath(path, mustWork = TRUE)
133+
is_dir <- file.info(path)$isdir
134+
if(any(is_dir))
135+
stop(sprintf("Upload contains directories, you can only upload files (%s)", paste(path[is_dir], collapse = ", ")))
133136
stopifnot(length(name) == length(path))
134137
id <- rep(NA, length(name))
135138
if(is.null(type))
@@ -147,13 +150,13 @@ mongo_gridfs_upload <- function(fs, name, path, type, metadata){
147150
mongo_gridfs_download <- function(fs, name, path){
148151
stopifnot(is.character(name))
149152
path <- normalizePath(path, mustWork = FALSE)
153+
lapply(path, function(x){ dir.create(dirname(x), showWarnings = FALSE, recursive = TRUE)})
150154
stopifnot(length(name) == length(path))
151-
out <- rep(NA, length(name))
155+
out <- vector("list", length(name))
152156
for(i in seq_along(name)){
153-
res <- .Call(R_mongo_gridfs_download, fs, name[i], path[i])
154-
out[i] <- res$id
157+
out[[i]] <- .Call(R_mongo_gridfs_download, fs, name[i], path[i])
155158
}
156-
structure(out, names = name)
159+
do.call(rbind.data.frame, out)
157160
}
158161

159162
#' @useDynLib mongolite R_mongo_gridfs_remove

0 commit comments

Comments
 (0)