Image error solve update
This commit is contained in:
@ -18,7 +18,7 @@ import java.util.UUID;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/files")
|
@RequestMapping("/api/files")
|
||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
public class FileController { // Changed from FileUploadController to FileController
|
public class FileController {
|
||||||
|
|
||||||
@Value("${file.upload.directory:uploads}")
|
@Value("${file.upload.directory:uploads}")
|
||||||
private String uploadDirectory;
|
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 UPLOAD DEBUG ===");
|
||||||
System.out.println("File name: " + file.getOriginalFilename());
|
System.out.println("File name: " + file.getOriginalFilename());
|
||||||
System.out.println("Content type: " + file.getContentType());
|
System.out.println("Content type: " + file.getContentType());
|
||||||
|
System.out.println("Base URL: " + baseUrl);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (file.isEmpty()) {
|
if (file.isEmpty()) {
|
||||||
@ -58,12 +59,14 @@ public class FileController { // Changed from FileUploadController to FileContro
|
|||||||
Path filePath = uploadPath.resolve(uniqueFilename);
|
Path filePath = uploadPath.resolve(uniqueFilename);
|
||||||
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
// Return file URL - use generic /files/ path for documents
|
// FIXED: Return full URL with base URL
|
||||||
String fileUrl = "/uploads/" + uniqueFilename;
|
String fileUrl = baseUrl + "/uploads/" + uniqueFilename;
|
||||||
Map<String, String> response = new HashMap<>();
|
Map<String, String> response = new HashMap<>();
|
||||||
response.put("url", fileUrl);
|
response.put("url", fileUrl);
|
||||||
response.put("filename", uniqueFilename);
|
response.put("filename", uniqueFilename);
|
||||||
|
|
||||||
|
System.out.println("File uploaded successfully. URL: " + fileUrl);
|
||||||
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} 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}")
|
@GetMapping("/images/{filename}")
|
||||||
public ResponseEntity<byte[]> getImage(@PathVariable String filename) {
|
public ResponseEntity<byte[]> getImage(@PathVariable String filename) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user