41
41
query_layer_attachments <- function (
42
42
x ,
43
43
definition_expression = " 1=1" ,
44
+ attachments_definition_expression = NULL ,
44
45
object_ids = NULL ,
45
46
global_ids = NULL ,
46
47
attachment_types = NULL ,
@@ -50,6 +51,11 @@ query_layer_attachments <- function(
50
51
# Ignored arguments for now:
51
52
# returnMetadata, size,
52
53
) {
54
+ check_string(definition_expression , allow_null = TRUE )
55
+ check_string(attachments_definition_expression , allow_null = TRUE )
56
+ check_character(global_ids , allow_null = TRUE )
57
+ # TODO validate that object_ids is a vector
58
+
53
59
# ensure that attachments are available.
54
60
if (! x [[" hasAttachments" ]]) {
55
61
cli :: cli_abort(" {.arg layer} does not support attachments." )
@@ -75,12 +81,21 @@ query_layer_attachments <- function(
75
81
76
82
req <- httr2 :: req_body_form(
77
83
b_req ,
78
- objectIds = object_ids ,
79
- globalIds = global_ids ,
84
+ # TODO test these why aren't these working?!!
85
+ objectIds = paste(object_ids , collapse = " ," ),
86
+ # TODO create a test for this
87
+ globalIds = paste(global_ids , collapse = " ," ),
80
88
attachmentTypes = attachment_types ,
89
+ # TODO document common examples:
90
+ # filter based on
91
+ # - date
92
+ # - common prefix
93
+ # - numeric value
81
94
definitionExpression = definition_expression ,
95
+ attachmentsDefinitionExpression = attachments_definition_expression ,
82
96
keywords = keywords ,
83
97
returnUrl = TRUE ,
98
+ returnMetadata = TRUE ,
84
99
f = " json"
85
100
)
86
101
@@ -96,6 +111,11 @@ query_layer_attachments <- function(
96
111
# ' @param x the attachmentGroups column from the query results
97
112
# ' @noRd
98
113
unnest_attachment_groups <- function (x ) {
114
+ if (is.null(x [[" attachmentInfos" ]])) {
115
+ cli :: cli_alert_info(" No attachments found." )
116
+ return (NULL )
117
+ }
118
+
99
119
n_elem <- vapply(x [[" attachmentInfos" ]], nrow , integer(1 ))
100
120
res <- cbind(
101
121
x [rep.int(1 : nrow(x ), n_elem ), c(" parentGlobalId" , " parentObjectId" )],
@@ -128,6 +148,7 @@ download_attachments <- function(
128
148
attachments ,
129
149
out_dir ,
130
150
... ,
151
+ overwrite = FALSE ,
131
152
.progress = TRUE ,
132
153
token = arc_token()) {
133
154
# check that the input is a data frame with the appropriate types
@@ -195,9 +216,32 @@ download_attachments <- function(
195
216
196
217
# create the output path names
197
218
out_fps <- file.path(out_dir , attachments [[" name" ]])
219
+ already_exist <- file.exists(out_fps )
220
+
221
+ # required fields
222
+ urls <- attachments [[" url" ]]
223
+ content_types <- attachments [[" contentType" ]]
224
+
225
+ if (! overwrite && any(already_exist )) {
226
+ # Files with the same name found.
227
+ cli :: cli_inform(
228
+ c(
229
+ " Files with the same name found in {.path {out_dir}}" ,
230
+ " i" = " Existing files: {.file {out_fps[already_exist]}}" ,
231
+ " >" = " set {.arg overwrite = TRUE} to overwrite these files"
232
+ )
233
+ )
234
+ # subset to the files that dont already exist
235
+ out_fps <- out_fps [! already_exist ]
236
+ urls <- urls [! already_exist ]
237
+ content_types <- content_types [! already_exist ]
238
+ }
239
+ # TODO check if files exist, if they do provide a message
240
+ # saying we're skipping downloading.
241
+ # To download use `overwrite = TRUE`
198
242
199
243
# create the requests
200
- attachment_reqs <- lapply(attachments [[ " url " ]] , arc_base_req , token = token )
244
+ attachment_reqs <- lapply(urls , arc_base_req , token = token )
201
245
202
246
# perform the requests
203
247
resps <- httr2 :: req_perform_parallel(
@@ -207,7 +251,7 @@ download_attachments <- function(
207
251
)
208
252
209
253
210
- Map(.download_attachment , resps , attachments [[ " contentType " ]] , out_fps )
254
+ Map(.download_attachment , resps , content_types , out_fps )
211
255
}
212
256
213
257
.download_attachment <- function (.resp , .content_type , .fp ) {
0 commit comments