Skip to content

Commit b8b146a

Browse files
Update folder list method name, added recursion feature
1 parent d5e9900 commit b8b146a

File tree

3 files changed

+114
-22
lines changed

3 files changed

+114
-22
lines changed

src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,37 @@ public CompletableFuture<GroupsXMLAnswer> getGroupsAsync(String search, int limi
535535
*
536536
* @param path path of the folder
537537
* @return found subfolders
538+
* @deprecated The methods naming is somehow misleading, as it lists all resources (subfolders and files) within the given {@code rootPath}. Please use {@link #listFolderContent(String)} instead.
538539
*/
540+
@Deprecated
539541
public List<String> getFolders(String path)
540542
{
541543
return fd.getFolders(path);
542544
}
543545

546+
/**
547+
* Get all subfolders of the specified path
548+
*
549+
* @param path path of the folder
550+
* @return found subfolders
551+
*/
552+
public List<String> listFolderContent(String path)
553+
{
554+
return fd.listFolderContent(path);
555+
}
556+
557+
/**
558+
* List all file names and subfolders of the specified path traversing into subfolders to the given depth.
559+
*
560+
* @param path path of the folder
561+
* @param depth depth of recursion while listing folder contents
562+
* @return found file names and subfolders
563+
*/
564+
public List<String> listFolderContent(String path, int depth)
565+
{
566+
return fd.listFolderContent(path, depth);
567+
}
568+
544569
/**
545570
* Checks if the folder at the specified path exists
546571
*
@@ -595,16 +620,16 @@ public Share doShare(
595620
}
596621

597622
/**
598-
* Shares the specified path with the provided parameters asynchronously
599-
*
600-
* @param path path to the file/folder which should be shared
601-
* @param shareType 0 = user; 1 = group; 3 = public link; 6 = federated cloud share
602-
* @param shareWithUserOrGroupId user / group id with which the file should be shared
603-
* @param publicUpload allow public upload to a public shared folder (true/false)
604-
* @param password password to protect public link Share with
605-
* @param permissions 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1)
606-
* @return a CompletableFuture containing the result of the operation
607-
*/
623+
* Shares the specified path with the provided parameters asynchronously
624+
*
625+
* @param path path to the file/folder which should be shared
626+
* @param shareType 0 = user; 1 = group; 3 = public link; 6 = federated cloud share
627+
* @param shareWithUserOrGroupId user / group id with which the file should be shared
628+
* @param publicUpload allow public upload to a public shared folder (true/false)
629+
* @param password password to protect public link Share with
630+
* @param permissions 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1)
631+
* @return a CompletableFuture containing the result of the operation
632+
*/
608633
public CompletableFuture<SingleShareXMLAnswer> doShareAsync(
609634
String path,
610635
ShareType shareType,
@@ -659,10 +684,10 @@ public CompletableFuture<SharesXMLAnswer> getSharesAsync()
659684
}
660685

661686
/** Uploads a file at the specified path with the data from the InputStream
662-
*
663-
* @param inputStream InputStream of the file which should be uploaded
664-
* @param remotePath path where the file should be uploaded to
665-
*/
687+
*
688+
* @param inputStream InputStream of the file which should be uploaded
689+
* @param remotePath path where the file should be uploaded to
690+
*/
666691
public void uploadFile(InputStream inputStream, String remotePath)
667692
{
668693
fl.uploadFile(inputStream, remotePath);

src/main/java/org/aarboard/nextcloud/api/webdav/Folders.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (C) 2017 a.schild
33
*
44
* This program is free software: you can redistribute it and/or modify
@@ -16,16 +16,17 @@
1616
*/
1717
package org.aarboard.nextcloud.api.webdav;
1818

19-
import com.github.sardine.DavResource;
20-
import com.github.sardine.Sardine;
21-
import com.github.sardine.SardineFactory;
22-
2319
import java.io.IOException;
2420
import java.util.LinkedList;
2521
import java.util.List;
22+
2623
import org.aarboard.nextcloud.api.ServerConfig;
2724
import org.aarboard.nextcloud.api.exception.NextcloudApiException;
2825

26+
import com.github.sardine.DavResource;
27+
import com.github.sardine.Sardine;
28+
import com.github.sardine.SardineFactory;
29+
2930
/**
3031
*
3132
* @author a.schild
@@ -45,17 +46,43 @@ public Folders(ServerConfig _serverConfig) {
4546
*
4647
* @param rootPath path of the folder
4748
* @return found subfolders
49+
*
50+
* @deprecated The methods naming is somehow misleading, as it lists all resources (subfolders and files) within the given {@code rootPath}. Please use {@link #listFolderContent(String)} instead.
4851
*/
52+
@Deprecated
4953
public List<String> getFolders(String rootPath)
5054
{
51-
String path= (_serverConfig.isUseHTTPS() ? "https" : "http") +"://"+_serverConfig.getServerName()+"/"+WEB_DAV_BASE_PATH+rootPath ;
55+
return listFolderContent(rootPath);
56+
}
57+
58+
/**
59+
* List all file names and subfolders of the specified path
60+
*
61+
* @param path path of the folder
62+
* @return found file names and subfolders
63+
*/
64+
public List<String> listFolderContent(String path)
65+
{
66+
return listFolderContent(path, 1);
67+
}
68+
69+
/**
70+
* List all file names and subfolders of the specified path traversing into subfolders to the given depth.
71+
*
72+
* @param path path of the folder
73+
* @param depth depth of recursion while listing folder contents
74+
* @return found file names and subfolders
75+
*/
76+
public List<String> listFolderContent(String path, int depth)
77+
{
78+
String url = (_serverConfig.isUseHTTPS() ? "https" : "http") +"://"+_serverConfig.getServerName()+"/"+WEB_DAV_BASE_PATH+path ;
5279

5380
List<String> retVal= new LinkedList<>();
5481
Sardine sardine = SardineFactory.begin();
5582
sardine.setCredentials(_serverConfig.getUserName(), _serverConfig.getPassword());
5683
List<DavResource> resources;
5784
try {
58-
resources = sardine.list(path);
85+
resources = sardine.list(url, depth);
5986
} catch (IOException e) {
6087
throw new NextcloudApiException(e);
6188
}

src/test/java/org/aarboard/nextcloud/api/NextcloudConnectorTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (C) 2017 a.schild
33
*
44
* This program is free software: you can redistribute it and/or modify
@@ -354,4 +354,44 @@ public void t27_testRemoveFile() {
354354
_nc.removeFile(TESTFILE);
355355
}
356356
}
357+
358+
@Test
359+
public void t28_testList() {
360+
System.out.println("list");
361+
362+
if (_nc != null)
363+
{
364+
//prepare
365+
_nc.createFolder(TEST_FOLDER);
366+
367+
String rootPath = "";
368+
List<String> result = _nc.listFolderContent(rootPath);
369+
370+
//cleanup
371+
_nc.deleteFolder(TEST_FOLDER);
372+
373+
assertNotNull(result);
374+
assertTrue(result.contains(TEST_FOLDER));
375+
}
376+
}
377+
378+
@Test
379+
public void t29_testListRecursive() {
380+
System.out.println("list recursive");
381+
if (_nc != null)
382+
{
383+
//prepare
384+
_nc.createFolder(TEST_FOLDER);
385+
_nc.createFolder(TEST_FOLDER+"/"+TEST_FOLDER+"_sub");
386+
387+
String rootPath = "";
388+
List<String> result = _nc.listFolderContent(rootPath, 2);
389+
390+
//cleanup
391+
_nc.deleteFolder(TEST_FOLDER);
392+
393+
assertNotNull(result);
394+
assertTrue(result.contains(TEST_FOLDER+"_sub"));
395+
}
396+
}
357397
}

0 commit comments

Comments
 (0)