Ejemplo n.º 1
0
 @RequestMapping(method = RequestMethod.GET, value = "/{userId}/picture", produces = "image/jpeg")
 public byte[] pictureGet(@PathVariable("userId") Long userId) {
   try {
     return userService.getUserPicture(userId);
   } catch (SportManagerException e1) {
     String msg = "Can't retrieve user picture with id " + userId + " from DB";
     logger.error(msg, e1);
     throw new DefaultSportManagerException(
         new ErrorResource("db error", msg, HttpStatus.INTERNAL_SERVER_ERROR));
   }
 }
Ejemplo n.º 2
0
  /**
   * Delete the picture of a specified user
   *
   * @param userId the user identifier
   * @return reponse
   */
  @RequestMapping(method = RequestMethod.DELETE, value = "/{userId}/picture")
  @ResponseStatus(HttpStatus.OK)
  @PreAuthorize(value = "hasRole('AK_ADMIN')")
  public void pictureDelete(@PathVariable("userId") Long userId) {

    try {
      userService.deleteUserPicture(userId);
    } catch (SportManagerException e) {
      String msg = "Can't delete user picture with id " + userId + " from DB";
      logger.error(msg, e);
      throw new DefaultSportManagerException(
          new ErrorResource("db error", msg, HttpStatus.INTERNAL_SERVER_ERROR));
    }
  }
Ejemplo n.º 3
0
  /**
   * Affect a image to a specified user
   *
   * @param userId the user identifier
   * @param uploadedInputStream the image uploaded input stream
   * @param fileDetail the image detail format
   * @return response
   */
  @RequestMapping(method = RequestMethod.POST, value = "/{userId}/picture")
  @ResponseStatus(HttpStatus.OK)
  @PreAuthorize(value = "hasRole('AK_ADMIN')")
  public void pictureUpdate(
      @PathVariable("userId") Long userId, @RequestParam MultipartFile user_picture) {

    // Le nom du paramètre correspond au nom du champ de formulaire qu'il faut utiliser !!!!

    if (user_picture == null || user_picture.isEmpty()) {
      logger.debug("Incorrect input stream or file name for image of user" + userId);
      throw new DefaultSportManagerException(
          new ErrorResource(
              "parameter missing",
              "Picture should be uploaded as a multipart/form-data file param named user_picture",
              HttpStatus.BAD_REQUEST));
    }

    if (!(user_picture.getOriginalFilename().endsWith(".jpg")
        || user_picture.getOriginalFilename().endsWith(".jpeg"))) {
      logger.debug("File for picture of user" + userId + " must be a JPEG file.");
      throw new DefaultSportManagerException(
          new ErrorResource(
              "incorrect file format", "Picture should be a JPG file", HttpStatus.BAD_REQUEST));
    }

    try {
      userService.addUserPicture(userId, user_picture.getInputStream());
    } catch (SportManagerException e) {
      String msg = "Can't update user picture with id " + userId + " into DB";
      logger.error(msg, e);
      throw new DefaultSportManagerException(
          new ErrorResource("db error", msg, HttpStatus.INTERNAL_SERVER_ERROR));
    } catch (IOException e) {
      String msg = "Can't update user picture with id " + userId + " because of IO Error";
      logger.error(msg, e);
      throw new DefaultSportManagerException(
          new ErrorResource("io error", msg, HttpStatus.INTERNAL_SERVER_ERROR));
    }
  }