fix(path): remove trailing dots from path components for Windows compatibility (#1786)

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-07 23:17:27 +01:00
committed by GitHub
parent 5e5bb41585
commit afe9c59380
2 changed files with 46 additions and 0 deletions

View File

@@ -266,6 +266,9 @@ public class PathPatternResolver {
if (component.getBytes(StandardCharsets.UTF_8).length > MAX_FILESYSTEM_COMPONENT_BYTES) {
component = truncatePathComponent(component, MAX_FILESYSTEM_COMPONENT_BYTES);
}
while (component.endsWith(".")) {
component = component.substring(0, component.length() - 1);
}
}
if (i > 0) result.append("/");

View File

@@ -467,4 +467,47 @@ class PathPatternResolverTest {
int byteLen = result.getBytes(StandardCharsets.UTF_8).length;
assertTrue(byteLen <= 245, "Total filename bytes " + byteLen + " should be <= 245");
}
@Test
@DisplayName("Should remove trailing dots from path components for Windows compatibility")
void testResolvePattern_removesTrailingDots() {
BookMetadata metadata = BookMetadata.builder()
.title("Book Title")
.authors(Set.of("Author Name Jr."))
.build();
// Pattern: {authors}/{title}
String result = PathPatternResolver.resolvePattern(metadata, "{authors}/{title}", "original.pdf");
// Expected: Author Name Jr/Book Title.pdf
// Windows does not allow folder names ending in '.'
// So "Author Name Jr." should become "Author Name Jr"
String[] components = result.split("/");
assertTrue(components.length >= 1);
String authorDir = components[0];
assertFalse(authorDir.endsWith("."), "Directory name should not end with a dot: " + authorDir);
assertTrue(authorDir.equals("Author Name Jr"), "Expected 'Author Name Jr' but got '" + authorDir + "'");
}
@Test
@DisplayName("Should remove trailing dots from multiple path components")
void testResolvePattern_removesTrailingDotsFromMultipleComponents() {
BookMetadata metadata = BookMetadata.builder()
.title("Book Title.")
.seriesName("Series.")
.authors(Set.of("Author."))
.build();
String result = PathPatternResolver.resolvePattern(metadata, "{authors}/{series}/{title}", "original.pdf");
String[] components = result.split("/");
for (int i = 0; i < components.length - 1; i++) { // Check directories
assertFalse(components[i].endsWith("."), "Component " + i + " should not end with dot: " + components[i]);
}
assertTrue(components[0].equals("Author"));
assertTrue(components[1].equals("Series"));
}
}