@@ -12,6 +12,12 @@ SearchResults <- R6::R6Class("SearchResults",
1212 # ' @field results Stores the search results data.
1313 results = NULL ,
1414
15+ # ' @field total_count Stores the total count of results' element.
16+ total_count = NULL ,
17+
18+ # ' @field total_size Stores the total size of results
19+ total_size = NULL ,
20+
1521 # ' @description Initializes a new SearchResults object with the specified client, results, and dataset identifier.
1622 # '
1723 # ' @param client An object containing the API client used to interact with the dataset.
@@ -23,6 +29,11 @@ SearchResults <- R6::R6Class("SearchResults",
2329 private $ client <- client
2430 private $ dataset_id <- dataset_id
2531 self $ results <- results
32+
33+ self $ total_count <- length(sapply(results , FUN = function (x ) {
34+ x $ id
35+ }))
36+ self $ total_size <- sum(sapply(results , function (x ) if (! is.null(x $ properties $ size )) x $ properties $ size else 0 ))
2637 },
2738
2839 # ' @description
@@ -33,6 +44,10 @@ SearchResults <- R6::R6Class("SearchResults",
3344 # ' @return Nothing returned but downloaded files are saved at the specified location.
3445 # ' @export
3546 download = function (output_dir = " ." , selected_indexes , stop_at_failure = TRUE ) {
47+ if (self $ total_count == 0 || ! private $ prompt_user_confirmation(self $ total_size )) {
48+ return (NULL )
49+ }
50+
3651 print(" [Download] Start" )
3752
3853 if (! dir.exists(output_dir )) {
@@ -72,6 +87,7 @@ SearchResults <- R6::R6Class("SearchResults",
7287 }
7388 ),
7489 private = list (
90+ LARGE_DOWNLOAD_SIZE = 100 * 1024 * 1024 , # 100MB
7591 client = NULL ,
7692 dataset_id = NULL ,
7793 check_status = function (download_id ) {
@@ -120,7 +136,7 @@ SearchResults <- R6::R6Class("SearchResults",
120136 # Extract the file name from the Content-Disposition header
121137 content_disposition <- httr2 :: resp_headers(resp , " content-disposition" )
122138 filename <- if (! is.null(content_disposition )) {
123- gsub(' .*filename="?([^"]+)"?.*' , ' \\ 1' , content_disposition )
139+ gsub(' .*filename="?([^"]+)"?.*' , " \\ 1" , content_disposition )
124140 } else {
125141 " downloaded_file"
126142 }
@@ -130,6 +146,22 @@ SearchResults <- R6::R6Class("SearchResults",
130146 writeBin(httr2 :: resp_body_raw(resp ), file_path )
131147
132148 return (NA )
149+ },
150+ prompt_user_confirmation = function (total_size ) {
151+ if (total_size > = private $ LARGE_DOWNLOAD_SIZE ) {
152+ repeat {
153+ cat(" The total size is" , humanize :: natural_size(total_size ), " . Do you want to proceed? (Y/N): " )
154+ answer <- tolower(readLines(n = 1 ))
155+ if (answer %in% c(" y" , " n" )) {
156+ return (answer == " y" )
157+ } else {
158+ cat(" Invalid input. Please enter 'Y' or 'N'.\n " )
159+ return (TRUE )
160+ }
161+ }
162+ } else {
163+ return (TRUE )
164+ }
133165 }
134166 )
135167)
0 commit comments