Search in sources :

Example 46 with ApiException

use of com.faforever.api.error.ApiException 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 47 with ApiException

use of com.faforever.api.error.ApiException 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 48 with ApiException

use of com.faforever.api.error.ApiException 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)48 Error (com.faforever.api.error.Error)29 Player (com.faforever.api.data.domain.Player)18 Test (org.junit.Test)18 ProgrammingError (com.faforever.api.error.ProgrammingError)13 SneakyThrows (lombok.SneakyThrows)11 Clan (com.faforever.api.data.domain.Clan)10 ClanMembership (com.faforever.api.data.domain.ClanMembership)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 BufferedInputStream (java.io.BufferedInputStream)6 FileInputStream (java.io.FileInputStream)6 ArrayList (java.util.ArrayList)6 ZipInputStream (java.util.zip.ZipInputStream)6 Map (com.faforever.api.config.FafApiProperties.Map)5 Vote (com.faforever.api.data.domain.Vote)5 VotingSubject (com.faforever.api.data.domain.VotingSubject)5 NotFoundApiException (com.faforever.api.error.NotFoundApiException)5 Jwt (org.springframework.security.jwt.Jwt)5 LuaValue (org.luaj.vm2.LuaValue)4