Skip to content

Commit

Permalink
close #391 Split by text: File name too long on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ediweissmann committed Feb 8, 2021
1 parent dbeb866 commit b8f8a26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ public static String toSafeFilename(String input) {
* Strips all but characters that are known to be safe: alphanumerics for now.
*/
public static String toStrictFilename(String input) {
int maxLength = 251; // leave room for extension ".pdf"
String safe = defaultIfBlank(input, "").replaceAll("[^A-Za-z0-9_ .-]", "");
if (safe.length() > 255) {
safe = safe.substring(0, 255);
if (safe.length() > maxLength) {
safe = safe.substring(0, maxLength);
}
return safe;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,20 @@ public void safeFilenameWhitespaces() {
}

@Test
public void testStrictFilename() {
public void testStrictFilename_specialChars() throws TaskIOException {
assertEquals("1_Invoice0001", IOUtils.toStrictFilename("1_Invoice#0001:*<>/\\"));
assertEquals(StringUtils.repeat('a', 255), IOUtils.toStrictFilename(StringUtils.repeat('a', 256)));
}

@Test
public void testStrictFilename_tooLong() throws TaskIOException {
String tooLong = StringUtils.repeat('a', 256);

// check we can actually create the file, it's not too long
File tmpFile = IOUtils.createTemporaryBufferWithName(IOUtils.toStrictFilename(tooLong) + ".pdf");
assertTrue(tmpFile.exists());
tmpFile.delete();

assertEquals(StringUtils.repeat('a', 251), IOUtils.toStrictFilename(tooLong));
}

@Test
Expand Down

0 comments on commit b8f8a26

Please sign in to comment.