Improve the SVG fetching and caching logic (#2011)

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2025-12-27 15:26:11 -07:00
committed by GitHub
co-authored by acx10
parent 81757317e5
commit 9e151010e7
14 changed files with 368 additions and 374 deletions
@@ -15,6 +15,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Tag(name = "Icons", description = "Endpoints for managing SVG icons")
@AllArgsConstructor
@RestController
@@ -69,4 +71,12 @@ public class IconController {
iconService.deleteSvgIcon(svgName);
return ResponseEntity.ok().build();
}
@Operation(summary = "Get all icon contents", description = "Retrieve all SVG icons as a map of icon names to their content.")
@ApiResponse(responseCode = "200", description = "All icon contents retrieved successfully")
@GetMapping("/all/content")
public ResponseEntity<Map<String, String>> getAllIconsContent() {
Map<String, String> iconsMap = iconService.getAllIconsContent();
return ResponseEntity.ok(iconsMap);
}
}
@@ -19,7 +19,9 @@ import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Stream;
@@ -271,4 +273,35 @@ public class IconService {
ConcurrentHashMap<String, String> getSvgCache() {
return svgCache;
}
public Map<String, String> getAllIconsContent() {
Path iconsPath = getIconsSvgPath();
if (!Files.exists(iconsPath)) {
return Collections.emptyMap();
}
try (Stream<Path> paths = Files.list(iconsPath)) {
Map<String, String> iconMap = new HashMap<>();
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(SVG_EXTENSION))
.forEach(path -> {
try {
String filename = path.getFileName().toString();
String iconName = filename.replace(SVG_EXTENSION, "");
String content = svgCache.getOrDefault(filename, Files.readString(path));
iconMap.put(iconName, content);
} catch (IOException e) {
log.warn("Failed to read icon: {}", path.getFileName(), e);
}
});
log.info("Retrieved {} icons for bulk content request", iconMap.size());
return iconMap;
} catch (IOException e) {
log.error("Failed to read icons directory: {}", e.getMessage(), e);
throw ApiError.FILE_READ_ERROR.createException("Failed to read icons: " + e.getMessage());
}
}
}