Allow uploading multiple SVG icons and render them directly as inline SVGs instead of using <img> tags (#1796)
This commit is contained in:
-10
@@ -119,14 +119,4 @@ public class BookMediaController {
|
||||
return ResponseEntity.internalServerError().build();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "Get SVG icon", description = "Retrieve an SVG icon by its name.")
|
||||
@ApiResponse(responseCode = "200", description = "SVG icon returned successfully")
|
||||
@GetMapping("/icon/{name}")
|
||||
public ResponseEntity<String> getSvgIcon(@Parameter(description = "Name of the icon") @PathVariable String name) {
|
||||
String svgData = iconService.getSvgIcon(name);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.valueOf("image/svg+xml"))
|
||||
.body(svgData);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.request.SvgIconBatchRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.SvgIconCreateRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.SvgIconBatchResponse;
|
||||
import com.adityachandel.booklore.service.IconService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -28,6 +30,24 @@ public class IconController {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@Operation(summary = "Save multiple SVG icons", description = "Saves multiple SVG icons to the system in batch.")
|
||||
@ApiResponse(responseCode = "200", description = "Batch save completed with detailed results")
|
||||
@PostMapping("/batch")
|
||||
public ResponseEntity<SvgIconBatchResponse> saveBatchSvgIcons(@Valid @RequestBody SvgIconBatchRequest request) {
|
||||
SvgIconBatchResponse response = iconService.saveBatchSvgIcons(request.getIcons());
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get SVG icon content", description = "Retrieve the SVG content of an icon by its name.")
|
||||
@ApiResponse(responseCode = "200", description = "SVG icon content retrieved successfully")
|
||||
@GetMapping("/{svgName}/content")
|
||||
public ResponseEntity<String> getSvgIconContent(@Parameter(description = "SVG icon name") @PathVariable String svgName) {
|
||||
String svgContent = iconService.getSvgIcon(svgName);
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Type", "image/svg+xml")
|
||||
.body(svgContent);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get paginated icon names", description = "Retrieve a paginated list of icon names (default 50 per page).")
|
||||
@ApiResponse(responseCode = "200", description = "Icon names retrieved successfully")
|
||||
@GetMapping
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SvgIconBatchRequest {
|
||||
|
||||
@NotEmpty(message = "Icons list cannot be empty")
|
||||
@Valid
|
||||
private List<SvgIconCreateRequest> icons;
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.adityachandel.booklore.model.dto.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SvgIconBatchResponse {
|
||||
private int totalRequested;
|
||||
private int successCount;
|
||||
private int failureCount;
|
||||
private List<IconSaveResult> results;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class IconSaveResult {
|
||||
private String iconName;
|
||||
private boolean success;
|
||||
private String errorMessage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.service;
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.dto.request.SvgIconCreateRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.SvgIconBatchResponse;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -16,6 +17,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -97,6 +99,44 @@ public class IconService {
|
||||
}
|
||||
}
|
||||
|
||||
public SvgIconBatchResponse saveBatchSvgIcons(List<SvgIconCreateRequest> requests) {
|
||||
if (requests == null || requests.isEmpty()) {
|
||||
throw ApiError.INVALID_INPUT.createException("Icons list cannot be empty");
|
||||
}
|
||||
|
||||
List<SvgIconBatchResponse.IconSaveResult> results = new ArrayList<>();
|
||||
int successCount = 0;
|
||||
int failureCount = 0;
|
||||
|
||||
for (SvgIconCreateRequest request : requests) {
|
||||
try {
|
||||
saveSvgIcon(request);
|
||||
results.add(SvgIconBatchResponse.IconSaveResult.builder()
|
||||
.iconName(request.getSvgName())
|
||||
.success(true)
|
||||
.build());
|
||||
successCount++;
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to save icon '{}': {}", request.getSvgName(), e.getMessage());
|
||||
results.add(SvgIconBatchResponse.IconSaveResult.builder()
|
||||
.iconName(request.getSvgName())
|
||||
.success(false)
|
||||
.errorMessage(e.getMessage())
|
||||
.build());
|
||||
failureCount++;
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Batch save completed: {} successful, {} failed", successCount, failureCount);
|
||||
|
||||
return SvgIconBatchResponse.builder()
|
||||
.totalRequested(requests.size())
|
||||
.successCount(successCount)
|
||||
.failureCount(failureCount)
|
||||
.results(results)
|
||||
.build();
|
||||
}
|
||||
|
||||
public String getSvgIcon(String name) {
|
||||
String filename = normalizeFilename(name);
|
||||
String cachedSvg = svgCache.get(filename);
|
||||
|
||||
Reference in New Issue
Block a user