From 0c3054df1b47ae8ed408352711b612cadeeacde2 Mon Sep 17 00:00:00 2001 From: mukeshs Date: Fri, 10 Oct 2025 13:29:37 +0530 Subject: [PATCH] Image error solve update --- .../backend/controller/FileController.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/controller/FileController.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/controller/FileController.java index b3f3802..21c4972 100644 --- a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/controller/FileController.java +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/controller/FileController.java @@ -18,7 +18,7 @@ import java.util.UUID; @RestController @RequestMapping("/api/files") @CrossOrigin(origins = "*") -public class FileController { // Changed from FileUploadController to FileController +public class FileController { @Value("${file.upload.directory:uploads}") private String uploadDirectory; @@ -31,6 +31,7 @@ public class FileController { // Changed from FileUploadController to FileContro System.out.println("=== FILE UPLOAD DEBUG ==="); System.out.println("File name: " + file.getOriginalFilename()); System.out.println("Content type: " + file.getContentType()); + System.out.println("Base URL: " + baseUrl); try { if (file.isEmpty()) { @@ -58,12 +59,14 @@ public class FileController { // Changed from FileUploadController to FileContro Path filePath = uploadPath.resolve(uniqueFilename); Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING); - // Return file URL - use generic /files/ path for documents - String fileUrl = "/uploads/" + uniqueFilename; + // FIXED: Return full URL with base URL + String fileUrl = baseUrl + "/uploads/" + uniqueFilename; Map response = new HashMap<>(); response.put("url", fileUrl); response.put("filename", uniqueFilename); + System.out.println("File uploaded successfully. URL: " + fileUrl); + return ResponseEntity.ok(response); } catch (IOException e) { @@ -88,6 +91,23 @@ public class FileController { // Changed from FileUploadController to FileContro ); } + @DeleteMapping("/images/{filename}") + public ResponseEntity deleteImage(@PathVariable String filename) { + try { + Path filePath = Paths.get(uploadDirectory).resolve(filename); + if (!Files.exists(filePath)) { + return ResponseEntity.notFound().build(); + } + + Files.delete(filePath); + return ResponseEntity.ok().body(Map.of("message", "File deleted successfully")); + + } catch (IOException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Failed to delete file: " + e.getMessage()); + } + } + @GetMapping("/images/{filename}") public ResponseEntity getImage(@PathVariable String filename) { try {