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);
}
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);
}
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);
}
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);
}
}
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()));
}
}
Aggregations