Skip to content

Commit

Permalink
[Backport 4.2.x] URL Decode filename for resources uploaded by url (#…
Browse files Browse the repository at this point in the history
…8641)

* Update getFilenameFromUrl to url decode the file name

* Remove unneeded import

* Add unit test

* fix decode

---------

Co-authored-by: tylerjmchugh <[email protected]>
Co-authored-by: Ian Allen <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2025
1 parent 1302eb2 commit e06c622
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -170,6 +173,11 @@ protected String getFilenameFromUrl(final URL fileUrl) {
if (fileName.contains("?")) {
fileName = fileName.substring(0, fileName.indexOf("?"));
}
try {
fileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return fileName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -100,4 +101,21 @@ public void testGetResourceDescriptionNonExistingUuid() throws Exception {
filesystemStore.getResourceDescription(context, "nonExistingUuid",
MetadataResourceVisibility.PUBLIC, "existingResource.jpg", true);
}

@Test
public void testGetFilenameFromUrl() throws Exception {
FilesystemStore filesystemStore = new FilesystemStore();

String fileName = filesystemStore.getFilenameFromUrl(new URL("http://mydomain.com/filename.txt"));
assertEquals("filename.txt", fileName);

fileName = filesystemStore.getFilenameFromUrl(new URL("http://mydomain.com/filename%20with%20spaces.txt"));
assertEquals("filename with spaces.txt", fileName);

fileName = filesystemStore.getFilenameFromUrl(new URL("http://mydomain.com/filename.txt?param=value"));
assertEquals("filename.txt", fileName);

fileName = filesystemStore.getFilenameFromUrl(new URL("http://mydomain.com/filename%20with%20spaces.txt?param=value"));
assertEquals("filename with spaces.txt", fileName);
}
}

0 comments on commit e06c622

Please sign in to comment.