Class ImageController

java.lang.Object
com.mt.ecommerce.product.controller.ImageController

@RestController @RequestMapping("/image") public class ImageController extends Object
Controller for managing image uploads and retrievals. Provides endpoints for uploading, updating, deleting, and fetching images.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    deleteImage(String id, String imageName)
    Endpoint to delete an image.
    org.springframework.http.ResponseEntity<byte[]>
    getImage(String imageName)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ImageController

      public ImageController(ImageService imageService)
  • Method Details

    • createDir

      @PostConstruct public void createDir() throws IOException
      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 details
      file - the image file to upload
      altText - 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 file
      altText - the new alternative text for the image
      iamgeID - 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 delete
      imageName - 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