-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More validation of maps on upload, reworked file name sanitization
- Loading branch information
1 parent
b5c59a8
commit f2c68c2
Showing
9 changed files
with
100 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.faforever.api.utils; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.nio.file.Paths; | ||
import java.text.Normalizer; | ||
import java.text.Normalizer.Form; | ||
import java.util.Locale; | ||
|
||
public final class NameUtil { | ||
private NameUtil() { | ||
// Utility class | ||
} | ||
|
||
/** | ||
* Returns true if the string only consists of all printable ASCII characters (see ASCII table from SP to ~ [SP being | ||
* space]) | ||
*/ | ||
public static boolean isPrintableAsciiString(String name) { | ||
return !name.matches(".*[^\\x20-\\x7E]+.*"); | ||
} | ||
|
||
public static String normalizeFileName(String originalFileName) { | ||
return StringUtils.strip( | ||
Normalizer.normalize( | ||
Paths.get(originalFileName | ||
.replaceAll("[/\\\\ :*?<>|\"]", "_") | ||
.toLowerCase(Locale.US)) | ||
.getFileName() | ||
.toString(), | ||
Form.NFKC), | ||
"_"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
src/test/java/com/faforever/api/utils/FileNameUtilTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.faforever.api.utils; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class NameUtilTest { | ||
@Test | ||
public void testIsPrintableAsciiString() { | ||
final boolean positiveResult = NameUtil.isPrintableAsciiString(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); | ||
assertThat(positiveResult, is(true)); | ||
|
||
final boolean negativeResult = NameUtil.isPrintableAsciiString("Hello this is ä test."); | ||
assertThat(negativeResult, is(false)); | ||
} | ||
|
||
@Test | ||
public void generateFileName() throws Exception { | ||
final String fileName = NameUtil.normalizeFileName(" :*?<>|\"{}:[]la/ \\la??"); | ||
assertThat(fileName, is("{}_[]la___la")); | ||
} | ||
} |