Search in sources :

Example 1 with Mod

use of com.faforever.api.data.domain.Mod in project faf-java-api by FAForever.

the class ModServiceTest method processUploadedMod.

@Test
@SuppressWarnings("unchecked")
public void processUploadedMod() throws Exception {
    Path uploadedFile = prepareMod(TEST_MOD);
    Player uploader = new Player();
    instance.processUploadedMod(uploadedFile, uploader);
    assertThat(Files.exists(temporaryFolder.getRoot().toPath().resolve("mods/no_friendly_fire.v0003.zip")), is(true));
    assertThat(Files.exists(temporaryFolder.getRoot().toPath().resolve("thumbnails/no_friendly_fire.v0003.png")), is(true));
    ArgumentCaptor<Mod> modCaptor = ArgumentCaptor.forClass(Mod.class);
    verify(modRepository).save(modCaptor.capture());
    Mod savedMod = modCaptor.getValue();
    assertThat(savedMod.getId(), is(nullValue()));
    assertThat(savedMod.getAuthor(), is("IceDreamer"));
    assertThat(savedMod.getDisplayName(), is("No Friendly Fire"));
    assertThat(savedMod.getUploader(), is(uploader));
    ModVersion savedModVersion = savedMod.getVersions().get(0);
    assertThat(savedModVersion.getId(), is(nullValue()));
    assertThat(savedModVersion.getIcon(), is("no_friendly_fire.v0003.png"));
    assertThat(savedModVersion.getFilename(), is("mods/no_friendly_fire.v0003.zip"));
    assertThat(savedModVersion.getUid(), is("26778D4E-BA75-5CC2-CBA8-63795BDE74AA"));
    assertThat(savedModVersion.getDescription(), is("All friendly fire, including between allies, is turned off."));
    assertThat(savedModVersion.getMod(), is(savedMod));
    assertThat(savedModVersion.isRanked(), is(false));
    assertThat(savedModVersion.isHidden(), is(false));
    ArgumentCaptor<Example<ModVersion>> exampleCaptor = ArgumentCaptor.forClass((Class) ModVersion.class);
    verify(modVersionRepository).exists(exampleCaptor.capture());
    verify(modVersionRepository).existsByUid("26778D4E-BA75-5CC2-CBA8-63795BDE74AA");
}
Also used : Path(java.nio.file.Path) ModVersion(com.faforever.api.data.domain.ModVersion) Player(com.faforever.api.data.domain.Player) Mod(com.faforever.api.data.domain.Mod) Example(org.springframework.data.domain.Example) Test(org.junit.Test)

Example 2 with Mod

use of com.faforever.api.data.domain.Mod in project faf-java-api by FAForever.

the class ModService method store.

private void store(com.faforever.commons.mod.Mod modInfo, Optional<Path> thumbnailPath, Player uploader, String zipFileName) {
    ModVersion modVersion = new ModVersion().setUid(modInfo.getUid()).setType(modInfo.isUiOnly() ? ModType.UI : ModType.SIM).setDescription(modInfo.getDescription()).setVersion((short) Integer.parseInt(modInfo.getVersion().toString())).setFilename(MOD_PATH_PREFIX + zipFileName).setIcon(thumbnailPath.map(path -> path.getFileName().toString()).orElse(null));
    Mod mod = modRepository.findOneByDisplayName(modInfo.getName()).orElse(new Mod().setAuthor(modInfo.getAuthor()).setDisplayName(modInfo.getName()).setVersions(new ArrayList<>()).setUploader(uploader));
    mod.getVersions().add(modVersion);
    modVersion.setMod(mod);
    mod = modRepository.save(mod);
    modRepository.insertModStats(mod.getDisplayName());
}
Also used : ModVersion(com.faforever.api.data.domain.ModVersion) Mod(com.faforever.api.data.domain.Mod)

Example 3 with Mod

use of com.faforever.api.data.domain.Mod 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)

Example 4 with Mod

use of com.faforever.api.data.domain.Mod in project faf-java-api by FAForever.

the class ModServiceTest method testNotOriginalUploader.

@Test
public void testNotOriginalUploader() throws Exception {
    Path uploadedFile = prepareMod(TEST_MOD);
    Player uploader = new Player();
    when(modRepository.existsByDisplayNameIgnoreCaseAndUploaderIsNot("No Friendly Fire", uploader)).thenReturn(true);
    when(modRepository.findOneByDisplayName("No Friendly Fire")).thenReturn(Optional.of(new Mod()));
    expectedException.expect(ApiExceptionWithCode.apiExceptionWithCode(ErrorCode.MOD_NOT_ORIGINAL_AUTHOR));
    instance.processUploadedMod(uploadedFile, uploader);
}
Also used : Path(java.nio.file.Path) Player(com.faforever.api.data.domain.Player) Mod(com.faforever.api.data.domain.Mod) Test(org.junit.Test)

Aggregations

Mod (com.faforever.api.data.domain.Mod)4 Path (java.nio.file.Path)3 ModVersion (com.faforever.api.data.domain.ModVersion)2 Player (com.faforever.api.data.domain.Player)2 Test (org.junit.Test)2 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 SneakyThrows (lombok.SneakyThrows)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Example (org.springframework.data.domain.Example)1 Transactional (org.springframework.transaction.annotation.Transactional)1