From d614372732998239ab24708c3137f97b66a2ca00 Mon Sep 17 00:00:00 2001 From: Jan Bernitt Date: Tue, 10 Dec 2024 14:54:00 +0100 Subject: [PATCH] fix: auto-repair default icons when there is a orphan file resource [DHIS2-18606] --- .../src/main/java/org/hisp/dhis/icon/DefaultIconService.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/icon/DefaultIconService.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/icon/DefaultIconService.java index 46252c6b9ed3..5226c174b114 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/icon/DefaultIconService.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/icon/DefaultIconService.java @@ -40,6 +40,7 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; @@ -114,11 +115,13 @@ public String addDefaultIconImage(@Nonnull String key, @Nonnull DefaultIcon orig try { FileResource fr = FileResource.ofKey(ICON, key, MEDIA_TYPE_SVG); fr.setUid(fileResourceId); + Optional existing = fileResourceService.findByStorageKey(fr.getStorageKey()); + if (existing.isPresent()) fr = existing.get(); fr.setAssigned(true); try (InputStream image = resource.getInputStream()) { fileResourceService.syncSaveFileResource(fr, image); } - return fileResourceId; + return fr.getUid(); } catch (IOException ex) { ignoredAfterFailure.add(origin); throw new ConflictException("Failed to create default icon resource: " + ex.getMessage());