Commit fe867e9f authored by Matija Obreza's avatar Matija Obreza
Browse files

Properly list subdirectories

parent ad1768c4
......@@ -64,7 +64,7 @@ public interface RepositoryFilePersistence extends JpaRepository<RepositoryFile,
* @param prefix the prefix
* @return the list of paths
*/
@Query("select distinct(rf.path) from RepositoryFile rf where rf.path like ?1% and locate('/', trim(both '/' from substring(rf.path, length(?1), 200))) = 0")
@Query("select distinct(rf.path) from RepositoryFile rf where rf.path like ?1%")
List<String> listDistinctPaths(String prefix);
/**
......
......@@ -503,7 +503,16 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
String p = path.normalize().toAbsolutePath().toString();
PathValidator.checkValidPath(p);
LOG.trace("Is directory path={}", p);
return repositoryFilePersistence.countByPath(p) > 0;
if (repositoryFilePersistence.countByPath(p) > 0) {
// Exact match
LOG.trace("Yes, hasPath {}", p);
return true;
} else {
// Any files within the specified parent
List<String> x = repositoryFilePersistence.listDistinctPaths(p);
LOG.trace("Testing hasPath within {}: {}", p, x);
return x.size() > 0;
}
}
/*
......
......@@ -158,7 +158,11 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
all.addAll(repositoryService.getFiles(currentPath).stream().map(rf -> file(rf)).collect(Collectors.toList()));
try {
all.addAll(repositoryService.listPaths(currentPath).stream().map(path -> directory(path, session)).collect(Collectors.toList()));
all.addAll(repositoryService.listPaths(currentPath).stream().map(path -> {
Path foo = Paths.get(path.substring(currentPath.length()));
LOG.trace("Sub cp=" + currentPath + " p=" + path + " x=" + foo.getName(0));
return foo.getName(0).toString();
}).distinct().map(path -> directory(path, session)).collect(Collectors.toList()));
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error listing paths for {}: {}", currentPath, e.getMessage());
}
......@@ -215,7 +219,7 @@ public class RepositoryFileSystemFactory implements FileSystemFactory, Initializ
@Override
public FtpFile getFile(String file) throws FtpException {
LOG.debug("getFile file={} for user={}", file, username);
Path path = Paths.get(cwd.getAbsolutePath(), file).normalize();
Path path = file.startsWith("/") ? Paths.get(file).normalize() : Paths.get(cwd.getAbsolutePath(), file).normalize();
LOG.trace("Resolved normalized={}", path.toString());
LOG.trace("Temporary dirs: {}", temporaryDirs);
......
......@@ -88,13 +88,12 @@ public class FtpServerTest {
}
};
ftp.setTrustManager(trustAllCerts);
return ftp;
}
/**
* Uncomment the @Ignore and run this test to connect with other clients for testing. Press enter in the console to
* stop the server.
* Uncomment the @Ignore and run this test to connect with other clients for testing. Press enter in the console to stop the server.
*
* @throws IOException
*/
......@@ -299,6 +298,40 @@ public class FtpServerTest {
}
}
@Test
public void folderListing() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
assertThat(ftp.getDataConnectionMode(), is(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE));
ftp.login(username, password);
assertThat("Login failed", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.setFileTransferMode(FTPClient.ASCII_FILE_TYPE);
try (InputStream local = new ByteArrayInputStream(TEST_FILE_CONTENTS.getBytes())) {
assertThat(ftp.storeFile("/a/b/c/d/e/f/g", local), is(true));
}
for (String dir : new String[] { "/", "/a", "/a/b", "/a/b/c", "/a/b/c/d", "/a/b/c/d/e", "/a/b/c/d/e", "/a/b/c/d/e/f" }) {
assertThat(ftp.changeWorkingDirectory(dir), is(true));
debugListFiles(ftp);
char nextDir = dir == "/" ? 'a' : (char) (dir.substring(dir.length() - 1, dir.length()).charAt(0) + 1);
// System.err.println("Curr=" + dir + " next=" + nextDir);
assertThat(Arrays.stream(ftp.listFiles()).map(file -> file.getName()).collect(Collectors.toList()), hasItems("" + nextDir));
}
assertThat(ftp.deleteFile("/a/b/c/d/e/f/g"), is(true));
} finally {
ftp.disconnect();
}
}
@Test
public void simpleRewriteTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment