-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize attachment filenames and bump dependency versions
(cherry picked from commit 1f5cd2d)
- Loading branch information
1 parent
a1221a6
commit daac97f
Showing
13 changed files
with
56,892 additions
and
29 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package util; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class FileNameSanitizer { | ||
|
||
private static Pattern ILLEGAL_CHARACTERS; | ||
private static final Set<String> RESERVED_NAMES = new HashSet<>(); | ||
|
||
static { | ||
setOsName(System.getProperty("os.name").toLowerCase()); | ||
} | ||
|
||
@VisibleForTesting | ||
public static void setOsName(String osName) { | ||
RESERVED_NAMES.clear(); | ||
if (osName.toLowerCase().contains("win")) { | ||
ILLEGAL_CHARACTERS = Pattern.compile("[<>:\"/\\\\|?*]"); | ||
String[] reservedNames = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" | ||
}; | ||
RESERVED_NAMES.addAll(Arrays.asList(reservedNames)); | ||
} else { | ||
ILLEGAL_CHARACTERS = Pattern.compile("[/]"); | ||
} | ||
} | ||
|
||
public static String sanitizeFileName(String fileName, char replacement) { | ||
if (fileName == null || fileName.isEmpty()) { | ||
return fileName; | ||
} | ||
|
||
String sanitizedFileName = ILLEGAL_CHARACTERS.matcher(fileName).replaceAll(Matcher.quoteReplacement(String.valueOf(replacement))); | ||
|
||
if (RESERVED_NAMES.contains(sanitizedFileName.toUpperCase())) { | ||
sanitizedFileName = "_" + sanitizedFileName.toUpperCase() + "_"; | ||
} | ||
|
||
if (sanitizedFileName.isEmpty()) { | ||
throw new IllegalArgumentException("Sanitized file name cannot be empty"); | ||
} | ||
|
||
return sanitizedFileName; | ||
} | ||
} |
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 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,56 @@ | ||
package util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class FileNameSanitizerTest { | ||
|
||
@Test | ||
public void sanitizeFileName_returnsNullForNull_Windows() { | ||
FileNameSanitizer.setOsName("Windows 10"); | ||
|
||
String originalFileName = null; | ||
String sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo(null)); | ||
} | ||
|
||
@Test | ||
public void sanitizeFileName_shouldReplaceIllegalCharacters_Windows() { | ||
FileNameSanitizer.setOsName("Windows 10"); | ||
|
||
String originalFileName = "illegal:/\\*?\"<>|.txt"; | ||
String sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo("illegal_________.txt")); | ||
} | ||
|
||
@Test | ||
public void sanitizeFileName_shouldReplaceIllegalCharacters_Unix() { | ||
FileNameSanitizer.setOsName("Linux"); | ||
|
||
String originalFileName = "illegal:/\\*?\"<>|.txt"; | ||
String sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo("illegal:_\\*?\"<>|.txt")); | ||
} | ||
|
||
@Test | ||
public void sanitizeFileName_shouldHandleReservedNames_Windows() { | ||
FileNameSanitizer.setOsName("Windows 10"); | ||
|
||
String originalFileName = "CON"; | ||
String sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo("_CON_")); | ||
|
||
originalFileName = "prn"; | ||
sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo("_PRN_")); | ||
} | ||
|
||
@Test | ||
public void sanitizeFileName_shouldNotChangeValidFileName() { | ||
String originalFileName = "valid_filename.txt"; | ||
String sanitizedFileName = FileNameSanitizer.sanitizeFileName(originalFileName, '_'); | ||
assertThat(sanitizedFileName, equalTo("valid_filename.txt")); | ||
} | ||
} |
Oops, something went wrong.