Search in sources :

Example 1 with ModReader

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;
    }
}
Also used : Path(java.nio.file.Path) Mod(com.faforever.api.data.domain.Mod) Error(com.faforever.api.error.Error) IOException(java.io.IOException) ApiException(com.faforever.api.error.ApiException) IOException(java.io.IOException) ModReader(com.faforever.commons.mod.ModReader) ApiException(com.faforever.api.error.ApiException) CacheEvict(org.springframework.cache.annotation.CacheEvict) SneakyThrows(lombok.SneakyThrows) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Mod (com.faforever.api.data.domain.Mod)1 ApiException (com.faforever.api.error.ApiException)1 Error (com.faforever.api.error.Error)1 ModReader (com.faforever.commons.mod.ModReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 SneakyThrows (lombok.SneakyThrows)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Transactional (org.springframework.transaction.annotation.Transactional)1