Skip to content

Commit d75729e

Browse files
apply suggestions from code review
1 parent 4dbfb11 commit d75729e

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/main/java/org/cryptomator/integrations/update/DownloadUpdateMechanism.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
public abstract class DownloadUpdateMechanism implements UpdateMechanism<DownloadUpdateInfo> {
2424

25-
private static final Logger LOG = LoggerFactory .getLogger(DownloadUpdateMechanism.class);
25+
private static final Logger LOG = LoggerFactory.getLogger(DownloadUpdateMechanism.class);
2626
private static final String LATEST_VERSION_API_URL = "https://api.cryptomator.org/connect/apps/desktop/latest-version?format=1";
2727
private static final ObjectMapper MAPPER = new ObjectMapper();
2828

@@ -32,7 +32,8 @@ public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpC
3232
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(LATEST_VERSION_API_URL)).build();
3333
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
3434
if (response.statusCode() != 200) {
35-
throw new RuntimeException("Failed to fetch release: " + response.statusCode());
35+
LOG.warn("Failed to fetch release: HTTP {}", response.statusCode());
36+
return null;
3637
}
3738
var release = MAPPER.readValue(response.body(), LatestVersionResponse.class);
3839
return checkForUpdate(currentVersion, release);
@@ -94,7 +95,7 @@ public record LatestVersion(
9495
@JsonIgnoreProperties(ignoreUnknown = true)
9596
public record Asset(
9697
@JsonProperty("name") String name,
97-
@JsonProperty("digest") String digest, // TODO: verify this starts with "sha256:"?
98+
@JsonProperty("digest") String digest,
9899
@JsonProperty("size") long size,
99100
@JsonProperty("downloadUrl") String downloadUrl
100101
) {}
@@ -106,7 +107,9 @@ private class FirstStep extends DownloadUpdateStep {
106107
public FirstStep(Path workDir, DownloadUpdateInfo updateInfo) {
107108
var uri = URI.create(updateInfo.asset().downloadUrl);
108109
var destination = workDir.resolve(updateInfo.asset().name);
109-
var digest = HexFormat.of().withLowerCase().parseHex(updateInfo.asset().digest.substring(7)); // remove "sha256:" prefix
110+
var digest = updateInfo.asset().digest().startsWith("sha256:")
111+
? HexFormat.of().withLowerCase().parseHex(updateInfo.asset().digest.substring(7)) // remove "sha256:" prefix
112+
: null;
110113
var size = updateInfo.asset().size;
111114
super(uri, destination, digest, size);
112115
this.workDir = workDir;

src/main/java/org/cryptomator/integrations/update/DownloadUpdateStep.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.cryptomator.integrations.update;
22

33
import org.cryptomator.integrations.Localization;
4+
import org.jetbrains.annotations.Nullable;
45

56
import java.io.FilterInputStream;
67
import java.io.IOException;
@@ -16,6 +17,7 @@
1617
import java.nio.file.StandardOpenOption;
1718
import java.security.MessageDigest;
1819
import java.security.NoSuchAlgorithmException;
20+
import java.time.Duration;
1921
import java.util.concurrent.CountDownLatch;
2022
import java.util.concurrent.TimeUnit;
2123
import java.util.concurrent.atomic.AtomicLong;
@@ -35,11 +37,11 @@ public abstract class DownloadUpdateStep implements UpdateStep {
3537
/**
3638
* Creates a new DownloadUpdateProcess instance.
3739
* @param source The URI from which the update will be downloaded.
38-
* @param destination The path to theworking directory where the downloaded file will be saved.
40+
* @param destination The path where to save the downloaded file.
3941
* @param checksum (optional) The expected SHA-256 checksum of the downloaded file, can be null if not required.
4042
* @param estDownloadSize The estimated size of the download in bytes.
4143
*/
42-
protected DownloadUpdateStep(URI source, Path destination, byte[] checksum, long estDownloadSize) {
44+
protected DownloadUpdateStep(URI source, Path destination, @Nullable byte[] checksum, long estDownloadSize) {
4345
this.source = source;
4446
this.destination = destination;
4547
this.checksum = checksum;
@@ -88,7 +90,7 @@ public void cancel() {
8890

8991
protected void download() {
9092
var request = HttpRequest.newBuilder().uri(source).GET().build();
91-
try (HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build()) {
93+
try (HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).connectTimeout(Duration.ofSeconds(10)).build()) {
9294
downloadInternal(client, request);
9395
} catch (IOException e) {
9496
downloadException = e;

src/main/java/org/cryptomator/integrations/update/UpdateStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public String description() {
6767
void cancel();
6868

6969
/**
70-
* Blocks the current thread until this update step completed or an error occured.
70+
* Blocks the current thread until this update step completed or an error occurred.
7171
* <p>
7272
* If the step is already complete, this method returns immediately.
7373
*
@@ -76,7 +76,7 @@ public String description() {
7676
void await() throws InterruptedException;
7777

7878
/**
79-
* Blocks the current thread until this update step completed or an error occured, or until the specified timeout expires.
79+
* Blocks the current thread until this update step completed or an error occurred, or until the specified timeout expires.
8080
* <p>
8181
* If the step is already complete, this method returns immediately.
8282
*

0 commit comments

Comments
 (0)