Process URL path for filename extraction if URI does not expose path
Closes gh-31718
This commit is contained in:
parent
df00aafdff
commit
f3b1f37000
|
@ -332,13 +332,15 @@ public class UrlResource extends AbstractFileResolvingResource {
|
|||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -302,11 +302,28 @@ class ResourceTests {
|
|||
|
||||
@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";
|
||||
|
|
Loading…
Reference in New Issue