Search in sources :

Example 16 with Error

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));
    }
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) Dimension(java.awt.Dimension) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Example 17 with Error

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);
}
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 18 with Error

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);
}
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 19 with Error

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()));
    }
}
Also used : ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) NotFoundApiException(com.faforever.api.error.NotFoundApiException)

Example 20 with Error

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;
}
Also used : Clan(com.faforever.api.data.domain.Clan) Error(com.faforever.api.error.Error) ProgrammingError(com.faforever.api.error.ProgrammingError) ClanMembership(com.faforever.api.data.domain.ClanMembership) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Aggregations

ApiException (com.faforever.api.error.ApiException)29 Error (com.faforever.api.error.Error)29 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Path (java.nio.file.Path)6 ArrayList (java.util.ArrayList)6 Player (com.faforever.api.data.domain.Player)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 LuaValue (org.luaj.vm2.LuaValue)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Clan (com.faforever.api.data.domain.Clan)3 User (com.faforever.api.data.domain.User)3 InvitationResult (com.faforever.api.clan.result.InvitationResult)2 Achievement (com.faforever.api.data.domain.Achievement)2 Avatar (com.faforever.api.data.domain.Avatar)2 ClanMembership (com.faforever.api.data.domain.ClanMembership)2 Map (com.faforever.api.data.domain.Map)2 PlayerAchievement (com.faforever.api.data.domain.PlayerAchievement)2 Vote (com.faforever.api.data.domain.Vote)2 VotingSubject (com.faforever.api.data.domain.VotingSubject)2