@@ -75,9 +75,16 @@ query_layer_attachments <- function(
75
75
76
76
req <- httr2 :: req_body_form(
77
77
b_req ,
78
- objectIds = object_ids ,
79
- globalIds = global_ids ,
78
+ # TODO test these why aren't these working?!!
79
+ objectIds = paste(object_ids , collapse = " ," ),
80
+ # TODO create a test for this
81
+ globalIds = paste(global_ids , collapse = " ," ),
80
82
attachmentTypes = attachment_types ,
83
+ # TODO document common examples:
84
+ # filter based on
85
+ # - date
86
+ # - common prefix
87
+ # - numeric value
81
88
definitionExpression = definition_expression ,
82
89
keywords = keywords ,
83
90
returnUrl = TRUE ,
@@ -96,6 +103,11 @@ query_layer_attachments <- function(
96
103
# ' @param x the attachmentGroups column from the query results
97
104
# ' @noRd
98
105
unnest_attachment_groups <- function (x ) {
106
+ if (is.null(x [[" attachmentInfos" ]])) {
107
+ cli :: cli_alert_info(" No attachments found." )
108
+ return (NULL )
109
+ }
110
+
99
111
n_elem <- vapply(x [[" attachmentInfos" ]], nrow , integer(1 ))
100
112
res <- cbind(
101
113
x [rep.int(1 : nrow(x ), n_elem ), c(" parentGlobalId" , " parentObjectId" )],
@@ -128,6 +140,7 @@ download_attachments <- function(
128
140
attachments ,
129
141
out_dir ,
130
142
... ,
143
+ overwrite = FALSE ,
131
144
.progress = TRUE ,
132
145
token = arc_token()) {
133
146
# check that the input is a data frame with the appropriate types
@@ -195,9 +208,32 @@ download_attachments <- function(
195
208
196
209
# create the output path names
197
210
out_fps <- file.path(out_dir , attachments [[" name" ]])
211
+ already_exist <- file.exists(out_fps )
212
+
213
+ # required fields
214
+ urls <- attachments [[" url" ]]
215
+ content_types <- attachments [[" contentType" ]]
216
+
217
+ if (! overwrite && any(already_exist )) {
218
+ # Files with the same name found.
219
+ cli :: cli_inform(
220
+ c(
221
+ " Files with the same name found in {.path {out_dir}}" ,
222
+ " i" = " Existing files: {.file {out_fps[already_exist]}}" ,
223
+ " >" = " set {.arg overwrite = TRUE} to overwrite these files"
224
+ )
225
+ )
226
+ # subset to the files that dont already exist
227
+ out_fps <- out_fps [! already_exist ]
228
+ urls <- urls [! already_exist ]
229
+ content_types <- content_types [! already_exist ]
230
+ }
231
+ # TODO check if files exist, if they do provide a message
232
+ # saying we're skipping downloading.
233
+ # To download use `overwrite = TRUE`
198
234
199
235
# create the requests
200
- attachment_reqs <- lapply(attachments [[ " url " ]] , arc_base_req , token = token )
236
+ attachment_reqs <- lapply(urls , arc_base_req , token = token )
201
237
202
238
# perform the requests
203
239
resps <- httr2 :: req_perform_parallel(
@@ -207,7 +243,7 @@ download_attachments <- function(
207
243
)
208
244
209
245
210
- Map(.download_attachment , resps , attachments [[ " contentType " ]] , out_fps )
246
+ Map(.download_attachment , resps , content_types , out_fps )
211
247
}
212
248
213
249
.download_attachment <- function (.resp , .content_type , .fp ) {
0 commit comments