|
| 1 | +package org.cryptomator.integrations.update; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import org.jetbrains.annotations.Blocking; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.net.URI; |
| 15 | +import java.net.http.HttpClient; |
| 16 | +import java.net.http.HttpRequest; |
| 17 | +import java.net.http.HttpResponse; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.HexFormat; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +public abstract class DownloadUpdateMechanism implements UpdateMechanism<DownloadUpdateInfo> { |
| 24 | + |
| 25 | + private static final Logger LOG = LoggerFactory.getLogger(DownloadUpdateMechanism.class); |
| 26 | + private static final String LATEST_VERSION_API_URL = "https://api.cryptomator.org/connect/apps/desktop/latest-version?format=1"; |
| 27 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpClient) { |
| 31 | + try { |
| 32 | + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(LATEST_VERSION_API_URL)).build(); |
| 33 | + HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); |
| 34 | + if (response.statusCode() != 200) { |
| 35 | + LOG.warn("Failed to fetch release: HTTP {}", response.statusCode()); |
| 36 | + return null; |
| 37 | + } |
| 38 | + var release = MAPPER.readValue(response.body(), LatestVersionResponse.class); |
| 39 | + return checkForUpdate(currentVersion, release); |
| 40 | + } catch (InterruptedException e) { |
| 41 | + Thread.currentThread().interrupt(); |
| 42 | + LOG.debug("Update check interrupted."); |
| 43 | + return null; |
| 44 | + } catch (IOException e) { |
| 45 | + LOG.warn("Update check failed", e); |
| 46 | + return null; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the first step to prepare the update. This downloads the {@link DownloadUpdateInfo#asset() asset} to a temporary location and verifies its checksum. |
| 52 | + * @param updateInfo The {@link DownloadUpdateInfo} retrieved from {@link #checkForUpdate(String, HttpClient)}. |
| 53 | + * @return a new {@link UpdateStep} that can be used to monitor the download progress. |
| 54 | + * @throws UpdateFailedException When failing to prepare a temporary download location. |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public UpdateStep firstStep(DownloadUpdateInfo updateInfo) throws UpdateFailedException { |
| 58 | + try { |
| 59 | + Path workDir = Files.createTempDirectory("cryptomator-update"); |
| 60 | + return new FirstStep(workDir, updateInfo); |
| 61 | + } catch (IOException e) { |
| 62 | + throw new UpdateFailedException("Failed to create temporary directory for update", e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Second step that is executed after the download has completed in the {@link #firstStep(DownloadUpdateInfo) first step}. |
| 68 | + * @param workDir A temporary working directory to which the asset has been downloaded. |
| 69 | + * @param assetPath The path of the downloaded asset. |
| 70 | + * @param updateInfo The {@link DownloadUpdateInfo} representing the update. |
| 71 | + * @return The next step of the update process. |
| 72 | + * @throws IllegalStateException if preconditions aren't met. |
| 73 | + * @throws IOException indicating an error preventing the next step from starting. |
| 74 | + * @implSpec The returned {@link UpdateStep} must either be stateless or a new instance must be returned on each call. |
| 75 | + */ |
| 76 | + public abstract UpdateStep secondStep(Path workDir, Path assetPath, DownloadUpdateInfo updateInfo) throws IllegalStateException, IOException; |
| 77 | + |
| 78 | + @Nullable |
| 79 | + @Blocking |
| 80 | + protected abstract DownloadUpdateInfo checkForUpdate(String currentVersion, LatestVersionResponse response); |
| 81 | + |
| 82 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 83 | + public record LatestVersionResponse( |
| 84 | + @JsonProperty("latestVersion") LatestVersion latestVersion, |
| 85 | + @JsonProperty("assets") List<Asset> assets |
| 86 | + ) {} |
| 87 | + |
| 88 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 89 | + public record LatestVersion( |
| 90 | + @JsonProperty("mac") String macVersion, |
| 91 | + @JsonProperty("win") String winVersion, |
| 92 | + @JsonProperty("linux") String linuxVersion |
| 93 | + ) {} |
| 94 | + |
| 95 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 96 | + public record Asset( |
| 97 | + @JsonProperty("name") String name, |
| 98 | + @JsonProperty("digest") String digest, |
| 99 | + @JsonProperty("size") long size, |
| 100 | + @JsonProperty("downloadUrl") String downloadUrl |
| 101 | + ) {} |
| 102 | + |
| 103 | + private class FirstStep extends DownloadUpdateStep { |
| 104 | + private final Path workDir; |
| 105 | + private final DownloadUpdateInfo updateInfo; |
| 106 | + |
| 107 | + public FirstStep(Path workDir, DownloadUpdateInfo updateInfo) { |
| 108 | + var uri = URI.create(updateInfo.asset().downloadUrl); |
| 109 | + var destination = workDir.resolve(updateInfo.asset().name); |
| 110 | + var digest = updateInfo.asset().digest().startsWith("sha256:") |
| 111 | + ? HexFormat.of().withLowerCase().parseHex(updateInfo.asset().digest.substring(7)) // remove "sha256:" prefix |
| 112 | + : null; |
| 113 | + var size = updateInfo.asset().size; |
| 114 | + super(uri, destination, digest, size); |
| 115 | + this.workDir = workDir; |
| 116 | + this.updateInfo = updateInfo; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public @Nullable UpdateStep nextStep() throws IllegalStateException, IOException { |
| 121 | + if (!isDone()) { |
| 122 | + throw new IllegalStateException("Download not yet completed."); |
| 123 | + } else if (downloadException != null) { |
| 124 | + throw new UpdateFailedException("Download failed.", downloadException); |
| 125 | + } |
| 126 | + return secondStep(workDir, destination, updateInfo); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments