-
Notifications
You must be signed in to change notification settings - Fork 21
/
item_list_files.R
163 lines (127 loc) · 4.61 KB
/
item_list_files.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#' @title Get list of files attached to SB item
#'
#' @template manipulate_item
#' @param recursive (logical) List files recursively. Default: \code{FALSE}
#' @param fetch_cloud_urls (logical) fetch a tokenized cloud download URLs? Default: \code{TRUE}
#' This option will take slightly longer but the `url` attribute of the returned list will
#' work for direct file downloads or use with pther applications and libraries.
#'
#' @return
#' A data.frame with columns fname, size, url, and facet.
#' If item has no attached files, returns a zero row data.frame.
#'
#' @description
#' Lists all files attached to a SB item. Files can be downloaded
#' from ScienceBase using \code{\link{item_file_download}}.
#'
#' (advanced) Recursive options lists all files attached to an item and all children items.
#'
#' NOTE: A sciencebase item can contain so-called "extensions". The
#' sciencebase item data model refers to the information that describes
#' an extension as a "facet". Some extension facets contain files
#' (such as with a shapefile). The "facet" attribute of the return from
#' this function will contain the name of the facet the file came from
#' if the file was found in a facet.
#'
#' @export
#' @examples \dontrun{
#'
#' #regular files
#' item_list_files("57976a0ce4b021cadec97890")
#'
#' # files in facets
#' item_list_files("5ebe92af82ce476925e44b8f")
#'
#' # list files recursively
#' ## create item
#' id <- item_create(user_id(), title="some title")
#' ## 1. create nested item w/ file
#' file <- system.file("examples", "books.json", package = "sbtools")
#' id2 <- item_create(id, title = "newest-thing")
#' item_upload_create(id2, file)
#' ## 2. create nested item w/ file
#' file <- system.file("examples", "species.json", package = "sbtools")
#' id3 <- item_create(id, title = "a-new-thing")
#' item_upload_create(id3, file)
#' ## 3. create nested item w/ file
#' file <- system.file("examples", "data.csv", package = "sbtools")
#' id4 <- item_create(id, title = "another-thing")
#' item_upload_create(id4, file)
#' item_list_files(id = '56562348e4b071e7ea53e09d', recursive = FALSE) # default
#' item_list_files(id = '56562348e4b071e7ea53e09d', recursive = TRUE)
#' }
item_list_files = function(sb_id, recursive = FALSE, fetch_cloud_urls = TRUE, ...){
session_val()
id <- as.sbitem(sb_id)
item <- item_get(id)
if (recursive) {
if (item$hasChildren) {
children <- list(item)
i <- 1
while (i <= length(children)) {
next_children <- item_list_children(children[[i]])
children <- c(children, next_children)
i <- i + 1
message(paste("Checked child item", i - 1, "found", length(next_children), "more children."))
}
files <- unlist(lapply(children, function(x) x$files), recursive = FALSE)
} else {
files <- list()
message("no child items found")
}
} else {
files <- item$files
}
files <- c(files, get_facet_files(item))
lf <- length(files)
out <- data.frame(fname = rep("", lf),
size = rep(NA_integer_, lf),
url = rep("", lf),
facet = rep("", lf))
cloud <- rep(list(list(cuid = "", key = "",
title = "", useForPreview = "")),
lf)
if (length(files) == 0) {
return(out)
}
for (i in 1:lf) {
out[i,'fname'] = files[[i]]$name
out[i,'size'] = files[[i]]$size
out[i,'url'] = files[[i]]$url
if(!is.null(f <- files[[i]]$facet_name))
out[i, "facet"] = f
if(!is.null(files[[i]]$cuid)) {
cloud[[i]]$cuid <- files[[i]]$cuid
cloud[[i]]$key <- files[[i]]$key
cloud[[i]]$title <- ifelse(!is.null(files[[i]]$title),
files[[i]]$title, "")
cloud[[i]]$useForPreview <- ifelse(!is.null(files[[i]]$useForPreview),
files[[i]]$useForPreview, FALSE)
if(fetch_cloud_urls & is_logged_in()) {
if(!exists("gql")) gql <- httr::handle(url = pkg.env$graphql_url)
message("retrieving S3 URL")
out[i, 'url'] = get_cloud_download_url(
data.frame(cuid = cloud[[i]]$cuid,
key = cloud[[i]]$key,
title = cloud[[i]]$title,
useForPreview = cloud[[i]]$useForPreview),
gql)[[1]]$getS3DownloadUrl$downloadUri[1]
} else if(fetch_cloud_urls & !is_logged_in()) {
warning("Can't fetch cloud URLs as session is not logged in.")
}
}
}
attr(out, "cloud") <- cloud
return(out)
}
get_facet_files <- function(item) {
unlist(lapply(item$facets, function(x) {
lapply(x$files,
function(y, n) {
list(name = y$name,
size = y$size,
url = y$downloadUri,
facet_name = x$name)
}, n = x$name)
}), recursive = FALSE)
}