Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 8e4f243

Browse files
abelgardepJuancaG05
authored andcommitted
Fix remote path retrieval. Now it depends on webdav url to support spaces
1 parent a65a82c commit 8e4f243

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/ReadRemoteFolderOperation.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ class ReadRemoteFolderOperation(
7272
if (isSuccess(status)) {
7373
val mFolderAndFiles = ArrayList<RemoteFile>()
7474

75-
// parse data from remote folder
76-
// TODO: Remove that !!
7775
val remoteFolder = RemoteFile.getRemoteFileFromDav(
7876
davResource = propfindMethod.root!!,
7977
userId = AccountUtils.getUserId(mAccount, mContext),
80-
userName = mAccount.name
78+
userName = mAccount.name,
79+
spaceWebDavUrl = spaceWebDavUrl,
8180
)
8281
mFolderAndFiles.add(remoteFolder)
8382

@@ -86,7 +85,8 @@ class ReadRemoteFolderOperation(
8685
val remoteFile = RemoteFile.getRemoteFileFromDav(
8786
davResource = resource,
8887
userId = AccountUtils.getUserId(mAccount, mContext),
89-
userName = mAccount.name
88+
userName = mAccount.name,
89+
spaceWebDavUrl = spaceWebDavUrl,
9090
)
9191
mFolderAndFiles.add(remoteFile)
9292
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/files/RemoteFile.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package com.owncloud.android.lib.resources.files
2626

2727
import android.net.Uri
2828
import android.os.Parcelable
29+
import androidx.annotation.VisibleForTesting
2930
import at.bitfire.dav4jvm.PropStat
3031
import at.bitfire.dav4jvm.Property
3132
import at.bitfire.dav4jvm.Response
@@ -100,8 +101,13 @@ data class RemoteFile(
100101
const val MIME_DIR = "DIR"
101102
const val MIME_DIR_UNIX = "httpd/unix-directory"
102103

103-
fun getRemoteFileFromDav(davResource: Response, userId: String, userName: String): RemoteFile {
104-
val remotePath = getRemotePathFromUrl(davResource.href, userId)
104+
fun getRemoteFileFromDav(
105+
davResource: Response,
106+
userId: String,
107+
userName: String,
108+
spaceWebDavUrl: String? = null
109+
): RemoteFile {
110+
val remotePath = getRemotePathFromUrl(davResource.href, userId, spaceWebDavUrl)
105111
val remoteFile = RemoteFile(remotePath = remotePath, owner = userName)
106112
val properties = getPropertiesEvenIfPostProcessing(davResource)
107113

@@ -164,15 +170,25 @@ data class RemoteFile(
164170
* Retrieves a relative path from a remote file url
165171
*
166172
*
167-
* Example: url:port/remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt
173+
* Example legacy:
174+
* /remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt
175+
*
176+
* Example spaces:
177+
* /dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9/Documents/text.txt => /Documents/text.txt
168178
*
169179
* @param url remote file url
170180
* @param userId file owner
181+
* @param spaceWebDavUrl custom web dav url for space
171182
* @return remote relative path of the file
172183
*/
173-
private fun getRemotePathFromUrl(url: HttpUrl, userId: String): String {
174-
val davFilesPath = OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId
175-
val absoluteDavPath = Uri.decode(url.encodedPath)
184+
@VisibleForTesting
185+
fun getRemotePathFromUrl(
186+
url: HttpUrl,
187+
userId: String,
188+
spaceWebDavUrl: String? = null,
189+
): String {
190+
val davFilesPath = spaceWebDavUrl ?: (OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId)
191+
val absoluteDavPath = if (spaceWebDavUrl != null) Uri.decode(url.toString()) else Uri.decode(url.encodedPath)
176192
val pathToOc = absoluteDavPath.split(davFilesPath).first()
177193
return absoluteDavPath.replace(pathToOc + davFilesPath, "")
178194
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* ownCloud Android Library is available under MIT license
2+
* Copyright (C) 2023 ownCloud GmbH.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
*/
24+
package com.owncloud.android.lib
25+
26+
import android.os.Build
27+
import com.owncloud.android.lib.resources.files.RemoteFile
28+
import okhttp3.HttpUrl.Companion.toHttpUrl
29+
import org.junit.Assert.assertEquals
30+
import org.junit.Test
31+
import org.junit.runner.RunWith
32+
import org.robolectric.RobolectricTestRunner
33+
import org.robolectric.annotation.Config
34+
35+
@RunWith(RobolectricTestRunner::class)
36+
@Config(sdk = [Build.VERSION_CODES.O], manifest = Config.NONE)
37+
class RemoteFileTest {
38+
39+
@Test
40+
fun getRemotePathFromUrl_legacyWebDav() {
41+
val httpUrlToTest = "https://server.url/remote.php/dav/files/username/Documents/text.txt".toHttpUrl()
42+
val expectedRemotePath = "/Documents/text.txt"
43+
44+
val actualRemotePath = RemoteFile.Companion.getRemotePathFromUrl(httpUrlToTest, "username")
45+
assertEquals(expectedRemotePath, actualRemotePath)
46+
}
47+
48+
@Test
49+
fun getRemotePathFromUrl_spacesWebDav() {
50+
val spaceWebDavUrl = "https://server.url/dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9"
51+
52+
val httpUrlToTest =
53+
"https://server.url/dav/spaces/8871f4f3-fc6f-4a66-8bed-62f175f76f38$05bca744-d89f-4e9c-a990-25a0d7f03fe9/Documents/text.txt".toHttpUrl()
54+
val expectedRemotePath = "/Documents/text.txt"
55+
56+
val actualRemotePath = RemoteFile.Companion.getRemotePathFromUrl(httpUrlToTest, "username", spaceWebDavUrl)
57+
assertEquals(expectedRemotePath, actualRemotePath)
58+
}
59+
}

0 commit comments

Comments
 (0)