use of com.faforever.commons.mod.ModReader in project faf-java-api by FAForever.
the class ModService method processUploadedMod.
@SneakyThrows
@Transactional
@CacheEvict(value = { Mod.TYPE_NAME, ModVersion.TYPE_NAME }, allEntries = true)
public void processUploadedMod(Path uploadedFile, Player uploader) {
log.debug("Player '{}' uploaded a mod", uploader);
ModReader modReader = new ModReader();
com.faforever.commons.mod.Mod modInfo = modReader.readZip(uploadedFile);
validateModInfo(modInfo);
validateModStructure(uploadedFile);
log.debug("Mod uploaded by user '{}' is valid: {}", uploader, modInfo);
String displayName = modInfo.getName().trim();
short version = (short) Integer.parseInt(modInfo.getVersion().toString());
if (!canUploadMod(displayName, uploader)) {
Mod mod = modRepository.findOneByDisplayName(displayName).orElseThrow(() -> new IllegalStateException("Mod could not be found"));
throw new ApiException(new Error(ErrorCode.MOD_NOT_ORIGINAL_AUTHOR, mod.getAuthor(), displayName));
}
if (modExists(displayName, version)) {
throw new ApiException(new Error(ErrorCode.MOD_VERSION_EXISTS, displayName, version));
}
String uuid = modInfo.getUid();
if (modUidExists(uuid)) {
throw new ApiException(new Error(ErrorCode.MOD_UID_EXISTS, uuid));
}
String zipFileName = generateZipFileName(displayName, version);
Path targetPath = properties.getMod().getTargetDirectory().resolve(zipFileName);
if (Files.exists(targetPath)) {
throw new ApiException(new Error(ErrorCode.MOD_NAME_CONFLICT, zipFileName));
}
Optional<Path> thumbnailPath = extractThumbnail(uploadedFile, version, displayName, modInfo.getIcon());
log.debug("Moving uploaded mod '{}' to: {}", modInfo.getName(), targetPath);
Files.createDirectories(targetPath.getParent(), FilePermissionUtil.directoryPermissionFileAttributes());
Files.move(uploadedFile, targetPath);
FilePermissionUtil.setDefaultFilePermission(targetPath);
try {
store(modInfo, thumbnailPath, uploader, zipFileName);
} catch (Exception exception) {
try {
Files.delete(targetPath);
} catch (IOException ioException) {
log.warn("Could not delete file " + targetPath, ioException);
}
throw exception;
}
}
Aggregations