Class ImageController
java.lang.Object
com.mt.ecommerce.product.controller.ImageController
Controller for managing image uploads and retrievals.
Provides endpoints for uploading, updating, deleting, and fetching images.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
deleteImage
(String id, String imageName) Endpoint to delete an image.org.springframework.http.ResponseEntity<byte[]>
Endpoint to fetch and display an image by its name.getImages
(org.springframework.security.core.userdetails.UserDetails userDetails) Endpoint to fetch all images for the authenticated user.void
updateImage
(org.springframework.web.multipart.MultipartFile file, String altText, String iamgeID) Endpoint to update an existing image.void
uploadImage
(org.springframework.security.core.userdetails.UserDetails userDetails, org.springframework.web.multipart.MultipartFile file, String altText) Endpoint to upload a new image.
-
Constructor Details
-
ImageController
-
-
Method Details
-
createDir
- Throws:
IOException
-
uploadImage
@PostMapping("") @PreAuthorize("hasAnyAuthority(\'ROLE_VENDOR\', \'ROLE_ADMIN\')") public void uploadImage(@AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails, @RequestParam("file") org.springframework.web.multipart.MultipartFile file, @RequestParam("altText") String altText) Endpoint to upload a new image. Accessible by users with ROLE_VENDOR or ROLE_ADMIN.- Parameters:
userDetails
- the authenticated user's detailsfile
- the image file to uploadaltText
- the alternative text for the image
-
updateImage
@PutMapping("") @PreAuthorize("hasAnyAuthority(\'ROLE_VENDOR\', \'ROLE_ADMIN\')") public void updateImage(@RequestParam("file") org.springframework.web.multipart.MultipartFile file, @RequestParam("altText") String altText, @RequestParam("iamgeID") String iamgeID) Endpoint to update an existing image. Accessible by users with ROLE_VENDOR or ROLE_ADMIN.- Parameters:
file
- the new image filealtText
- the new alternative text for the imageiamgeID
- the ID of the image to update
-
deleteImage
@DeleteMapping("") @PreAuthorize("hasAnyAuthority(\'ROLE_VENDOR\', \'ROLE_ADMIN\')") public void deleteImage(@RequestParam("id") String id, @RequestParam("imageName") String imageName) throws IOException Endpoint to delete an image. Accessible by users with ROLE_VENDOR or ROLE_ADMIN.- Parameters:
id
- the ID of the image to deleteimageName
- the name of the image file to delete- Throws:
IOException
- if an I/O error occurs
-
getImages
@GetMapping(value="", produces="application/json") public List<ImageBO> getImages(@AuthenticationPrincipal org.springframework.security.core.userdetails.UserDetails userDetails) Endpoint to fetch all images for the authenticated user. Accessible by users with ROLE_VENDOR or ROLE_ADMIN.- Parameters:
userDetails
- the authenticated user's details- Returns:
- a list of ImageBO objects
-
getImage
@GetMapping("/unsecured/display/{imageName}") public org.springframework.http.ResponseEntity<byte[]> getImage(@PathVariable("imageName") String imageName) throws IOException Endpoint to fetch and display an image by its name. This endpoint is unsecured and can be accessed without authentication.- Parameters:
imageName
- the name of the image file to retrieve- Returns:
- ResponseEntity containing the image bytes and appropriate headers, or NOT_FOUND status if the image does not exist
- Throws:
IOException
- if an I/O error occurs while reading the image file
-