Fix cover images display

This commit is contained in:
Stefan Hardegger
2025-08-21 12:38:48 +02:00
parent 87a4999ffe
commit 35a5825e76
3 changed files with 43 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
package com.storycove.controller;
import com.storycove.service.ImageService;
import com.storycove.service.LibraryService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
@@ -10,6 +11,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -21,9 +23,16 @@ import java.util.Map;
public class FileController {
private final ImageService imageService;
private final LibraryService libraryService;
public FileController(ImageService imageService) {
public FileController(ImageService imageService, LibraryService libraryService) {
this.imageService = imageService;
this.libraryService = libraryService;
}
private String getCurrentLibraryId() {
String libraryId = libraryService.getCurrentLibraryId();
return libraryId != null ? libraryId : "default";
}
@PostMapping("/upload/cover")
@@ -34,7 +43,8 @@ public class FileController {
Map<String, String> response = new HashMap<>();
response.put("message", "Cover uploaded successfully");
response.put("path", imagePath);
response.put("url", "/api/files/images/" + imagePath);
String currentLibraryId = getCurrentLibraryId();
response.put("url", "/api/files/images/" + currentLibraryId + "/" + imagePath);
return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
@@ -53,7 +63,8 @@ public class FileController {
Map<String, String> response = new HashMap<>();
response.put("message", "Avatar uploaded successfully");
response.put("path", imagePath);
response.put("url", "/api/files/images/" + imagePath);
String currentLibraryId = getCurrentLibraryId();
response.put("url", "/api/files/images/" + currentLibraryId + "/" + imagePath);
return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
@@ -64,17 +75,18 @@ public class FileController {
}
}
@GetMapping("/images/**")
public ResponseEntity<Resource> serveImage(@RequestParam String path) {
@GetMapping("/images/{libraryId}/**")
public ResponseEntity<Resource> serveImage(@PathVariable String libraryId, HttpServletRequest request) {
try {
// Extract path from the URL
String imagePath = path.replace("/api/files/images/", "");
// Extract the full request path after /api/files/images/{libraryId}/
String requestURI = request.getRequestURI();
String imagePath = requestURI.replaceFirst(".*/api/files/images/" + libraryId + "/", "");
if (!imageService.imageExists(imagePath)) {
if (!imageService.imageExistsInLibrary(imagePath, libraryId)) {
return ResponseEntity.notFound().build();
}
Path fullPath = imageService.getImagePath(imagePath);
Path fullPath = imageService.getImagePathInLibrary(imagePath, libraryId);
Resource resource = new FileSystemResource(fullPath);
if (!resource.exists()) {

View File

@@ -116,6 +116,19 @@ public class ImageService {
return Files.exists(getImagePath(imagePath));
}
public boolean imageExistsInLibrary(String imagePath, String libraryId) {
if (imagePath == null || imagePath.trim().isEmpty() || libraryId == null) {
return false;
}
return Files.exists(getImagePathInLibrary(imagePath, libraryId));
}
public Path getImagePathInLibrary(String imagePath, String libraryId) {
String libraryPath = libraryService.getImagePathForLibrary(libraryId);
return Paths.get(baseUploadDir + libraryPath, imagePath);
}
private void validateFile(MultipartFile file) throws IOException {
if (file == null || file.isEmpty()) {
throw new IllegalArgumentException("File is empty");

View File

@@ -272,6 +272,15 @@ public class LibraryService implements ApplicationContextAware {
return current != null ? current.getImagePath() : "/images/default";
}
public String getImagePathForLibrary(String libraryId) {
if (libraryId == null) {
return "/images/default";
}
Library library = libraries.get(libraryId);
return library != null ? library.getImagePath() : "/images/default";
}
public boolean changeLibraryPassword(String libraryId, String currentPassword, String newPassword) {
Library library = libraries.get(libraryId);
if (library == null) {