Fix cover images display
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.storycove.controller;
|
package com.storycove.controller;
|
||||||
|
|
||||||
import com.storycove.service.ImageService;
|
import com.storycove.service.ImageService;
|
||||||
|
import com.storycove.service.LibraryService;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@@ -10,6 +11,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@@ -21,9 +23,16 @@ import java.util.Map;
|
|||||||
public class FileController {
|
public class FileController {
|
||||||
|
|
||||||
private final ImageService imageService;
|
private final ImageService imageService;
|
||||||
|
private final LibraryService libraryService;
|
||||||
|
|
||||||
public FileController(ImageService imageService) {
|
public FileController(ImageService imageService, LibraryService libraryService) {
|
||||||
this.imageService = imageService;
|
this.imageService = imageService;
|
||||||
|
this.libraryService = libraryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getCurrentLibraryId() {
|
||||||
|
String libraryId = libraryService.getCurrentLibraryId();
|
||||||
|
return libraryId != null ? libraryId : "default";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/upload/cover")
|
@PostMapping("/upload/cover")
|
||||||
@@ -34,7 +43,8 @@ public class FileController {
|
|||||||
Map<String, String> response = new HashMap<>();
|
Map<String, String> response = new HashMap<>();
|
||||||
response.put("message", "Cover uploaded successfully");
|
response.put("message", "Cover uploaded successfully");
|
||||||
response.put("path", imagePath);
|
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);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -53,7 +63,8 @@ public class FileController {
|
|||||||
Map<String, String> response = new HashMap<>();
|
Map<String, String> response = new HashMap<>();
|
||||||
response.put("message", "Avatar uploaded successfully");
|
response.put("message", "Avatar uploaded successfully");
|
||||||
response.put("path", imagePath);
|
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);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -64,17 +75,18 @@ public class FileController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/images/**")
|
@GetMapping("/images/{libraryId}/**")
|
||||||
public ResponseEntity<Resource> serveImage(@RequestParam String path) {
|
public ResponseEntity<Resource> serveImage(@PathVariable String libraryId, HttpServletRequest request) {
|
||||||
try {
|
try {
|
||||||
// Extract path from the URL
|
// Extract the full request path after /api/files/images/{libraryId}/
|
||||||
String imagePath = path.replace("/api/files/images/", "");
|
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();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
Path fullPath = imageService.getImagePath(imagePath);
|
Path fullPath = imageService.getImagePathInLibrary(imagePath, libraryId);
|
||||||
Resource resource = new FileSystemResource(fullPath);
|
Resource resource = new FileSystemResource(fullPath);
|
||||||
|
|
||||||
if (!resource.exists()) {
|
if (!resource.exists()) {
|
||||||
|
|||||||
@@ -116,6 +116,19 @@ public class ImageService {
|
|||||||
return Files.exists(getImagePath(imagePath));
|
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 {
|
private void validateFile(MultipartFile file) throws IOException {
|
||||||
if (file == null || file.isEmpty()) {
|
if (file == null || file.isEmpty()) {
|
||||||
throw new IllegalArgumentException("File is empty");
|
throw new IllegalArgumentException("File is empty");
|
||||||
|
|||||||
@@ -272,6 +272,15 @@ public class LibraryService implements ApplicationContextAware {
|
|||||||
return current != null ? current.getImagePath() : "/images/default";
|
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) {
|
public boolean changeLibraryPassword(String libraryId, String currentPassword, String newPassword) {
|
||||||
Library library = libraries.get(libraryId);
|
Library library = libraries.get(libraryId);
|
||||||
if (library == null) {
|
if (library == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user