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));
}
});
}
}
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);
}
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]));
}
}
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));
}
Aggregations