Skip to content

Commit

Permalink
Fixes #207
Browse files Browse the repository at this point in the history
  • Loading branch information
1-alex98 committed Jul 13, 2018
1 parent 06def6d commit c395468
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/faforever/api/map/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void uploadMap(byte[] mapData, String mapFilename, Player author, boolean isRank
Assert.notNull(author, "'author' must not be null");
Assert.isTrue(mapData.length > 0, "'mapData' must not be empty");

mapFilename = mapFilename.replaceAll("[^\\w.\\-]", ""); //replacing all characters that are illegal in filenames

MapUploadData progressData = new MapUploadData()
.setBaseDir(contentService.createTempDir())
.setUploadFileName(mapFilename)
Expand Down Expand Up @@ -236,7 +238,7 @@ private void updateMapEntities(MapUploadData progressData) {
if (map == null) {
map = new Map();
}
map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString())
map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString().replaceAll("[^\\w.\\-\\ ]", ""))
.setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring())
.setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1)
.get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring())
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/faforever/api/map/MapServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,42 @@ public void mapWithMoreRootFoldersInZip() throws IOException {
}
}

@Test
public void positiveUploadTestWithProblematicCharacters() throws IOException {
String zipFilename = "scmp_037_no_ascii.zip";
when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.empty());
try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
byte[] mapData = ByteStreams.toByteArray(inputStream);

Path tmpDir = temporaryDirectory.getRoot().toPath();
instance.uploadMap(mapData, zipFilename, author, true);

ArgumentCaptor<com.faforever.api.data.domain.Map> mapCaptor = ArgumentCaptor.forClass(com.faforever.api.data.domain.Map.class);
verify(mapRepository, Mockito.times(1)).save(mapCaptor.capture());
assertEquals("No_Ascii", mapCaptor.getValue().getDisplayName());
assertEquals("skirmish", mapCaptor.getValue().getMapType());
assertEquals("FFA", mapCaptor.getValue().getBattleType());
assertEquals(1, mapCaptor.getValue().getVersions().size());

MapVersion mapVersion = mapCaptor.getValue().getVersions().get(0);
assertEquals("The thick, brackish water clings to everything, staining anything it touches. If it weren't for this planet's proximity to the Quarantine Zone, no one would ever bother coming here.", mapVersion.getDescription());
assertEquals(1, mapVersion.getVersion());
assertEquals(256, mapVersion.getHeight());
assertEquals(256, mapVersion.getWidth());
assertEquals(3, mapVersion.getMaxPlayers());
assertEquals("maps/no_ascii.v0001.zip", mapVersion.getFilename());

assertFalse(Files.exists(tmpDir));

Path generatedFile = finalDirectory.getRoot().toPath().resolve("no_ascii.v0001.zip");
assertTrue(Files.exists(generatedFile));

assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathLarge().resolve("no_ascii.v0001.png")));
assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathSmall().resolve("no_ascii.v0001.png")));

}
}

@Test
public void positiveUploadTest() throws IOException {
String zipFilename = "scmp_037.zip";
Expand Down
Binary file added src/test/resources/maps/scmp_037_no_ascii.zip
Binary file not shown.

0 comments on commit c395468

Please sign in to comment.