listing of all versions

Signed-off-by: Peter Siegmund <peter@rdkr.com>
This commit is contained in:
Peter Siegmund
2024-05-30 16:22:35 +02:00
parent c4ccf27130
commit 2e39c16d96
5 changed files with 78 additions and 7 deletions

View File

@@ -0,0 +1,15 @@
package com.rdkr.tide_display.backend.firmware.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class RootController {
@GetMapping()
public String index() {
return "Tide Display Backend";
}
}

View File

@@ -20,7 +20,7 @@ public class VersionController {
@GetMapping()
public ResponseEntity<List<Firmware>> getVersions() {
return ResponseEntity.of(Optional.of(firmwareService.getFirmwareVersions()));
return ResponseEntity.of(Optional.ofNullable(firmwareService.getFirmwareVersions()));
}
@GetMapping("latest")

View File

@@ -27,7 +27,7 @@ public class FirmwareServiceImpl implements FirmwareService {
public Optional<Firmware> getFirmwareVersion(String version) {
return
storageService.getFirmwareVersions().stream()
.filter(firmware -> firmware.version().equals(version))
//.filter(firmware -> firmware.version().equals(version))
.findFirst();
}
}

View File

@@ -1,5 +1,30 @@
package com.rdkr.tide_display.backend.gcp;
public record Firmware(String version, String url, String checksum) {
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.time.OffsetDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Firmware {
@JsonIgnore
private int major;
@JsonIgnore
private int minor;
@JsonIgnore
private int patch;
private String url;
private String eTag;
private OffsetDateTime lastModified;
public String getVersion() {
return String.format("%d.%d.%d", major, minor, patch);
}
}

View File

@@ -1,8 +1,13 @@
package com.rdkr.tide_display.backend.gcp.service;
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.StorageOptions;
import com.rdkr.tide_display.backend.gcp.Firmware;
import com.rdkr.tide_display.backend.gcp.StorageService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.val;
import org.springframework.stereotype.Service;
@Service
@@ -10,9 +15,35 @@ public class StorageServiceImpl implements StorageService {
@Override
public List<Firmware> getFirmwareVersions() {
return List.of(
new Firmware("1.0.1", "/firmware/1.0.1/firmware.bin", "0987654321"),
new Firmware("1.0.0", "/firmware/1.0.0/firmware.bin", "1234567890")
);
val result = new ArrayList<Firmware>();
val storage = StorageOptions.getDefaultInstance().getService();
String bucketName = "ec804bd8-38d2-4fcc-8f04-5addb55b3c90";
val bucket = storage.list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix("firmware/"));
for (val directory : bucket.iterateAll()) {
val files = storage.list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(directory.getName()));
for (val file : files.iterateAll()) {
val name = file.getName();
if (name.endsWith(".bin")) {
val version = directory.getName().replace("firmware/", "").replace("/", "").split("\\.");
if (version.length == 3) {
int major = Integer.parseInt(version[0]);
int minor = Integer.parseInt(version[1]);
int patch = Integer.parseInt(version[2]);
result.add(new Firmware(major, minor, patch, "/files/" + major + "." + minor + "." + patch + "/firmware.bin", file.getEtag(), file.getUpdateTimeOffsetDateTime()));
}
}
}
}
result.sort((a, b) -> {
if (a.getMajor() != b.getMajor()) {
return a.getMajor() - b.getMajor();
}
if (a.getMinor() != b.getMinor()) {
return a.getMinor() - b.getMinor();
}
return a.getPatch() - b.getPatch();
});
return result.reversed();
}
}