Search in sources :

Example 6 with Avatar

use of com.faforever.api.data.domain.Avatar 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 7 with Avatar

use of com.faforever.api.data.domain.Avatar 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 8 with Avatar

use of com.faforever.api.data.domain.Avatar 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)

Example 9 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarServiceTest method duplicateAvatarUpload.

@Test
public void duplicateAvatarUpload() throws Exception {
    final String avatarFileName = VALID_AVATAR_FILENAME;
    when(avatarRepository.findOneByUrl(String.format(DOWNLOAD_URL_FORMAT, avatarFileName))).thenReturn(Optional.of(new Avatar()));
    try (final InputStream imageInputStream = loadResource(avatarFileName).openStream()) {
        expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.AVATAR_NAME_CONFLICT));
        avatarService.createAvatar(AVATAR_METADATA, avatarFileName, imageInputStream, VALID_FILE_SIZE);
    }
}
Also used : InputStream(java.io.InputStream) Avatar(com.faforever.api.data.domain.Avatar) Test(org.junit.Test)

Example 10 with Avatar

use of com.faforever.api.data.domain.Avatar in project faf-java-api by FAForever.

the class AvatarServiceTest method newAvatarUploadWithValidName.

@Test
public void newAvatarUploadWithValidName() throws Exception {
    final String avatarFileName = VALID_AVATAR_FILENAME;
    final URL imageResource = loadResource(avatarFileName);
    try (final InputStream imageInputStream = imageResource.openStream()) {
        avatarService.createAvatar(AVATAR_METADATA, "[./><" + avatarFileName, imageInputStream, VALID_FILE_SIZE);
        ArgumentCaptor<Avatar> avatarCaptor = ArgumentCaptor.forClass(Avatar.class);
        verify(avatarRepository, times(1)).save(avatarCaptor.capture());
        final Avatar storedAvatar = avatarCaptor.getValue();
        assertEquals(String.format(DOWNLOAD_URL_FORMAT, avatarFileName), storedAvatar.getUrl());
        assertEquals(AVATAR_NAME, storedAvatar.getTooltip());
        assertThat(avatarsPath.resolve(avatarFileName).toFile().length(), is(imageResource.openConnection().getContentLengthLong()));
    }
}
Also used : InputStream(java.io.InputStream) URL(java.net.URL) Avatar(com.faforever.api.data.domain.Avatar) Test(org.junit.Test)

Aggregations

Avatar (com.faforever.api.data.domain.Avatar)12 Test (org.junit.Test)8 InputStream (java.io.InputStream)5 Path (java.nio.file.Path)5 Audit (com.faforever.api.security.Audit)3 SneakyThrows (lombok.SneakyThrows)3 Transactional (org.springframework.transaction.annotation.Transactional)3 AbstractIntegrationTest (com.faforever.api.AbstractIntegrationTest)2 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 URL (java.net.URL)2 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)2 WithUserDetails (org.springframework.security.test.context.support.WithUserDetails)2 FafApiProperties (com.faforever.api.config.FafApiProperties)1 AvatarAssignment (com.faforever.api.data.domain.AvatarAssignment)1 Before (org.junit.Before)1