diff --git a/NEWS.md b/NEWS.md index 9cf8e20..631c981 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ - added `get_username()` as exported function. - added `get_cached_token()` as exported function. - added `token_stache_path()` as exported function. +- fixed bug related to file downloads #332 and #335 # version 1.3.2 diff --git a/R/gql_helpers.R b/R/gql_helpers.R index b76cad9..fe60b01 100644 --- a/R/gql_helpers.R +++ b/R/gql_helpers.R @@ -86,3 +86,16 @@ delete_item_query <- function(id) { run_gql_query(query, httr::handle(url = pkg.env$graphql_url), json = json) } +# {"operationName":"DeleteQuery","variables":{"input":{"cuid":null,"key":"65cbc0b3d34ef4b119cb37e9/rf1.csv"}},"query":"mutation DeleteQuery($input: DeleteFileInput!) {\n deleteFile(input: $input) {\n id\n __typename\n }\n}\n"} +delete_file_query <- function(id, cuid, file) { + query <- "mutation DeleteQuery($input: DeleteFileInput!) {\n deleteFile(input: $input) {\n id\n __typename\n }\n}\n" + + variables <- list(input = list(cuid = cuid, key = paste0(id, "/", file))) + + json = jsonlite::toJSON(list(operationName = "DeleteQuery", + query = query, + variables = variables), + auto_unbox = TRUE, null = 'null') + + run_gql_query(query, httr::handle(url = pkg.env$graphql_url), json = json) +} diff --git a/R/item_replace_files.R b/R/item_replace_files.R index 14565aa..8155bb4 100644 --- a/R/item_replace_files.R +++ b/R/item_replace_files.R @@ -24,7 +24,7 @@ item_replace_files <- function(sb_id, files, ..., all=FALSE, if(all){ item <- item_rm_files(sb_id, ...) }else{ - item <- item_rm_files(sb_id, files, ...) + item <- item_rm_files(sb_id, basename(files), ...) } if(!is.null(item)) diff --git a/R/item_rm.R b/R/item_rm.R index 3fc5d27..e621dd9 100644 --- a/R/item_rm.R +++ b/R/item_rm.R @@ -63,7 +63,7 @@ item_rm = function(sb_id, ..., limit=1000, recursive=FALSE) { #' # then delete the whole folder #' sbtools:::item_rm_recursive(folder) #' } -item_rm_recursive = function(sb_id, ..., limit) { +item_rm_recursive = function(sb_id, ..., limit = 1000) { id <- as.sbitem(sb_id)$id # check args diff --git a/R/item_rm_files.R b/R/item_rm_files.R index e226ef5..70f8431 100644 --- a/R/item_rm_files.R +++ b/R/item_rm_files.R @@ -27,7 +27,6 @@ #' @export item_rm_files <- function(sb_id, files,...){ - #force a pull of the item to refresh the file info sb_id = as.sbitem(sb_id) if(is.null(sb_id)) return(NULL) @@ -41,18 +40,25 @@ item_rm_files <- function(sb_id, files,...){ #if files not supplied, set files vector to of files is just going to be empty if(missing(files)){ - files_to_keep = vector() + remove <- item$files }else{ #match the names supplied with the names of item files (sticking to basename, might have paths supplied) - fnames = sapply(item$files, function(x)x$name) - files_to_keep = item$files[!fnames %in% basename(files)] - #files_to_keep = lapply(files_to_keep, function(x){x[c('name', 'title', 'contentType')]}) + fnames = sapply(item$files, function(x) x$name) + remove = item$files[fnames %in% basename(files)] } - if(length(files_to_keep) == 0 && is.list(files_to_keep)) { + if(length(remove) == 0 && is.list(0)) { + # nothing to do return(item) } - as.sbitem(item_update(item$id, info = list(files = files_to_keep), ...)) + for(f in remove) { + cuid <- f$cuid + file <- f$name + + delete_file_query(item$id, cuid, file) + } + + return(get_item(sb_id$id)) } diff --git a/demo/00Index b/demo/00Index deleted file mode 100644 index 3cb1151..0000000 --- a/demo/00Index +++ /dev/null @@ -1,2 +0,0 @@ -figure_fault_code figure example showing WFS and query features of sbtool -figure_map_code figure example showing WFS access features diff --git a/demo/figure_fault_code.R b/demo/figure_fault_code.R deleted file mode 100644 index 68e5956..0000000 --- a/demo/figure_fault_code.R +++ /dev/null @@ -1,24 +0,0 @@ -#figure_faultline_code - -library(sbtools) -#Source non-sbtools-required but useful mapping packages -library(sp) -library(maps) - -faults = query_sb(list(q="faults", browseType = "OGC WFS Layer"), limit=20) - -#png('faultlinefig.png', res=300, width=1700, height=1400) -par(mar=c(5,5,1,1), oma=c(0,2,0,0)) -map('usa') -for(i in 1:length(faults)){ - #just to finish fig if there's an HTTP error - tryCatch({ - layer = item_get_wfs(faults[[i]]$id) - layer = spTransform(layer, CRS('+proj=longlat +datum=WGS84')) - plot(layer, add=TRUE, col='red') - }, error=function(e){}) -} -map.axes() -mtext('lon', 1, line=2.2, mex=2) -mtext('lat', 2, line=2.2, mex=2) -#dev.off() diff --git a/demo/figure_map_code.R b/demo/figure_map_code.R deleted file mode 100644 index ee60d6c..0000000 --- a/demo/figure_map_code.R +++ /dev/null @@ -1,18 +0,0 @@ - -library(sbtools) - -#Source non-sbtools-required but useful mapping packages -library(maps) -library(sp) -#an item with an included OGC WFS service -layer = item_get_wfs('55e372b9e4b05561fa208212') - -#png('manuscript/mapfig.png', res=300, width=1700, height=1400) -par(mar=c(5,5,1,1), oma=c(0,2,0,0)) -map('state', regions='iowa', ylab='Lat', xlab='Lon') - -plot(spTransform(layer, CRS("+proj=longlat +datum=WGS84")), add=TRUE) -map.axes(mex=0.2) -mtext('lon', 1, line=2.2, mex=2) -mtext('lat', 2, line=2.2, mex=2) -#dev.off() diff --git a/man/get_username.Rd b/man/get_username.Rd index 72c6b48..2caca3c 100644 --- a/man/get_username.Rd +++ b/man/get_username.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/authenticate_sb.R \name{get_username} \alias{get_username} -\title{get or set sciencebase username} +\title{Get or set ScienceBase username} \usage{ get_username(username = NULL) } diff --git a/man/item_rm_recursive.Rd b/man/item_rm_recursive.Rd index 261055f..90f9988 100644 --- a/man/item_rm_recursive.Rd +++ b/man/item_rm_recursive.Rd @@ -4,7 +4,7 @@ \alias{item_rm_recursive} \title{Remove an item completely by recursively removing its child items} \usage{ -item_rm_recursive(sb_id, ..., limit) +item_rm_recursive(sb_id, ..., limit = 1000) } \arguments{ \item{sb_id}{An \code{\link{sbitem}} object or a character ScienceBase ID corresponding to the item}