use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class AvatarService method checkImageDimensions.
private void checkImageDimensions(InputStream imageInputStream, String imageFileName) throws IOException {
imageInputStream.mark(4096);
final Dimension imageDimensions = readImageDimensions(imageInputStream, imageFileName);
imageInputStream.reset();
final int heightLimit = properties.getAvatar().getImageHeight();
final int widthLimit = properties.getAvatar().getImageWidth();
if (imageDimensions.width != widthLimit || imageDimensions.height != heightLimit) {
throw new ApiException(new Error(ErrorCode.INVALID_AVATAR_DIMENSION, widthLimit, heightLimit, imageDimensions.width, imageDimensions.height));
}
}
use of com.faforever.api.error.Error 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.error.Error 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.error.Error in project faf-java-api by FAForever.
the class AvatarService method validateImageFile.
private void validateImageFile(String originalFilename, long avatarImageFileSize) {
final int maxFileNameLength = properties.getAvatar().getMaxNameLength();
final Integer maxFileSizeBytes = properties.getAvatar().getMaxSizeBytes();
String extension = com.google.common.io.Files.getFileExtension(originalFilename);
if (avatarImageFileSize > maxFileSizeBytes) {
throw new ApiException(new Error(ErrorCode.FILE_SIZE_EXCEEDED, maxFileSizeBytes, avatarImageFileSize));
}
if (!properties.getAvatar().getAllowedExtensions().contains(extension)) {
throw new ApiException(new Error(ErrorCode.UPLOAD_INVALID_FILE_EXTENSIONS, properties.getAvatar().getAllowedExtensions()));
}
if (originalFilename.length() > maxFileNameLength) {
throw new ApiException(new Error(ErrorCode.FILE_NAME_TOO_LONG, maxFileNameLength, originalFilename.length()));
}
}
use of com.faforever.api.error.Error in project faf-java-api by FAForever.
the class ClanService method create.
@SneakyThrows
Clan create(String name, String tag, String description, Player creator) {
if (!creator.getClanMemberships().isEmpty()) {
throw new ApiException(new Error(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
}
if (clanRepository.findOneByName(name).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_NAME_EXISTS, name));
}
if (clanRepository.findOneByTag(tag).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_TAG_EXISTS, name));
}
Clan clan = new Clan();
clan.setName(name);
clan.setTag(tag);
clan.setDescription(description);
clan.setFounder(creator);
clan.setLeader(creator);
ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(creator);
clan.setMemberships(Collections.singletonList(membership));
// clan membership is saved over cascading, otherwise validation will fail
clanRepository.save(clan);
return clan;
}
Aggregations