Skip to content

Commit 8eacd3f

Browse files
authored
Merge pull request #62 from enriquemondragon/java_download_argument
Closes #61 Check MD5 regardless of quiet flag value
2 parents 5b9a68d + 1f9dc1d commit 8eacd3f

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

R/java_download.R

+28-36
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ java_download <- function(
3232
temp_dir = FALSE
3333
) {
3434

35+
# Download distribution and check MD5 checksum
36+
download_dist_check_md5 <- function(url, dest_file, quiet) {
37+
38+
curl::curl_download(url, dest_file, quiet = FALSE)
39+
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
40+
41+
if (!quiet) {
42+
cli::cli_inform("Download completed.", .envir = environment())
43+
}
44+
md5sum <- tools::md5sum(dest_file)
45+
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
46+
47+
if (md5sum != md5sum_expected) {
48+
cli::cli_abort("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
49+
unlink(dest_file)
50+
return(NULL)
51+
} else {
52+
if (!quiet) {
53+
cli::cli_inform("MD5 checksum verified.", .envir = environment())
54+
}
55+
}
56+
}
57+
3558
# override cache_path if temp_dir is set to TRUE
3659
if (temp_dir) {
3760
temp_dir <- tempdir()
@@ -96,7 +119,6 @@ java_download <- function(
96119
dest_file <- file.path(cache_path, basename(url))
97120
dest_file_md5 <- paste0(file.path(cache_path, basename(url_md5)), ".md5")
98121

99-
100122
if (!quiet) {
101123
cli::cli_inform("Downloading Java {version} ({distribution}) for {platform} {arch} to {dest_file}", .envir = environment())
102124
}
@@ -105,44 +127,14 @@ java_download <- function(
105127
if (!quiet) {
106128
cli::cli_inform("File already exists. Skipping download.", .envir = environment())
107129
}
108-
} else if(file.exists(dest_file) & force){
130+
} else if(file.exists(dest_file) & force) {
109131
if (!quiet) {
110132
cli::cli_inform("Removing existing installation.", .envir = environment())
111-
}
112-
file.remove(dest_file)
113-
curl::curl_download(url, dest_file, quiet = FALSE)
114-
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
115-
if (!quiet) {
116-
cli::cli_inform("Download completed.", .envir = environment())
117-
118-
md5sum <- tools::md5sum(dest_file)
119-
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
120-
121-
if (md5sum != md5sum_expected) {
122-
cli::cli_alert_danger("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
123-
unlink(dest_file)
124-
return(NULL)
125-
} else {
126-
cli::cli_inform("MD5 checksum verified.", .envir = environment())
127-
}
128-
}
129-
} else if (!file.exists(dest_file)){
130-
curl::curl_download(url, dest_file, quiet = FALSE)
131-
curl::curl_download(url_md5, dest_file_md5, quiet = TRUE)
132-
if (!quiet) {
133-
cli::cli_inform("Download completed.", .envir = environment())
134-
135-
md5sum <- tools::md5sum(dest_file)
136-
md5sum_expected <- readLines(dest_file_md5, warn = FALSE)
137-
138-
if (md5sum != md5sum_expected) {
139-
cli::cli_alert_danger("MD5 checksum mismatch. Please try downloading the file again.", .envir = environment())
140-
unlink(dest_file)
141-
return(NULL)
142-
} else {
143-
cli::cli_inform("MD5 checksum verified.", .envir = environment())
144133
}
145-
}
134+
file.remove(dest_file)
135+
download_dist_check_md5(url, dest_file, quiet)
136+
} else if (!file.exists(dest_file)) {
137+
download_dist_check_md5(url, dest_file, quiet)
146138
}
147139

148140
return(dest_file)

0 commit comments

Comments
 (0)