Search in sources :

Example 26 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class MapService method postProcessZipFiles.

@SneakyThrows
private void postProcessZipFiles(MapUploadData mapUploadData) {
    Optional<Path> mapFolder;
    try (Stream<Path> mapFolderStream = Files.list(mapUploadData.getBaseDir())) {
        mapFolder = mapFolderStream.filter(path -> Files.isDirectory(path)).findFirst();
    }
    if (!mapFolder.isPresent()) {
        throw new ApiException(new Error(ErrorCode.MAP_MISSING_MAP_FOLDER_INSIDE_ZIP));
    }
    try (Stream<Path> mapFolderStream = Files.list(mapUploadData.getBaseDir())) {
        if (mapFolderStream.count() != 2) {
            throw new ApiException(new Error(ErrorCode.MAP_INVALID_ZIP));
        }
    }
    mapUploadData.setOriginalMapFolder(mapFolder.get());
    mapUploadData.setUploadFolderName(mapUploadData.getOriginalMapFolder().getFileName().toString());
    List<Path> filePaths = new ArrayList<>();
    try (Stream<Path> mapFileStream = Files.list(mapUploadData.getOriginalMapFolder())) {
        mapFileStream.forEach(filePaths::add);
        Arrays.stream(REQUIRED_FILES).forEach(filePattern -> {
            if (filePaths.stream().noneMatch(filePath -> filePath.toString().endsWith(filePattern))) {
                throw new ApiException(new Error(ErrorCode.MAP_FILE_INSIDE_ZIP_MISSING, filePattern));
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) ProgrammingError(com.faforever.api.error.ProgrammingError) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) SneakyThrows(lombok.SneakyThrows)

Example 27 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class MapsController method uploadMap.

@ApiOperation("Upload a map")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 500, message = "Failure") })
@RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public void uploadMap(@RequestParam("file") MultipartFile file, @RequestParam("metadata") String jsonString, Authentication authentication) throws IOException {
    if (file == null) {
        throw new ApiException(new Error(ErrorCode.UPLOAD_FILE_MISSING));
    }
    String extension = Files.getFileExtension(file.getOriginalFilename());
    if (!fafApiProperties.getMap().getAllowedExtensions().contains(extension)) {
        throw new ApiException(new Error(ErrorCode.UPLOAD_INVALID_FILE_EXTENSIONS, fafApiProperties.getMap().getAllowedExtensions()));
    }
    boolean ranked;
    try {
        JsonNode node = objectMapper.readTree(jsonString);
        ranked = node.path("isRanked").asBoolean(false);
    } catch (IOException e) {
        log.debug("Could not parse metadata", e);
        throw new ApiException(new Error(ErrorCode.INVALID_METADATA, e.getMessage()));
    }
    Player player = playerService.getPlayer(authentication);
    mapService.uploadMap(file.getBytes(), file.getOriginalFilename(), player, ranked);
}
Also used : Player(com.faforever.api.data.domain.Player) Error(com.faforever.api.error.Error) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ApiException(com.faforever.api.error.ApiException) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class ModService method validateModInfo.

private void validateModInfo(com.faforever.commons.mod.Mod modInfo) {
    List<Error> errors = new ArrayList<>();
    String name = modInfo.getName();
    if (name == null) {
        errors.add(new Error(ErrorCode.MOD_NAME_MISSING));
    } else if (name.length() > properties.getMod().getMaxNameLength()) {
        errors.add(new Error(ErrorCode.MOD_NAME_TOO_LONG));
    }
    if (modInfo.getUid() == null) {
        errors.add(new Error(ErrorCode.MOD_UID_MISSING));
    }
    if (modInfo.getVersion() == null) {
        errors.add(new Error(ErrorCode.MOD_VERSION_MISSING));
    }
    if (Ints.tryParse(modInfo.getVersion().toString()) == null) {
        errors.add(new Error(ErrorCode.MOD_VERSION_NOT_A_NUMBER, modInfo.getVersion().toString()));
    }
    if (modInfo.getDescription() == null) {
        errors.add(new Error(ErrorCode.MOD_DESCRIPTION_MISSING));
    }
    if (modInfo.getAuthor() == null) {
        errors.add(new Error(ErrorCode.MOD_AUTHOR_MISSING));
    }
    if (!errors.isEmpty()) {
        throw new ApiException(errors.toArray(new Error[0]));
    }
}
Also used : ArrayList(java.util.ArrayList) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException)

Example 29 with Error

use of com.faforever.api.error.Error in project faf-java-api by FAForever.

the class ModsController method uploadMod.

@ApiOperation("Upload a mod")
@RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public void uploadMod(@RequestParam("file") MultipartFile file, Authentication authentication) throws IOException {
    if (file == null) {
        throw new ApiException(new Error(ErrorCode.UPLOAD_FILE_MISSING));
    }
    String extension = Files.getFileExtension(file.getOriginalFilename());
    if (!fafApiProperties.getMod().getAllowedExtensions().contains(extension)) {
        throw new ApiException(new Error(ErrorCode.UPLOAD_INVALID_FILE_EXTENSIONS, fafApiProperties.getMod().getAllowedExtensions()));
    }
    Path tempFile = java.nio.file.Files.createTempFile("mod", ".tmp");
    file.transferTo(tempFile.getFileName().toFile());
    modService.processUploadedMod(tempFile, playerService.getPlayer(authentication));
}
Also used : Path(java.nio.file.Path) Error(com.faforever.api.error.Error) ApiException(com.faforever.api.error.ApiException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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