use of com.faforever.api.error.ApiException 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.ApiException 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.ApiException 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;
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class AchievementService method updateSteps.
private UpdatedAchievementResponse updateSteps(int playerId, String achievementId, int steps, BiFunction<Integer, Integer, Integer> stepsFunction) {
Achievement achievement = achievementRepository.getOne(achievementId);
if (achievement.getType() != AchievementType.INCREMENTAL) {
throw new ApiException(new Error(ACHIEVEMENT_NOT_INCREMENTAL, achievementId));
}
PlayerAchievement playerAchievement = getOrCreatePlayerAchievement(playerId, achievement, AchievementState.REVEALED);
int currentSteps = MoreObjects.firstNonNull(playerAchievement.getCurrentSteps(), 0);
int newCurrentSteps = stepsFunction.apply(currentSteps, steps);
boolean newlyUnlocked = false;
if (newCurrentSteps >= achievement.getTotalSteps()) {
playerAchievement.setState(AchievementState.UNLOCKED);
playerAchievement.setCurrentSteps(achievement.getTotalSteps());
newlyUnlocked = playerAchievement.getState() != AchievementState.UNLOCKED;
} else {
playerAchievement.setCurrentSteps(newCurrentSteps);
}
playerAchievementRepository.save(playerAchievement);
return new UpdatedAchievementResponse(achievementId, newlyUnlocked, playerAchievement.getState(), playerAchievement.getCurrentSteps());
}
use of com.faforever.api.error.ApiException in project faf-java-api by FAForever.
the class MapServiceTest method mapWithMoreRootFoldersInZip.
@Test
public void mapWithMoreRootFoldersInZip() throws IOException {
String zipFilename = "scmp_037_invalid.zip";
when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.empty());
try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
byte[] mapData = ByteStreams.toByteArray(inputStream);
try {
instance.uploadMap(mapData, zipFilename, author, true);
} catch (ApiException e) {
assertThat(e, apiExceptionWithCode(ErrorCode.MAP_INVALID_ZIP));
}
}
}
Aggregations