Пример #1
0
  /**
   * check the user can do this operation according to config
   *
   * @param avatar
   */
  private void isAllowed(Avatar avatar) {
    this.applyCommonConstraints(avatar);

    boolean allowGallery = this.config.getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY);
    boolean allowUpload = this.config.getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD);

    if (avatar.getAvatarType() == AvatarType.AVATAR_UPLOAD && !allowUpload
        || avatar.getAvatarType() == AvatarType.AVATAR_GALLERY && !allowGallery) {
      throw new ValidationException(avatar.getAvatarType() + "is not allowed!");
    }
  }
Пример #2
0
  /**
   * fix the size of the image true, chenged the image size false, not change
   *
   * @param avatar
   */
  private void checkImageSize(Avatar avatar) {
    int maxWidth = this.config.getInt(ConfigKeys.AVATAR_MAX_WIDTH);
    int maxHeight = this.config.getInt(ConfigKeys.AVATAR_MAX_HEIGHT);
    int minWidth = this.config.getInt(ConfigKeys.AVATAR_MIN_WIDTH);
    int minHeight = this.config.getInt(ConfigKeys.AVATAR_MIN_HEIGHT);

    int height = avatar.getHeight();
    int width = avatar.getWidth();

    if (height < minHeight || height > maxHeight || width < minWidth || width > maxWidth) {
      throw new ValidationException("This image size is not allowed!");
    }
  }
Пример #3
0
  /**
   * get the key in config that for give avatar to find the path
   *
   * @param avatar
   * @return
   */
  private String getAvatarPathConfigKey(Avatar avatar) {
    String avatarConfigKey = null;

    if (avatar == null) {
      return avatarConfigKey;
    }

    if (avatar.getAvatarType() == AvatarType.AVATAR_UPLOAD) {
      avatarConfigKey = ConfigKeys.AVATAR_UPLOAD_DIR;
    } else if (avatar.getAvatarType() == AvatarType.AVATAR_GALLERY) {
      avatarConfigKey = ConfigKeys.AVATAR_GALLERY_DIR;
    }

    return avatarConfigKey;
  }
Пример #4
0
  @Test
  public void addExpectSuccess() throws IOException {
    final Avatar avatar = new Avatar();
    File tempFile = File.createTempFile("jforum", "tests");
    tempFile.deleteOnExit();
    final String tempDir = tempFile.getParent();

    File file = new File(this.getClass().getResource("/smilies/smilie.gif").getFile());
    TestCaseUtils.copyFile(file, tempFile);

    UploadedFile uploadedFile =
        new DefaultUploadedFile(new FileInputStream(file), file.getAbsolutePath(), "");

    context.checking(
        new Expectations() {
          {
            one(config).getApplicationPath();
            will(returnValue(tempDir));
            one(config).getValue(ConfigKeys.AVATAR_GALLERY_DIR);
            will(returnValue(""));
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY);
            will(returnValue(true));
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD);
            will(returnValue(true));
            one(config).getLong(ConfigKeys.AVATAR_MAX_SIZE);
            will(returnValue(10000l));
            one(config).getInt(ConfigKeys.AVATAR_MAX_WIDTH);
            will(returnValue(800));
            one(config).getInt(ConfigKeys.AVATAR_MAX_HEIGHT);
            will(returnValue(600));
            one(config).getInt(ConfigKeys.AVATAR_MIN_WIDTH);
            will(returnValue(1));
            one(config).getInt(ConfigKeys.AVATAR_MIN_HEIGHT);
            will(returnValue(1));
            one(repository).add(avatar);
          }
        });

    service.add(avatar, uploadedFile);
    context.assertIsSatisfied();
    Assert.assertNotNull(avatar.getFileName());

    File expectedFile = new File(String.format("%s/%s/%s", tempDir, "", avatar.getFileName()));
    expectedFile.deleteOnExit();

    Assert.assertTrue(expectedFile.exists());
  }
Пример #5
0
  @Test(expected = ValidationException.class)
  public void updateWithoutIdExpectException() {
    Avatar avatar = new Avatar();
    avatar.setId(0);

    context.checking(
        new Expectations() {
          {
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY);
            will(returnValue(true));
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD);
            will(returnValue(true));
          }
        });

    service.update(avatar, null);
  }
Пример #6
0
  /**
   * Add avatar without upload the image no operation on Image
   *
   * @param avatar
   */
  public void add(Avatar avatar) {
    this.isAllowed(avatar);

    if (avatar.getId() > 0) {
      throw new ValidationException("Cannot add an existing (id > 0) avatar");
    }

    this.checkImageSize(avatar);
    this.repository.add(avatar);
  }
Пример #7
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();
  }
Пример #8
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);
  }
Пример #9
0
  private File getAvatarImageFile(Avatar avatar) {
    String avatarConfigKey = getAvatarPathConfigKey(avatar);

    if (avatarConfigKey == null) {
      return null;
    } else {
      String imageName = avatar.getFileName();
      String imageFilePath =
          String.format(
              "%s/%s/%s",
              this.config.getApplicationPath(), this.config.getValue(avatarConfigKey), imageName);

      return new File(imageFilePath);
    }
  }
Пример #10
0
  /**
   * Adds a new avatar
   *
   * @param avatar
   * @param image
   */
  public void add(Avatar avatar, UploadedFile uploadedFile) {
    if (uploadedFile == null) {
      this.add(avatar);
      return;
    }

    this.isAllowed(avatar);

    if (avatar.getId() > 0) {
      throw new ValidationException("Cannot add an existing (id > 0) avatar");
    }

    String imgName = this.processImageUpload(avatar, uploadedFile);

    if (imgName != null) {
      this.repository.add(avatar);
    }
  }
Пример #11
0
  @Test
  public void updateImageShouldDeleteOldImage() throws IOException {
    final File currentFile = File.createTempFile("avatar", "tests");
    currentFile.deleteOnExit();

    final Avatar currentAvatar = new Avatar();
    currentAvatar.setId(1);
    currentAvatar.setAvatarType(AvatarType.AVATAR_GALLERY);
    currentAvatar.setFileName(currentFile.getName());

    context.checking(
        new Expectations() {
          {
            one(repository).get(1);
            will(returnValue(currentAvatar));
            atLeast(1).of(config).getApplicationPath();
            will(returnValue(currentFile.getParent()));
            atLeast(1).of(config).getValue(ConfigKeys.AVATAR_GALLERY_DIR);
            will(returnValue(""));
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_GALLERY);
            will(returnValue(true));
            one(config).getBoolean(ConfigKeys.AVATAR_ALLOW_UPLOAD);
            will(returnValue(true));
            one(config).getLong(ConfigKeys.AVATAR_MAX_SIZE);
            will(returnValue(10000l));
            one(config).getInt(ConfigKeys.AVATAR_MAX_WIDTH);
            will(returnValue(800));
            one(config).getInt(ConfigKeys.AVATAR_MAX_HEIGHT);
            will(returnValue(600));
            one(config).getInt(ConfigKeys.AVATAR_MIN_WIDTH);
            will(returnValue(1));
            one(config).getInt(ConfigKeys.AVATAR_MIN_HEIGHT);
            will(returnValue(1));
            one(repository).update(currentAvatar);
          }
        });

    File originalFile = new File(this.getClass().getResource("/smilies/smilie.gif").getFile());
    File newFile = File.createTempFile("jforum", "tests");
    TestCaseUtils.copyFile(originalFile, newFile);

    UploadedFile uploadedFile =
        new DefaultUploadedFile(new FileInputStream(newFile), newFile.getAbsolutePath(), "");

    String oldDiskName = currentAvatar.getFileName();

    Avatar newAvatar = new Avatar();
    newAvatar.setId(1);
    newAvatar.setAvatarType(AvatarType.AVATAR_GALLERY);
    service.update(newAvatar, uploadedFile);
    context.assertIsSatisfied();

    Assert.assertEquals(newAvatar.getAvatarType(), currentAvatar.getAvatarType());
    Assert.assertFalse(currentFile.exists());
    Assert.assertFalse(currentAvatar.getFileName().equals(oldDiskName));

    new File(String.format("%s/%s", currentFile.getParent(), currentAvatar.getFileName())).delete();
  }