Example #1
0
  /**
   * upload avatar image and resize the images if need
   *
   * @param avatar
   * @param uploadedFile
   * @return file name
   */
  private String processImageUpload(Avatar avatar, UploadedFile uploadedFile) {
    File file = this.saveImage(avatar, uploadedFile);

    if (file == null) {
      return null;
    }

    avatar.setFileName(file.getName());

    try {
      long size = file.length();
      long maxSize = this.config.getLong(ConfigKeys.AVATAR_MAX_SIZE);

      if (size > maxSize) {
        throw new ValidationException("File size too big");
      }

      ImageInfo ii = new ImageInfo();

      try {
        ii.setInput(new FileInputStream(file));
      } catch (FileNotFoundException e) {
        throw new ForumException(e);
      }

      if (!ii.check()) {
        throw new ValidationException("Not a supported image file format.");
      }

      avatar.setHeight(ii.getHeight());
      avatar.setWidth(ii.getWidth());

      // check the image size
      this.checkImageSize(avatar);
    } catch (ValidationException e) {
      file.delete();
      throw e;
    }

    return file.getName();
  }
Example #2
0
  /**
   * Updates a existing avatar
   *
   * @param avatar
   * @param image
   */
  public void update(Avatar avatar, UploadedFile uploadedFile) {
    this.isAllowed(avatar);

    if (avatar.getId() == 0) {
      throw new ValidationException("update() expects a avatar with an existing id");
    }

    Avatar current = this.repository.get(avatar.getId());
    avatar.setAvatarType(current.getAvatarType());

    // upload the img and get the upload img info
    String imageDiskName = this.processImageUpload(avatar, uploadedFile);

    if (imageDiskName != null) {
      this.deleteImage(current);

      current.setFileName(imageDiskName);
      current.setHeight(avatar.getHeight());
      current.setWidth(avatar.getWidth());
    }

    this.repository.update(current);
  }