From b8b146a4b16327719074875b52f253e61de71855 Mon Sep 17 00:00:00 2001 From: Martin Spielmann Date: Sat, 19 Aug 2017 15:29:46 +0200 Subject: [PATCH] Update folder list method name, added recursion feature --- .../nextcloud/api/NextcloudConnector.java | 53 ++++++++++++++----- .../nextcloud/api/webdav/Folders.java | 41 +++++++++++--- .../nextcloud/api/NextcloudConnectorTest.java | 42 ++++++++++++++- 3 files changed, 114 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java b/src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java index 6fd746f..c632142 100644 --- a/src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java +++ b/src/main/java/org/aarboard/nextcloud/api/NextcloudConnector.java @@ -535,12 +535,37 @@ public CompletableFuture getGroupsAsync(String search, int limi * * @param path path of the folder * @return found subfolders + * @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. */ + @Deprecated public List getFolders(String path) { return fd.getFolders(path); } + /** + * Get all subfolders of the specified path + * + * @param path path of the folder + * @return found subfolders + */ + public List listFolderContent(String path) + { + return fd.listFolderContent(path); + } + + /** + * List all file names and subfolders of the specified path traversing into subfolders to the given depth. + * + * @param path path of the folder + * @param depth depth of recursion while listing folder contents + * @return found file names and subfolders + */ + public List listFolderContent(String path, int depth) + { + return fd.listFolderContent(path, depth); + } + /** * Checks if the folder at the specified path exists * @@ -595,16 +620,16 @@ public Share doShare( } /** - * Shares the specified path with the provided parameters asynchronously - * - * @param path path to the file/folder which should be shared - * @param shareType 0 = user; 1 = group; 3 = public link; 6 = federated cloud share - * @param shareWithUserOrGroupId user / group id with which the file should be shared - * @param publicUpload allow public upload to a public shared folder (true/false) - * @param password password to protect public link Share with - * @param permissions 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1) - * @return a CompletableFuture containing the result of the operation - */ + * Shares the specified path with the provided parameters asynchronously + * + * @param path path to the file/folder which should be shared + * @param shareType 0 = user; 1 = group; 3 = public link; 6 = federated cloud share + * @param shareWithUserOrGroupId user / group id with which the file should be shared + * @param publicUpload allow public upload to a public shared folder (true/false) + * @param password password to protect public link Share with + * @param permissions 1 = read; 2 = update; 4 = create; 8 = delete; 16 = share; 31 = all (default: 31, for public shares: 1) + * @return a CompletableFuture containing the result of the operation + */ public CompletableFuture doShareAsync( String path, ShareType shareType, @@ -659,10 +684,10 @@ public CompletableFuture getSharesAsync() } /** Uploads a file at the specified path with the data from the InputStream - * - * @param inputStream InputStream of the file which should be uploaded - * @param remotePath path where the file should be uploaded to - */ + * + * @param inputStream InputStream of the file which should be uploaded + * @param remotePath path where the file should be uploaded to + */ public void uploadFile(InputStream inputStream, String remotePath) { fl.uploadFile(inputStream, remotePath); diff --git a/src/main/java/org/aarboard/nextcloud/api/webdav/Folders.java b/src/main/java/org/aarboard/nextcloud/api/webdav/Folders.java index 7190005..ebd830f 100644 --- a/src/main/java/org/aarboard/nextcloud/api/webdav/Folders.java +++ b/src/main/java/org/aarboard/nextcloud/api/webdav/Folders.java @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2017 a.schild * * This program is free software: you can redistribute it and/or modify @@ -16,16 +16,17 @@ */ package org.aarboard.nextcloud.api.webdav; -import com.github.sardine.DavResource; -import com.github.sardine.Sardine; -import com.github.sardine.SardineFactory; - import java.io.IOException; import java.util.LinkedList; import java.util.List; + import org.aarboard.nextcloud.api.ServerConfig; import org.aarboard.nextcloud.api.exception.NextcloudApiException; +import com.github.sardine.DavResource; +import com.github.sardine.Sardine; +import com.github.sardine.SardineFactory; + /** * * @author a.schild @@ -45,17 +46,43 @@ public Folders(ServerConfig _serverConfig) { * * @param rootPath path of the folder * @return found subfolders + * + * @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. */ + @Deprecated public List getFolders(String rootPath) { - String path= (_serverConfig.isUseHTTPS() ? "https" : "http") +"://"+_serverConfig.getServerName()+"/"+WEB_DAV_BASE_PATH+rootPath ; + return listFolderContent(rootPath); + } + + /** + * List all file names and subfolders of the specified path + * + * @param path path of the folder + * @return found file names and subfolders + */ + public List listFolderContent(String path) + { + return listFolderContent(path, 1); + } + + /** + * List all file names and subfolders of the specified path traversing into subfolders to the given depth. + * + * @param path path of the folder + * @param depth depth of recursion while listing folder contents + * @return found file names and subfolders + */ + public List listFolderContent(String path, int depth) + { + String url = (_serverConfig.isUseHTTPS() ? "https" : "http") +"://"+_serverConfig.getServerName()+"/"+WEB_DAV_BASE_PATH+path ; List retVal= new LinkedList<>(); Sardine sardine = SardineFactory.begin(); sardine.setCredentials(_serverConfig.getUserName(), _serverConfig.getPassword()); List resources; try { - resources = sardine.list(path); + resources = sardine.list(url, depth); } catch (IOException e) { throw new NextcloudApiException(e); } diff --git a/src/test/java/org/aarboard/nextcloud/api/NextcloudConnectorTest.java b/src/test/java/org/aarboard/nextcloud/api/NextcloudConnectorTest.java index 8537293..4866f72 100644 --- a/src/test/java/org/aarboard/nextcloud/api/NextcloudConnectorTest.java +++ b/src/test/java/org/aarboard/nextcloud/api/NextcloudConnectorTest.java @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2017 a.schild * * This program is free software: you can redistribute it and/or modify @@ -354,4 +354,44 @@ public void t27_testRemoveFile() { _nc.removeFile(TESTFILE); } } + + @Test + public void t28_testList() { + System.out.println("list"); + + if (_nc != null) + { + //prepare + _nc.createFolder(TEST_FOLDER); + + String rootPath = ""; + List result = _nc.listFolderContent(rootPath); + + //cleanup + _nc.deleteFolder(TEST_FOLDER); + + assertNotNull(result); + assertTrue(result.contains(TEST_FOLDER)); + } + } + + @Test + public void t29_testListRecursive() { + System.out.println("list recursive"); + if (_nc != null) + { + //prepare + _nc.createFolder(TEST_FOLDER); + _nc.createFolder(TEST_FOLDER+"/"+TEST_FOLDER+"_sub"); + + String rootPath = ""; + List result = _nc.listFolderContent(rootPath, 2); + + //cleanup + _nc.deleteFolder(TEST_FOLDER); + + assertNotNull(result); + assertTrue(result.contains(TEST_FOLDER+"_sub")); + } + } }