fix of images deletion
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -93,6 +93,11 @@
|
|||||||
<artifactId>jakarta.validation-api</artifactId>
|
<artifactId>jakarta.validation-api</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.springdoc</groupId>-->
|
||||||
|
<!-- <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>-->
|
||||||
|
<!-- <version>2.8.5</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -78,12 +78,12 @@ public class ImageController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/delete/{id}")
|
@DeleteMapping("/delete/{id}")
|
||||||
public ResponseEntity deleteImage(@PathVariable("id") String filename) {
|
public ResponseEntity<RequestResponseDTO> deleteImage(@PathVariable("id") String filename) {
|
||||||
try {
|
try {
|
||||||
imageService.deleteImage(uploadDir, filename);
|
imageService.deleteImage(uploadDir, filename);
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
return ResponseEntity.status(HttpStatus.OK).body(new RequestResponseDTO("Image deleted successfully."));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new RequestResponseDTO(e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,14 +60,13 @@ public class ImageService {
|
|||||||
return resource;
|
return resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String deleteImage(String imageDirectory, String imageName) throws IOException {
|
public void deleteImage(String imageDirectory, String imageName) throws IOException {
|
||||||
Path imagePath = Path.of(imageDirectory, imageName);
|
Path imagePath = Path.of(imageDirectory, imageName);
|
||||||
|
|
||||||
if (Files.exists(imagePath)) {
|
if (Files.exists(imagePath)) {
|
||||||
Files.delete(imagePath);
|
Files.delete(imagePath);
|
||||||
return "Success";
|
|
||||||
} else {
|
} else {
|
||||||
return "Failed"; // Handle missing images
|
throw new IOException("File not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user