mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Handle special characters in file names
This commit is contained in:
@@ -4,6 +4,7 @@ import sonia.scm.repository.NamespaceAndName;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
class ResourceLinks {
|
||||
|
||||
@@ -16,7 +17,11 @@ class ResourceLinks {
|
||||
|
||||
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
|
||||
private static String addPath(String sourceWithPath, String path) {
|
||||
return URI.create(sourceWithPath).resolve(path).toASCIIString();
|
||||
try {
|
||||
return new URI(sourceWithPath).resolve(new URI(null, null, path, null)).toASCIIString();
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
GroupLinks group() {
|
||||
|
||||
@@ -173,6 +173,30 @@ public class ResourceLinksTest {
|
||||
assertEquals(BASE_URL + ConfigResource.CONFIG_PATH_V2, url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleSpacesInPaths() {
|
||||
String url = resourceLinks.source().content("space", "name", "rev", "name with spaces");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name%20with%20spaces", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleBackslashInPaths() {
|
||||
String url = resourceLinks.source().content("space", "name", "rev", "name_with_\\");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name_with_%5C", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNewLineInPaths() {
|
||||
String url = resourceLinks.source().content("space", "name", "rev", "name_with\nnew_line");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/name_with%0Anew_line", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldKeepSlashesInInPaths() {
|
||||
String url = resourceLinks.source().content("space", "name", "rev", "some/dir/somewhere");
|
||||
assertEquals(BASE_URL + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/name/content/rev/some/dir/somewhere", url);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initUriInfo() {
|
||||
initMocks(this);
|
||||
|
||||
Reference in New Issue
Block a user