@@ -46,10 +46,11 @@ SearchResults <- R6::R6Class("SearchResults",
4646 # ' @param selected_indexes Optional; indices of the specific results to download.
4747 # ' @param stop_at_failure Optional; controls whether the download process of multiple files should immediately stop upon encountering the first failure.
4848 # ' @param force Optional; forces the download even if the file already exists in the specified output directory.
49+ # ' @param prompt Optional; enables all user prompts for decisions during file downloads. Defaults to true.
4950 # ' @return Nothing returned but downloaded files are saved at the specified location.
5051 # ' @export
51- download = function (output_dir , selected_indexes , stop_at_failure = TRUE , force = FALSE ) {
52- if (self $ total_count == 0 || ! private $ prompt_user_confirmation(self $ total_size )) {
52+ download = function (output_dir , selected_indexes , stop_at_failure = TRUE , force = FALSE , prompt = TRUE ) {
53+ if (self $ total_count == 0 || ( prompt && ! private $ prompt_user_confirmation(self $ total_size ) )) {
5354 return (NULL )
5455 }
5556
@@ -127,24 +128,8 @@ SearchResults <- R6::R6Class("SearchResults",
127128 },
128129 download_resource = function (download_id , output_dir , force = FALSE ) {
129130 url <- paste0(private $ client $ apiUrl , " /dataaccess/download/" , download_id )
130- req <- httr2 :: request(url ) %> %
131- httr2 :: req_method(" GET" )
132-
133131
134- resp <- private $ client $ send_request(req , TRUE )
135- if (resp $ status_code != 200 ) {
136- stop(paste(" Couldn't download: " , url ))
137- }
138-
139- # Extract the file name from the Content-Disposition header
140- content_disposition <- httr2 :: resp_headers(resp , " content-disposition" )
141- filename <- if (! is.null(content_disposition )) {
142- gsub(' .*filename="?([^"]+)"?.*' , " \\ 1" , content_disposition )
143- } else {
144- " downloaded_file"
145- }
146-
147- # Define the full file path
132+ filename <- private $ check_resource_name(url )
148133 file_path <- file.path(output_dir , filename )
149134
150135 # Check if the file already exists and force flag is FALSE
@@ -153,8 +138,15 @@ SearchResults <- R6::R6Class("SearchResults",
153138 return (NA )
154139 }
155140
156- writeBin(httr2 :: resp_body_raw(resp ), file_path )
141+ req <- httr2 :: request(url ) %> %
142+ httr2 :: req_method(" GET" )
157143
144+ resp <- private $ client $ send_request(req , TRUE )
145+ if (resp $ status_code != 200 ) {
146+ stop(paste(" Couldn't download: " , url ))
147+ }
148+
149+ writeBin(httr2 :: resp_body_raw(resp ), file_path )
158150 return (NA )
159151 },
160152 prompt_user_confirmation = function (total_size ) {
@@ -172,6 +164,25 @@ SearchResults <- R6::R6Class("SearchResults",
172164 } else {
173165 return (TRUE )
174166 }
167+ },
168+ check_resource_name = function (url ) {
169+ req <- httr2 :: request(url ) %> %
170+ httr2 :: req_method(" HEAD" )
171+
172+ resp <- private $ client $ send_request(req , TRUE )
173+ if (resp $ status_code != 200 ) {
174+ stop(paste(" Couldn't download: " , url ))
175+ }
176+
177+ # Extract the file name from the Content-Disposition header
178+ content_disposition <- httr2 :: resp_headers(resp , " content-disposition" )
179+ filename <- if (! is.null(content_disposition )) {
180+ gsub(' .*filename="?([^"]+)"?.*' , " \\ 1" , content_disposition )
181+ } else {
182+ " downloaded_file"
183+ }
184+
185+ return (filename )
175186 }
176187 )
177188)
0 commit comments