Search in sources :

Example 1 with Audit

use of com.faforever.api.security.Audit in project faf-java-api by FAForever.

the class AvatarService method updateAvatar.

@SneakyThrows
@Transactional
@Audit(messageTemplate = "Avatar ''{0}'' updated with [''{1}'' - ''{2}''].", expressions = { "${avatarId}", "${avatarMetadata.name}", "${originalFilename}" })
public void updateAvatar(Integer avatarId, AvatarMetadata avatarMetadata, String originalFilename, InputStream imageDataInputStream, long avatarImageFileSize) {
    final Avatar existingAvatar = getExistingAvatar(avatarId);
    final String normalizedAvatarFileName = getFileNameFromUrl(existingAvatar.getUrl());
    existingAvatar.setTooltip(avatarMetadata.getName());
    final InputStream markSupportedImageInputStream = getMarkSupportedInputStream(imageDataInputStream);
    validateImageFile(originalFilename, avatarImageFileSize);
    checkImageDimensions(markSupportedImageInputStream, originalFilename);
    final Path imageTargetPath = properties.getAvatar().getTargetDirectory().resolve(normalizedAvatarFileName);
    avatarRepository.save(existingAvatar);
    copyAvatarFile(markSupportedImageInputStream, imageTargetPath, true);
}
Also used : Path(java.nio.file.Path) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) Avatar(com.faforever.api.data.domain.Avatar) Audit(com.faforever.api.security.Audit) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Audit

use of com.faforever.api.security.Audit in project faf-java-api by FAForever.

the class AvatarService method createAvatar.

@SneakyThrows
@Transactional
@Audit(messageTemplate = "Avatar [''{0}'' - ''{1}''] created.", expressions = { "${avatarMetadata.name}", "${originalFilename}" })
public void createAvatar(AvatarMetadata avatarMetadata, String originalFilename, InputStream imageDataInputStream, long avatarImageFileSize) {
    final Avatar avatarToCreate = new Avatar();
    final String normalizedAvatarFileName = FileNameUtil.normalizeFileName(originalFilename);
    String url = String.format(properties.getAvatar().getDownloadUrlFormat(), normalizedAvatarFileName);
    avatarRepository.findOneByUrl(url).ifPresent(existingAvatar -> {
        throw new ApiException(new Error(ErrorCode.AVATAR_NAME_CONFLICT, normalizedAvatarFileName));
    });
    avatarToCreate.setTooltip(avatarMetadata.getName()).setUrl(url);
    final InputStream markSupportedImageInputStream = getMarkSupportedInputStream(imageDataInputStream);
    validateImageFile(originalFilename, avatarImageFileSize);
    checkImageDimensions(markSupportedImageInputStream, normalizedAvatarFileName);
    final Path imageTargetPath = properties.getAvatar().getTargetDirectory().resolve(normalizedAvatarFileName);
    avatarRepository.save(avatarToCreate);
    copyAvatarFile(markSupportedImageInputStream, imageTargetPath, false);
}
Also used : Path(java.nio.file.Path) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Avatar(com.faforever.api.data.domain.Avatar) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException) Audit(com.faforever.api.security.Audit) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Audit

use of com.faforever.api.security.Audit in project faf-java-api by FAForever.

the class AvatarService method deleteAvatar.

@SneakyThrows
@Transactional
@Audit(messageTemplate = "Avatar ''{0}'' deleted.", expressions = "${avatarId}")
public void deleteAvatar(Integer avatarId) {
    final Avatar avatar = getExistingAvatar(avatarId);
    if (!avatar.getAssignments().isEmpty()) {
        throw new ApiException(new Error(ErrorCode.AVATAR_IN_USE, avatarId));
    }
    // TODO: 21.11.2017 !!!!!!!!!!!! HACK TO GET FILENAME FROM URL..... !!!!!!!!!!!!!!!
    final String fileName = getFileNameFromUrl(avatar.getUrl());
    final Path avatarImageFilePath = properties.getAvatar().getTargetDirectory().resolve(fileName);
    Files.deleteIfExists(avatarImageFilePath);
    avatarRepository.delete(avatar);
}
Also used : Path(java.nio.file.Path) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Avatar(com.faforever.api.data.domain.Avatar) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException) Audit(com.faforever.api.security.Audit) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Avatar (com.faforever.api.data.domain.Avatar)3 Audit (com.faforever.api.security.Audit)3 Path (java.nio.file.Path)3 SneakyThrows (lombok.SneakyThrows)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ApiException (com.faforever.api.error.ApiException)2 Error (com.faforever.api.error.Error)2 NotFoundApiException (com.faforever.api.error.NotFoundApiException)2 ProgrammingError (com.faforever.api.error.ProgrammingError)2 InputStream (java.io.InputStream)2 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)2