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