mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-30 18:07:38 +00:00 
			
		
		
		
	Merge pull request #533 from owncloud/spaces/list_content
[SPACES] List&Browse Spaces content
This commit is contained in:
		
						commit
						887ea6f2cd
					
				| @ -46,7 +46,8 @@ import java.util.concurrent.TimeUnit | ||||
| class CreateRemoteFolderOperation( | ||||
|     val remotePath: String, | ||||
|     private val createFullPath: Boolean, | ||||
|     private val isChunksFolder: Boolean = false | ||||
|     private val isChunksFolder: Boolean = false, | ||||
|     val spaceWebDavUrl: String? = null, | ||||
| ) : RemoteOperation<Unit>() { | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { | ||||
| @ -67,13 +68,13 @@ class CreateRemoteFolderOperation( | ||||
|         var result: RemoteOperationResult<Unit> | ||||
|         try { | ||||
|             val webDavUri = if (isChunksFolder) { | ||||
|                 client.uploadsWebDavUri | ||||
|                 client.uploadsWebDavUri.toString() | ||||
|             } else { | ||||
|                 client.userFilesWebDavUri | ||||
|                 spaceWebDavUrl ?: client.userFilesWebDavUri.toString() | ||||
|             } | ||||
| 
 | ||||
|             val mkCol = MkColMethod( | ||||
|                 URL(webDavUri.toString() + WebdavUtils.encodePath(remotePath)) | ||||
|                 URL(webDavUri + WebdavUtils.encodePath(remotePath)) | ||||
|             ).apply { | ||||
|                 setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS) | ||||
|                 setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS) | ||||
|  | ||||
| @ -46,7 +46,10 @@ import java.util.concurrent.TimeUnit | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| 
 | ||||
| class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFile>() { | ||||
| class ReadRemoteFileOperation( | ||||
|     val remotePath: String, | ||||
|     val spaceWebDavUrl: String? = null, | ||||
| ) : RemoteOperation<RemoteFile>() { | ||||
| 
 | ||||
|     /** | ||||
|      * Performs the read operation. | ||||
| @ -57,7 +60,7 @@ class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFi | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteFile> { | ||||
|         try { | ||||
|             val propFind = PropfindMethod( | ||||
|                 url = URL("${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)}"), | ||||
|                 url = getFinalWebDavUrl(), | ||||
|                 depth = DEPTH_0, | ||||
|                 propertiesToRequest = DavUtils.allPropset | ||||
|             ).apply { | ||||
| @ -69,10 +72,11 @@ class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFi | ||||
|             Timber.i("Read remote file $remotePath with status ${propFind.statusCode}") | ||||
| 
 | ||||
|             return if (isSuccess(status)) { | ||||
|                 // TODO: Remove that !! | ||||
|                 val remoteFile = RemoteFile.getRemoteFileFromDav( | ||||
|                     propFind.root!!, | ||||
|                     AccountUtils.getUserId(mAccount, mContext), mAccount.name | ||||
|                     davResource = propFind.root!!, | ||||
|                     userId = AccountUtils.getUserId(mAccount, mContext), | ||||
|                     userName = mAccount.name, | ||||
|                     spaceWebDavUrl = spaceWebDavUrl, | ||||
|                 ) | ||||
| 
 | ||||
|                 RemoteOperationResult<RemoteFile>(RemoteOperationResult.ResultCode.OK).apply { | ||||
| @ -88,6 +92,12 @@ class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFi | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun getFinalWebDavUrl(): URL { | ||||
|         val baseWebDavUrl = spaceWebDavUrl ?: client.userFilesWebDavUri.toString() | ||||
| 
 | ||||
|         return URL(baseWebDavUrl + WebdavUtils.encodePath(remotePath)) | ||||
|     } | ||||
| 
 | ||||
|     private fun isSuccess(status: Int) = status.isOneOf(HTTP_MULTI_STATUS, HTTP_OK) | ||||
| 
 | ||||
|     companion object { | ||||
|  | ||||
| @ -48,7 +48,8 @@ import java.net.URL | ||||
|  * @author David González Verdugo | ||||
|  */ | ||||
| class ReadRemoteFolderOperation( | ||||
|     val remotePath: String | ||||
|     val remotePath: String, | ||||
|     val spaceWebDavUrl: String? = null, | ||||
| ) : RemoteOperation<ArrayList<RemoteFile>>() { | ||||
| 
 | ||||
|     /** | ||||
| @ -61,7 +62,7 @@ class ReadRemoteFolderOperation( | ||||
|             PropertyRegistry.register(OCShareTypes.Factory()) | ||||
| 
 | ||||
|             val propfindMethod = PropfindMethod( | ||||
|                 URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath)), | ||||
|                 getFinalWebDavUrl(), | ||||
|                 DavConstants.DEPTH_1, | ||||
|                 DavUtils.allPropset | ||||
|             ) | ||||
| @ -71,12 +72,11 @@ class ReadRemoteFolderOperation( | ||||
|             if (isSuccess(status)) { | ||||
|                 val mFolderAndFiles = ArrayList<RemoteFile>() | ||||
| 
 | ||||
|                 // parse data from remote folder | ||||
|                 // TODO: Remove that !! | ||||
|                 val remoteFolder = RemoteFile.getRemoteFileFromDav( | ||||
|                     davResource = propfindMethod.root!!, | ||||
|                     userId = AccountUtils.getUserId(mAccount, mContext), | ||||
|                     userName = mAccount.name | ||||
|                     userName = mAccount.name, | ||||
|                     spaceWebDavUrl = spaceWebDavUrl, | ||||
|                 ) | ||||
|                 mFolderAndFiles.add(remoteFolder) | ||||
| 
 | ||||
| @ -85,7 +85,8 @@ class ReadRemoteFolderOperation( | ||||
|                     val remoteFile = RemoteFile.getRemoteFileFromDav( | ||||
|                         davResource = resource, | ||||
|                         userId = AccountUtils.getUserId(mAccount, mContext), | ||||
|                         userName = mAccount.name | ||||
|                         userName = mAccount.name, | ||||
|                         spaceWebDavUrl = spaceWebDavUrl, | ||||
|                     ) | ||||
|                     mFolderAndFiles.add(remoteFile) | ||||
|                 } | ||||
| @ -107,5 +108,11 @@ class ReadRemoteFolderOperation( | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private fun getFinalWebDavUrl(): URL { | ||||
|         val baseWebDavUrl = spaceWebDavUrl ?: client.userFilesWebDavUri.toString() | ||||
| 
 | ||||
|         return URL(baseWebDavUrl + WebdavUtils.encodePath(remotePath)) | ||||
|     } | ||||
| 
 | ||||
|     private fun isSuccess(status: Int): Boolean = status.isOneOf(HTTP_OK, HTTP_MULTI_STATUS) | ||||
| } | ||||
|  | ||||
| @ -26,6 +26,7 @@ package com.owncloud.android.lib.resources.files | ||||
| 
 | ||||
| import android.net.Uri | ||||
| import android.os.Parcelable | ||||
| import androidx.annotation.VisibleForTesting | ||||
| import at.bitfire.dav4jvm.PropStat | ||||
| import at.bitfire.dav4jvm.Property | ||||
| import at.bitfire.dav4jvm.Response | ||||
| @ -100,8 +101,13 @@ data class RemoteFile( | ||||
|         const val MIME_DIR = "DIR" | ||||
|         const val MIME_DIR_UNIX = "httpd/unix-directory" | ||||
| 
 | ||||
|         fun getRemoteFileFromDav(davResource: Response, userId: String, userName: String): RemoteFile { | ||||
|             val remotePath = getRemotePathFromUrl(davResource.href, userId) | ||||
|         fun getRemoteFileFromDav( | ||||
|             davResource: Response, | ||||
|             userId: String, | ||||
|             userName: String, | ||||
|             spaceWebDavUrl: String? = null | ||||
|         ): RemoteFile { | ||||
|             val remotePath = getRemotePathFromUrl(davResource.href, userId, spaceWebDavUrl) | ||||
|             val remoteFile = RemoteFile(remotePath = remotePath, owner = userName) | ||||
|             val properties = getPropertiesEvenIfPostProcessing(davResource) | ||||
| 
 | ||||
| @ -164,15 +170,25 @@ data class RemoteFile( | ||||
|          * Retrieves a relative path from a remote file url | ||||
|          * | ||||
|          * | ||||
|          * Example: url:port/remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt | ||||
|          * Example legacy: | ||||
|          * /remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt | ||||
|          * | ||||
|          * Example spaces: | ||||
|          * /dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9/Documents/text.txt => /Documents/text.txt | ||||
|          * | ||||
|          * @param url    remote file url | ||||
|          * @param userId file owner | ||||
|          * @param spaceWebDavUrl custom web dav url for space | ||||
|          * @return remote relative path of the file | ||||
|          */ | ||||
|         private fun getRemotePathFromUrl(url: HttpUrl, userId: String): String { | ||||
|             val davFilesPath = OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId | ||||
|             val absoluteDavPath = Uri.decode(url.encodedPath) | ||||
|         @VisibleForTesting | ||||
|         fun getRemotePathFromUrl( | ||||
|             url: HttpUrl, | ||||
|             userId: String, | ||||
|             spaceWebDavUrl: String? = null, | ||||
|         ): String { | ||||
|             val davFilesPath = spaceWebDavUrl ?: (OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId) | ||||
|             val absoluteDavPath = if (spaceWebDavUrl != null) Uri.decode(url.toString()) else Uri.decode(url.encodedPath) | ||||
|             val pathToOc = absoluteDavPath.split(davFilesPath).first() | ||||
|             return absoluteDavPath.replace(pathToOc + davFilesPath, "") | ||||
|         } | ||||
|  | ||||
| @ -24,7 +24,6 @@ | ||||
| 
 | ||||
| package com.owncloud.android.lib.resources.files | ||||
| 
 | ||||
| import android.net.Uri | ||||
| import com.owncloud.android.lib.common.OwnCloudClient | ||||
| import com.owncloud.android.lib.common.http.HttpConstants.HTTP_NO_CONTENT | ||||
| import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK | ||||
| @ -46,7 +45,8 @@ import java.net.URL | ||||
|  * @author Abel García de Prada | ||||
|  */ | ||||
| open class RemoveRemoteFileOperation( | ||||
|     private val remotePath: String | ||||
|     private val remotePath: String, | ||||
|     val spaceWebDavUrl: String? = null, | ||||
| ) : RemoteOperation<Unit>() { | ||||
| 
 | ||||
|     override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { | ||||
| @ -54,7 +54,7 @@ open class RemoveRemoteFileOperation( | ||||
|         try { | ||||
|             val srcWebDavUri = getSrcWebDavUriForClient(client) | ||||
|             val deleteMethod = DeleteMethod( | ||||
|                 URL(srcWebDavUri.toString() + WebdavUtils.encodePath(remotePath)) | ||||
|                 URL(srcWebDavUri + WebdavUtils.encodePath(remotePath)) | ||||
|             ) | ||||
|             val status = client.executeHttpMethod(deleteMethod) | ||||
| 
 | ||||
| @ -75,7 +75,7 @@ open class RemoveRemoteFileOperation( | ||||
|      * For standard removals, we will use [OwnCloudClient.getUserFilesWebDavUri]. | ||||
|      * In case we need a different source Uri, override this method. | ||||
|      */ | ||||
|     open fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.userFilesWebDavUri | ||||
|     open fun getSrcWebDavUriForClient(client: OwnCloudClient): String = spaceWebDavUrl ?: client.userFilesWebDavUri.toString() | ||||
| 
 | ||||
|     private fun isSuccess(status: Int) = status.isOneOf(HTTP_OK, HTTP_NO_CONTENT) | ||||
| } | ||||
|  | ||||
| @ -48,6 +48,7 @@ class RenameRemoteFileOperation( | ||||
|     private val oldRemotePath: String, | ||||
|     private val newName: String, | ||||
|     isFolder: Boolean, | ||||
|     val spaceWebDavUrl: String? = null, | ||||
| ) : RemoteOperation<Unit>() { | ||||
| 
 | ||||
|     private var newRemotePath: String | ||||
| @ -75,8 +76,8 @@ class RenameRemoteFileOperation( | ||||
|             } | ||||
| 
 | ||||
|             val moveMethod: MoveMethod = MoveMethod( | ||||
|                 url = URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(oldRemotePath)), | ||||
|                 destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(newRemotePath), | ||||
|                 url = URL((spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(oldRemotePath)), | ||||
|                 destinationUrl = (spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(newRemotePath), | ||||
|             ).apply { | ||||
|                 setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS) | ||||
|                 setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) | ||||
|  | ||||
| @ -24,10 +24,9 @@ | ||||
|  */ | ||||
| package com.owncloud.android.lib.resources.files.chunks | ||||
| 
 | ||||
| import android.net.Uri | ||||
| import com.owncloud.android.lib.common.OwnCloudClient | ||||
| import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation | ||||
| 
 | ||||
| class RemoveRemoteChunksFolderOperation(remotePath: String) : RemoveRemoteFileOperation(remotePath) { | ||||
|     override fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.uploadsWebDavUri | ||||
|     override fun getSrcWebDavUriForClient(client: OwnCloudClient): String = client.uploadsWebDavUri.toString() | ||||
| } | ||||
|  | ||||
| @ -43,7 +43,8 @@ interface FileService : Service { | ||||
|     fun createFolder( | ||||
|         remotePath: String, | ||||
|         createFullPath: Boolean, | ||||
|         isChunkFolder: Boolean = false | ||||
|         isChunkFolder: Boolean = false, | ||||
|         spaceWebDavUrl: String? = null, | ||||
|     ): RemoteOperationResult<Unit> | ||||
| 
 | ||||
|     fun downloadFile( | ||||
| @ -57,15 +58,18 @@ interface FileService : Service { | ||||
|     ): RemoteOperationResult<Unit> | ||||
| 
 | ||||
|     fun readFile( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String? = null, | ||||
|     ): RemoteOperationResult<RemoteFile> | ||||
| 
 | ||||
|     fun refreshFolder( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String? = null, | ||||
|     ): RemoteOperationResult<ArrayList<RemoteFile>> | ||||
| 
 | ||||
|     fun removeFile( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String? = null, | ||||
|     ): RemoteOperationResult<Unit> | ||||
| 
 | ||||
|     fun renameFile( | ||||
| @ -73,5 +77,6 @@ interface FileService : Service { | ||||
|         oldRemotePath: String, | ||||
|         newName: String, | ||||
|         isFolder: Boolean, | ||||
|         spaceWebDavUrl: String? = null, | ||||
|     ): RemoteOperationResult<Unit> | ||||
| } | ||||
|  | ||||
| @ -64,12 +64,14 @@ class OCFileService(override val client: OwnCloudClient) : FileService { | ||||
|     override fun createFolder( | ||||
|         remotePath: String, | ||||
|         createFullPath: Boolean, | ||||
|         isChunkFolder: Boolean | ||||
|         isChunkFolder: Boolean, | ||||
|         spaceWebDavUrl: String?, | ||||
|     ): RemoteOperationResult<Unit> = | ||||
|         CreateRemoteFolderOperation( | ||||
|             remotePath = remotePath, | ||||
|             createFullPath = createFullPath, | ||||
|             isChunksFolder = isChunkFolder | ||||
|             isChunksFolder = isChunkFolder, | ||||
|             spaceWebDavUrl = spaceWebDavUrl, | ||||
|         ).execute(client) | ||||
| 
 | ||||
|     override fun downloadFile( | ||||
| @ -91,36 +93,44 @@ class OCFileService(override val client: OwnCloudClient) : FileService { | ||||
|         ).execute(client) | ||||
| 
 | ||||
|     override fun readFile( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String?, | ||||
|     ): RemoteOperationResult<RemoteFile> = | ||||
|         ReadRemoteFileOperation( | ||||
|             remotePath = remotePath | ||||
|             remotePath = remotePath, | ||||
|             spaceWebDavUrl = spaceWebDavUrl, | ||||
|         ).execute(client) | ||||
| 
 | ||||
|     override fun refreshFolder( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String?, | ||||
|     ): RemoteOperationResult<ArrayList<RemoteFile>> = | ||||
|         ReadRemoteFolderOperation( | ||||
|             remotePath = remotePath | ||||
|             remotePath = remotePath, | ||||
|             spaceWebDavUrl = spaceWebDavUrl, | ||||
|         ).execute(client) | ||||
| 
 | ||||
|     override fun removeFile( | ||||
|         remotePath: String | ||||
|         remotePath: String, | ||||
|         spaceWebDavUrl: String?, | ||||
|     ): RemoteOperationResult<Unit> = | ||||
|         RemoveRemoteFileOperation( | ||||
|             remotePath = remotePath | ||||
|             remotePath = remotePath, | ||||
|             spaceWebDavUrl = spaceWebDavUrl, | ||||
|         ).execute(client) | ||||
| 
 | ||||
|     override fun renameFile( | ||||
|         oldName: String, | ||||
|         oldRemotePath: String, | ||||
|         newName: String, | ||||
|         isFolder: Boolean | ||||
|         isFolder: Boolean, | ||||
|         spaceWebDavUrl: String?, | ||||
|     ): RemoteOperationResult<Unit> = | ||||
|         RenameRemoteFileOperation( | ||||
|             oldName = oldName, | ||||
|             oldRemotePath = oldRemotePath, | ||||
|             newName = newName, | ||||
|             isFolder = isFolder | ||||
|             isFolder = isFolder, | ||||
|             spaceWebDavUrl = spaceWebDavUrl, | ||||
|         ).execute(client) | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,59 @@ | ||||
| /* ownCloud Android Library is available under MIT license | ||||
|  *   Copyright (C) 2023 ownCloud GmbH. | ||||
|  * | ||||
|  *   Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  *   of this software and associated documentation files (the "Software"), to deal | ||||
|  *   in the Software without restriction, including without limitation the rights | ||||
|  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||
|  *   copies of the Software, and to permit persons to whom the Software is | ||||
|  *   furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  *   The above copyright notice and this permission notice shall be included in | ||||
|  *   all copies or substantial portions of the Software. | ||||
|  * | ||||
|  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||
|  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||
|  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||
|  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||||
|  *   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||||
|  *   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||||
|  *   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||
|  *   THE SOFTWARE. | ||||
|  * | ||||
|  */ | ||||
| package com.owncloud.android.lib | ||||
| 
 | ||||
| import android.os.Build | ||||
| import com.owncloud.android.lib.resources.files.RemoteFile | ||||
| import okhttp3.HttpUrl.Companion.toHttpUrl | ||||
| import org.junit.Assert.assertEquals | ||||
| import org.junit.Test | ||||
| import org.junit.runner.RunWith | ||||
| import org.robolectric.RobolectricTestRunner | ||||
| import org.robolectric.annotation.Config | ||||
| 
 | ||||
| @RunWith(RobolectricTestRunner::class) | ||||
| @Config(sdk = [Build.VERSION_CODES.O], manifest = Config.NONE) | ||||
| class RemoteFileTest { | ||||
| 
 | ||||
|     @Test | ||||
|     fun getRemotePathFromUrl_legacyWebDav() { | ||||
|         val httpUrlToTest = "https://server.url/remote.php/dav/files/username/Documents/text.txt".toHttpUrl() | ||||
|         val expectedRemotePath = "/Documents/text.txt" | ||||
| 
 | ||||
|         val actualRemotePath = RemoteFile.Companion.getRemotePathFromUrl(httpUrlToTest, "username") | ||||
|         assertEquals(expectedRemotePath, actualRemotePath) | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     fun getRemotePathFromUrl_spacesWebDav() { | ||||
|         val spaceWebDavUrl = "https://server.url/dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9" | ||||
| 
 | ||||
|         val httpUrlToTest = | ||||
|             "https://server.url/dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9/Documents/text.txt".toHttpUrl() | ||||
|         val expectedRemotePath = "/Documents/text.txt" | ||||
| 
 | ||||
|         val actualRemotePath = RemoteFile.Companion.getRemotePathFromUrl(httpUrlToTest, "username", spaceWebDavUrl) | ||||
|         assertEquals(expectedRemotePath, actualRemotePath) | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user