diff --git a/backend/src/main/java/com/storycove/controller/FileController.java b/backend/src/main/java/com/storycove/controller/FileController.java index c20e1e0..cc5934b 100644 --- a/backend/src/main/java/com/storycove/controller/FileController.java +++ b/backend/src/main/java/com/storycove/controller/FileController.java @@ -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 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 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 serveImage(@RequestParam String path) { + @GetMapping("/images/{libraryId}/**") + public ResponseEntity 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()) { diff --git a/backend/src/main/java/com/storycove/service/ImageService.java b/backend/src/main/java/com/storycove/service/ImageService.java index ac1d6fd..0f31d4e 100644 --- a/backend/src/main/java/com/storycove/service/ImageService.java +++ b/backend/src/main/java/com/storycove/service/ImageService.java @@ -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"); diff --git a/backend/src/main/java/com/storycove/service/LibraryService.java b/backend/src/main/java/com/storycove/service/LibraryService.java index c24a177..b19f6aa 100644 --- a/backend/src/main/java/com/storycove/service/LibraryService.java +++ b/backend/src/main/java/com/storycove/service/LibraryService.java @@ -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) {