- Remove library name from payload (#2054)
- Add basic installation ping to track number of active instances Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
@@ -16,6 +16,7 @@ public class AppProperties {
|
||||
private RemoteAuth remoteAuth;
|
||||
private Swagger swagger = new Swagger();
|
||||
private Boolean forceDisableOidc = false;
|
||||
private Telemetry telemetry = new Telemetry();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -35,4 +36,10 @@ public class AppProperties {
|
||||
public static class Swagger {
|
||||
private boolean enabled = true;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class Telemetry {
|
||||
private String baseUrl = "https://telemetry.booklore.org";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.crons;
|
||||
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.model.dto.BookloreTelemetry;
|
||||
import com.adityachandel.booklore.model.dto.InstallationPing;
|
||||
import com.adityachandel.booklore.service.TelemetryService;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -16,6 +18,7 @@ import java.util.concurrent.TimeUnit;
|
||||
@Slf4j
|
||||
public class CronService {
|
||||
|
||||
private final AppProperties appProperties;
|
||||
private final TelemetryService telemetryService;
|
||||
private final RestClient restClient;
|
||||
private final AppSettingService appSettingService;
|
||||
@@ -24,15 +27,31 @@ public class CronService {
|
||||
public void sendTelemetryData() {
|
||||
if (appSettingService.getAppSettings().isTelemetryEnabled()) {
|
||||
try {
|
||||
String url = appProperties.getTelemetry().getBaseUrl() + "/api/v1/ingest";
|
||||
BookloreTelemetry telemetry = telemetryService.collectTelemetry();
|
||||
restClient.post()
|
||||
.uri("https://telemetry.booklore.org/api/v1/ingest")
|
||||
.body(telemetry)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
} catch (Exception ignored) {
|
||||
|
||||
postData(url, telemetry);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to send telemetry data: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 24, timeUnit = TimeUnit.HOURS, initialDelay = 10)
|
||||
public void sendPing() {
|
||||
try {
|
||||
String url = appProperties.getTelemetry().getBaseUrl() + "/api/v1/heartbeat";
|
||||
InstallationPing ping = telemetryService.getInstallationPing();
|
||||
postData(url, ping);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to send installation ping: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void postData(String url, Object body) {
|
||||
restClient.post()
|
||||
.uri(url)
|
||||
.body(body)
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-8
@@ -11,9 +11,9 @@ import java.util.Map;
|
||||
@Setter
|
||||
@Getter
|
||||
public class BookloreTelemetry {
|
||||
@Builder.Default
|
||||
private int telemetryVersion = 1;
|
||||
private int telemetryVersion;
|
||||
private String installationId;
|
||||
private String installationDate;
|
||||
private String appVersion;
|
||||
|
||||
private int totalLibraries;
|
||||
@@ -49,8 +49,8 @@ public class BookloreTelemetry {
|
||||
@Builder
|
||||
@Getter
|
||||
public static class MetadataStatistics {
|
||||
private int[] enabledMetadataProviders;
|
||||
private int[] enabledReviewMetadataProviders;
|
||||
private String[] enabledMetadataProviders;
|
||||
private String[] enabledReviewMetadataProviders;
|
||||
private boolean saveMetadataToFile;
|
||||
private boolean moveFileViaPattern;
|
||||
private boolean autoBookSearchEnabled;
|
||||
@@ -85,17 +85,16 @@ public class BookloreTelemetry {
|
||||
@Getter
|
||||
public static class BookStatistics {
|
||||
private long totalBooks;
|
||||
private Map<Integer, Long> bookCountByType;
|
||||
private Map<String, Long> bookCountByType;
|
||||
}
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public static class LibraryStatistics {
|
||||
private String libraryName;
|
||||
private long bookCount;
|
||||
private int totalLibraryPaths;
|
||||
private boolean watchEnabled;
|
||||
private int iconType;
|
||||
private int scanMode;
|
||||
private String iconType;
|
||||
private String scanMode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Installation {
|
||||
private String id;
|
||||
private Instant date;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class InstallationPing {
|
||||
private int pingVersion;
|
||||
private String appVersion;
|
||||
private String installationId;
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "UTC")
|
||||
private Instant installationDate;
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Installation;
|
||||
import com.adityachandel.booklore.model.entity.AppSettingEntity;
|
||||
import com.adityachandel.booklore.repository.AppSettingsRepository;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class InstallationService {
|
||||
|
||||
private static final String INSTALLATION_ID_KEY = "installation_id";
|
||||
|
||||
private final AppSettingsRepository appSettingsRepository;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public InstallationService(AppSettingsRepository appSettingsRepository, ObjectMapper objectMapper) {
|
||||
this.appSettingsRepository = appSettingsRepository;
|
||||
this.objectMapper = objectMapper.copy();
|
||||
this.objectMapper.registerModule(new JavaTimeModule());
|
||||
this.objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
}
|
||||
|
||||
public Installation getOrCreateInstallation() {
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting == null) {
|
||||
return createNewInstallation();
|
||||
}
|
||||
|
||||
try {
|
||||
return objectMapper.readValue(setting.getVal(), Installation.class);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to parse installation ID, creating new one", e);
|
||||
return createNewInstallation();
|
||||
}
|
||||
}
|
||||
|
||||
private Installation createNewInstallation() {
|
||||
Instant now = Instant.now();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
|
||||
String combined = now.toString() + "_" + uuid;
|
||||
String installationId = hashToSha256(combined).substring(0, 24);
|
||||
|
||||
Installation installation = new Installation(installationId, now);
|
||||
saveInstallation(installation);
|
||||
|
||||
log.info("Generated new installation ID");
|
||||
return installation;
|
||||
}
|
||||
|
||||
private void saveInstallation(Installation installation) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(installation);
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting == null) {
|
||||
setting = new AppSettingEntity();
|
||||
setting.setName(INSTALLATION_ID_KEY);
|
||||
}
|
||||
|
||||
setting.setVal(json);
|
||||
appSettingsRepository.save(setting);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to save installation ID", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String hashToSha256(String input) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("SHA-256 algorithm not found", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
-77
@@ -1,63 +1,30 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookloreTelemetry;
|
||||
import com.adityachandel.booklore.model.dto.Installation;
|
||||
import com.adityachandel.booklore.model.dto.InstallationPing;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataProviderSettings;
|
||||
import com.adityachandel.booklore.model.dto.settings.MetadataPublicReviewsSettings;
|
||||
import com.adityachandel.booklore.model.entity.AppSettingEntity;
|
||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.ProvisioningMethod;
|
||||
import com.adityachandel.booklore.model.enums.IconType;
|
||||
import com.adityachandel.booklore.model.enums.LibraryScanMode;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
import com.adityachandel.booklore.model.enums.ProvisioningMethod;
|
||||
import com.adityachandel.booklore.repository.*;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class TelemetryService {
|
||||
|
||||
private static class EnumMappings {
|
||||
static final Map<BookFileType, Integer> BOOK_FILE_TYPE = Map.of(
|
||||
BookFileType.PDF, 1,
|
||||
BookFileType.EPUB, 2,
|
||||
BookFileType.CBX, 3,
|
||||
BookFileType.FB2, 4
|
||||
);
|
||||
static final Map<IconType, Integer> ICON_TYPE = Map.of(
|
||||
IconType.PRIME_NG, 1,
|
||||
IconType.CUSTOM_SVG, 2
|
||||
);
|
||||
static final Map<LibraryScanMode, Integer> LIBRARY_SCAN_MODE = Map.of(
|
||||
LibraryScanMode.FILE_AS_BOOK, 1,
|
||||
LibraryScanMode.FOLDER_AS_BOOK, 2
|
||||
);
|
||||
static final Map<MetadataProvider, Integer> METADATA_PROVIDER = Map.of(
|
||||
MetadataProvider.Amazon, 1,
|
||||
MetadataProvider.Google, 2,
|
||||
MetadataProvider.GoodReads, 3,
|
||||
MetadataProvider.Hardcover, 4,
|
||||
MetadataProvider.Comicvine, 5,
|
||||
MetadataProvider.Douban, 6
|
||||
);
|
||||
static final Map<MetadataProvider, Integer> REVIEW_PROVIDER = Map.of(
|
||||
MetadataProvider.Amazon, 1,
|
||||
MetadataProvider.Google, 2,
|
||||
MetadataProvider.GoodReads, 3,
|
||||
MetadataProvider.Hardcover, 4,
|
||||
MetadataProvider.Comicvine, 5,
|
||||
MetadataProvider.Douban, 6
|
||||
);
|
||||
}
|
||||
|
||||
private static final String INSTALLATION_ID_KEY = "installation_id";
|
||||
private final AppSettingsRepository appSettingsRepository;
|
||||
private final VersionService versionService;
|
||||
private final LibraryRepository libraryRepository;
|
||||
private final BookRepository bookRepository;
|
||||
@@ -77,6 +44,18 @@ public class TelemetryService {
|
||||
private final KoboUserSettingsRepository koboUserSettingsRepository;
|
||||
private final KoreaderUserRepository koreaderUserRepository;
|
||||
private final OpdsUserV2Repository opdsUserV2Repository;
|
||||
private final InstallationService installationService;
|
||||
|
||||
public InstallationPing getInstallationPing() {
|
||||
Installation installation = installationService.getOrCreateInstallation();
|
||||
|
||||
return InstallationPing.builder()
|
||||
.pingVersion(1)
|
||||
.appVersion(versionService.appVersion)
|
||||
.installationId(installation.getId())
|
||||
.installationDate(installation.getDate())
|
||||
.build();
|
||||
}
|
||||
|
||||
public BookloreTelemetry collectTelemetry() {
|
||||
long totalUsers = userRepository.count();
|
||||
@@ -94,11 +73,15 @@ public class TelemetryService {
|
||||
.map(this::mapLibraryStatistics)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int[] enabledMetadataProviders = getEnabledMetadataProvidersAsInt(settings.getMetadataProviderSettings());
|
||||
int[] enabledReviewMetadataProviders = getEnabledReviewMetadataProvidersAsInt(settings.getMetadataPublicReviewsSettings());
|
||||
String[] enabledMetadataProviders = getEnabledMetadataProviders(settings.getMetadataProviderSettings());
|
||||
String[] enabledReviewMetadataProviders = getEnabledReviewMetadataProviders(settings.getMetadataPublicReviewsSettings());
|
||||
|
||||
Installation installation = installationService.getOrCreateInstallation();
|
||||
|
||||
return BookloreTelemetry.builder()
|
||||
.installationId(getInstallationId())
|
||||
.telemetryVersion(2)
|
||||
.installationId(installation.getId())
|
||||
.installationDate(installation.getDate() != null ? installation.getDate().toString() : null)
|
||||
.appVersion(versionService.appVersion)
|
||||
.totalLibraries((int) libraryRepository.count())
|
||||
.totalBooks(bookRepository.count())
|
||||
@@ -146,64 +129,50 @@ public class TelemetryService {
|
||||
.build();
|
||||
}
|
||||
|
||||
private Map<Integer, Long> getBookFileTypeCounts() {
|
||||
Map<Integer, Long> countByType = new HashMap<>();
|
||||
private Map<String, Long> getBookFileTypeCounts() {
|
||||
Map<String, Long> countByType = new HashMap<>();
|
||||
for (BookFileType type : BookFileType.values()) {
|
||||
Integer mapped = EnumMappings.BOOK_FILE_TYPE.get(type);
|
||||
if (mapped != null) {
|
||||
countByType.put(mapped, bookRepository.countByBookType(type));
|
||||
}
|
||||
countByType.put(type.name(), bookRepository.countByBookType(type));
|
||||
}
|
||||
return countByType;
|
||||
}
|
||||
|
||||
private BookloreTelemetry.LibraryStatistics mapLibraryStatistics(LibraryEntity lib) {
|
||||
return BookloreTelemetry.LibraryStatistics.builder()
|
||||
.libraryName(lib.getName())
|
||||
.totalLibraryPaths(lib.getLibraryPaths() != null ? lib.getLibraryPaths().size() : 0)
|
||||
.bookCount(bookRepository.countByLibraryId(lib.getId()))
|
||||
.watchEnabled(lib.isWatch())
|
||||
.iconType(lib.getIconType() != null ? EnumMappings.ICON_TYPE.getOrDefault(lib.getIconType(), -1) : -1)
|
||||
.scanMode(lib.getScanMode() != null ? EnumMappings.LIBRARY_SCAN_MODE.getOrDefault(lib.getScanMode(), -1) : -1)
|
||||
.iconType(lib.getIconType() != null ? lib.getIconType().name() : null)
|
||||
.scanMode(lib.getScanMode() != null ? lib.getScanMode().name() : null)
|
||||
.build();
|
||||
}
|
||||
|
||||
private int[] getEnabledMetadataProvidersAsInt(MetadataProviderSettings providers) {
|
||||
List<Integer> enabled = new ArrayList<>();
|
||||
private String[] getEnabledMetadataProviders(MetadataProviderSettings providers) {
|
||||
List<String> enabled = new ArrayList<>();
|
||||
if (providers.getAmazon() != null && providers.getAmazon().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.Amazon));
|
||||
enabled.add(MetadataProvider.Amazon.name());
|
||||
if (providers.getGoogle() != null && providers.getGoogle().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.Google));
|
||||
enabled.add(MetadataProvider.Google.name());
|
||||
if (providers.getGoodReads() != null && providers.getGoodReads().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.GoodReads));
|
||||
enabled.add(MetadataProvider.GoodReads.name());
|
||||
if (providers.getHardcover() != null && providers.getHardcover().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.Hardcover));
|
||||
enabled.add(MetadataProvider.Hardcover.name());
|
||||
if (providers.getComicvine() != null && providers.getComicvine().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.Comicvine));
|
||||
enabled.add(MetadataProvider.Comicvine.name());
|
||||
if (providers.getDouban() != null && providers.getDouban().isEnabled())
|
||||
enabled.add(EnumMappings.METADATA_PROVIDER.get(MetadataProvider.Douban));
|
||||
return enabled.stream().mapToInt(i -> i).toArray();
|
||||
enabled.add(MetadataProvider.Douban.name());
|
||||
if (providers.getLubimyczytac() != null && providers.getLubimyczytac().isEnabled())
|
||||
enabled.add(MetadataProvider.Lubimyczytac.name());
|
||||
return enabled.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private int[] getEnabledReviewMetadataProvidersAsInt(MetadataPublicReviewsSettings reviewSettings) {
|
||||
List<Integer> enabled = new ArrayList<>();
|
||||
private String[] getEnabledReviewMetadataProviders(MetadataPublicReviewsSettings reviewSettings) {
|
||||
List<String> enabled = new ArrayList<>();
|
||||
if (reviewSettings.getProviders() != null) {
|
||||
reviewSettings.getProviders().stream()
|
||||
.filter(MetadataPublicReviewsSettings.ReviewProviderConfig::isEnabled)
|
||||
.forEach(cfg -> {
|
||||
try {
|
||||
MetadataProvider provider = MetadataProvider.valueOf(cfg.getProvider().name());
|
||||
Integer mapped = EnumMappings.REVIEW_PROVIDER.get(provider);
|
||||
if (mapped != null) enabled.add(mapped);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
});
|
||||
.forEach(cfg -> enabled.add(cfg.getProvider().name()));
|
||||
}
|
||||
return enabled.stream().mapToInt(i -> i).toArray();
|
||||
}
|
||||
|
||||
private String getInstallationId() {
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
return setting != null ? setting.getVal() : "unknown";
|
||||
return enabled.toArray(new String[0]);
|
||||
}
|
||||
}
|
||||
|
||||
+33
-32
@@ -11,9 +11,11 @@ import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.book.BookQueryService;
|
||||
import com.adityachandel.booklore.service.file.FileFingerprint;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import com.adityachandel.booklore.service.InstallationService;
|
||||
import com.adityachandel.booklore.util.BookUtils;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -33,6 +35,7 @@ import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -52,47 +55,18 @@ public class AppMigrationService {
|
||||
private MetadataMatchService metadataMatchService;
|
||||
private AppProperties appProperties;
|
||||
private FileService fileService;
|
||||
private ObjectMapper objectMapper;
|
||||
private InstallationService installationService;
|
||||
|
||||
@Transactional
|
||||
public void generateInstallationId() {
|
||||
if (migrationRepository.existsById("generateInstallationId")) return;
|
||||
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting == null) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String combined = now + "_" + uuid;
|
||||
|
||||
String installationId = hashToSha256(combined).substring(0, 24);
|
||||
|
||||
setting = new AppSettingEntity();
|
||||
setting.setName(INSTALLATION_ID_KEY);
|
||||
setting.setVal(installationId);
|
||||
appSettingsRepository.save(setting);
|
||||
|
||||
log.info("Generated new installation ID");
|
||||
}
|
||||
installationService.getOrCreateInstallation();
|
||||
|
||||
migrationRepository.save(new AppMigrationEntity("generateInstallationId", LocalDateTime.now(), "Generate unique installation ID using timestamp and UUID"));
|
||||
}
|
||||
|
||||
private String hashToSha256(String input) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("SHA-256 algorithm not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void populateSearchTextOnce() {
|
||||
if (migrationRepository.existsById("populateSearchText")) return;
|
||||
@@ -338,4 +312,31 @@ public class AppMigrationService {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void migrateInstallationIdToJson() {
|
||||
if (migrationRepository.existsById("migrateInstallationIdToJson")) return;
|
||||
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting != null) {
|
||||
String value = setting.getVal();
|
||||
try {
|
||||
objectMapper.readTree(value);
|
||||
log.info("Installation ID is already in JSON format, skipping migration");
|
||||
} catch (Exception e) {
|
||||
Instant now = Instant.now();
|
||||
String json = String.format("{\"id\":\"%s\",\"date\":\"%s\"}", value, now);
|
||||
setting.setVal(json);
|
||||
appSettingsRepository.save(setting);
|
||||
log.info("Migrated installation ID to JSON format with current date");
|
||||
}
|
||||
}
|
||||
|
||||
migrationRepository.save(new AppMigrationEntity(
|
||||
"migrateInstallationIdToJson",
|
||||
LocalDateTime.now(),
|
||||
"Migrate existing installation_id from plain string to JSON format with date"
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
@@ -14,6 +14,7 @@ public class AppMigrationStartup {
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runMigrationsOnce() {
|
||||
appMigrationService.generateInstallationId();
|
||||
appMigrationService.migrateInstallationIdToJson();
|
||||
appMigrationService.populateMissingFileSizesOnce();
|
||||
appMigrationService.populateMetadataScoresOnce();
|
||||
appMigrationService.populateFileHashesOnce();
|
||||
|
||||
@@ -14,6 +14,8 @@ app:
|
||||
admin-group: ${REMOTE_AUTH_ADMIN_GROUP}
|
||||
groups-delimiter: ${REMOTE_AUTH_GROUPS_DELIMITER:\\s+}
|
||||
force-disable-oidc: ${FORCE_DISABLE_OIDC:false}
|
||||
telemetry:
|
||||
base-url: ${TELEMETRY_BASE_URL:https://telemetry.booklore.org}
|
||||
|
||||
server:
|
||||
forward-headers-strategy: native
|
||||
|
||||
Reference in New Issue
Block a user