Skip to content

Commit

Permalink
Process URL path for filename extraction if URI does not expose path
Browse files Browse the repository at this point in the history
Closes gh-31718
  • Loading branch information
jhoeller committed Nov 29, 2023
1 parent df00aaf commit f3b1f37
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Expand Up @@ -332,13 +332,15 @@ protected URL createRelativeURL(String relativePath) throws MalformedURLExceptio
@Nullable
public String getFilename() {
if (this.uri != null) {
// URI path is decoded and has standard separators
return StringUtils.getFilename(this.uri.getPath());
}
else {
String filename = StringUtils.getFilename(StringUtils.cleanPath(this.url.getPath()));
return (filename != null ? URLDecoder.decode(filename, StandardCharsets.UTF_8) : null);
String path = this.uri.getPath();
if (path != null) {
// Prefer URI path: decoded and has standard separators
return StringUtils.getFilename(this.uri.getPath());
}
}
// Otherwise, process URL path
String filename = StringUtils.getFilename(StringUtils.cleanPath(this.url.getPath()));
return (filename != null ? URLDecoder.decode(filename, StandardCharsets.UTF_8) : null);
}

/**
Expand Down
Expand Up @@ -302,11 +302,28 @@ void sameResourceWithRelativePathIsEqual() throws Exception {

@Test
void filenameIsExtractedFromFilePath() throws Exception {
assertThat(new UrlResource("file:test?argh").getFilename()).isEqualTo("test");
assertThat(new UrlResource("file:/test?argh").getFilename()).isEqualTo("test");
assertThat(new UrlResource("file:test.txt?argh").getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource("file:/test.txt?argh").getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource("file:/dir/test?argh").getFilename()).isEqualTo("test");
assertThat(new UrlResource("file:/dir/test.txt?argh").getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource("file:\\dir\\test.txt?argh").getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource("file:\\dir/test.txt?argh").getFilename()).isEqualTo("test.txt");
}

@Test
void filenameIsExtractedFromURL() throws Exception {
assertThat(new UrlResource(new URL("file:test?argh")).getFilename()).isEqualTo("test");
assertThat(new UrlResource(new URL("file:/test?argh")).getFilename()).isEqualTo("test");
assertThat(new UrlResource(new URL("file:test.txt?argh")).getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource(new URL("file:/test.txt?argh")).getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource(new URL("file:/dir/test?argh")).getFilename()).isEqualTo("test");
assertThat(new UrlResource(new URL("file:/dir/test.txt?argh")).getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource(new URL("file:\\dir\\test.txt?argh")).getFilename()).isEqualTo("test.txt");
assertThat(new UrlResource(new URL("file:\\dir/test.txt?argh")).getFilename()).isEqualTo("test.txt");
}

@Test
void filenameContainingHashTagIsExtractedFromFilePathUnencoded() throws Exception {
String unencodedPath = "/dir/test#1.txt";
Expand Down

0 comments on commit f3b1f37

Please sign in to comment.