1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-07-09 15:38:41 +00:00

Compare commits

..

No commits in common. "887ea6f2cd90d39289a465bc7f32c1f91a7d569e" and "7799b7760093a654622527a7f8c66a5490abfffa" have entirely different histories.

10 changed files with 43 additions and 151 deletions

View File

@ -46,8 +46,7 @@ import java.util.concurrent.TimeUnit
class CreateRemoteFolderOperation(
val remotePath: String,
private val createFullPath: Boolean,
private val isChunksFolder: Boolean = false,
val spaceWebDavUrl: String? = null,
private val isChunksFolder: Boolean = false
) : RemoteOperation<Unit>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
@ -68,13 +67,13 @@ class CreateRemoteFolderOperation(
var result: RemoteOperationResult<Unit>
try {
val webDavUri = if (isChunksFolder) {
client.uploadsWebDavUri.toString()
client.uploadsWebDavUri
} else {
spaceWebDavUrl ?: client.userFilesWebDavUri.toString()
client.userFilesWebDavUri
}
val mkCol = MkColMethod(
URL(webDavUri + WebdavUtils.encodePath(remotePath))
URL(webDavUri.toString() + WebdavUtils.encodePath(remotePath))
).apply {
setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)

View File

@ -46,10 +46,7 @@ import java.util.concurrent.TimeUnit
* @author David González Verdugo
*/
class ReadRemoteFileOperation(
val remotePath: String,
val spaceWebDavUrl: String? = null,
) : RemoteOperation<RemoteFile>() {
class ReadRemoteFileOperation(val remotePath: String) : RemoteOperation<RemoteFile>() {
/**
* Performs the read operation.
@ -60,7 +57,7 @@ class ReadRemoteFileOperation(
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteFile> {
try {
val propFind = PropfindMethod(
url = getFinalWebDavUrl(),
url = URL("${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)}"),
depth = DEPTH_0,
propertiesToRequest = DavUtils.allPropset
).apply {
@ -72,11 +69,10 @@ class ReadRemoteFileOperation(
Timber.i("Read remote file $remotePath with status ${propFind.statusCode}")
return if (isSuccess(status)) {
// TODO: Remove that !!
val remoteFile = RemoteFile.getRemoteFileFromDav(
davResource = propFind.root!!,
userId = AccountUtils.getUserId(mAccount, mContext),
userName = mAccount.name,
spaceWebDavUrl = spaceWebDavUrl,
propFind.root!!,
AccountUtils.getUserId(mAccount, mContext), mAccount.name
)
RemoteOperationResult<RemoteFile>(RemoteOperationResult.ResultCode.OK).apply {
@ -92,12 +88,6 @@ class ReadRemoteFileOperation(
}
}
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 {

View File

@ -48,8 +48,7 @@ import java.net.URL
* @author David González Verdugo
*/
class ReadRemoteFolderOperation(
val remotePath: String,
val spaceWebDavUrl: String? = null,
val remotePath: String
) : RemoteOperation<ArrayList<RemoteFile>>() {
/**
@ -62,7 +61,7 @@ class ReadRemoteFolderOperation(
PropertyRegistry.register(OCShareTypes.Factory())
val propfindMethod = PropfindMethod(
getFinalWebDavUrl(),
URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath)),
DavConstants.DEPTH_1,
DavUtils.allPropset
)
@ -72,11 +71,12 @@ 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,
spaceWebDavUrl = spaceWebDavUrl,
userName = mAccount.name
)
mFolderAndFiles.add(remoteFolder)
@ -85,8 +85,7 @@ class ReadRemoteFolderOperation(
val remoteFile = RemoteFile.getRemoteFileFromDav(
davResource = resource,
userId = AccountUtils.getUserId(mAccount, mContext),
userName = mAccount.name,
spaceWebDavUrl = spaceWebDavUrl,
userName = mAccount.name
)
mFolderAndFiles.add(remoteFile)
}
@ -108,11 +107,5 @@ 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)
}

View File

@ -26,7 +26,6 @@ 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
@ -101,13 +100,8 @@ data class RemoteFile(
const val MIME_DIR = "DIR"
const val MIME_DIR_UNIX = "httpd/unix-directory"
fun getRemoteFileFromDav(
davResource: Response,
userId: String,
userName: String,
spaceWebDavUrl: String? = null
): RemoteFile {
val remotePath = getRemotePathFromUrl(davResource.href, userId, spaceWebDavUrl)
fun getRemoteFileFromDav(davResource: Response, userId: String, userName: String): RemoteFile {
val remotePath = getRemotePathFromUrl(davResource.href, userId)
val remoteFile = RemoteFile(remotePath = remotePath, owner = userName)
val properties = getPropertiesEvenIfPostProcessing(davResource)
@ -170,25 +164,15 @@ data class RemoteFile(
* Retrieves a relative path from a remote file url
*
*
* 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
* Example: url:port/remote.php/dav/files/username/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
*/
@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)
private fun getRemotePathFromUrl(url: HttpUrl, userId: String): String {
val davFilesPath = OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId
val absoluteDavPath = Uri.decode(url.encodedPath)
val pathToOc = absoluteDavPath.split(davFilesPath).first()
return absoluteDavPath.replace(pathToOc + davFilesPath, "")
}

View File

@ -24,6 +24,7 @@
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
@ -45,8 +46,7 @@ import java.net.URL
* @author Abel García de Prada
*/
open class RemoveRemoteFileOperation(
private val remotePath: String,
val spaceWebDavUrl: String? = null,
private val remotePath: String
) : 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 + WebdavUtils.encodePath(remotePath))
URL(srcWebDavUri.toString() + 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): String = spaceWebDavUrl ?: client.userFilesWebDavUri.toString()
open fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.userFilesWebDavUri
private fun isSuccess(status: Int) = status.isOneOf(HTTP_OK, HTTP_NO_CONTENT)
}

View File

@ -48,7 +48,6 @@ class RenameRemoteFileOperation(
private val oldRemotePath: String,
private val newName: String,
isFolder: Boolean,
val spaceWebDavUrl: String? = null,
) : RemoteOperation<Unit>() {
private var newRemotePath: String
@ -76,8 +75,8 @@ class RenameRemoteFileOperation(
}
val moveMethod: MoveMethod = MoveMethod(
url = URL((spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(oldRemotePath)),
destinationUrl = (spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(newRemotePath),
url = URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(oldRemotePath)),
destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(newRemotePath),
).apply {
setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS)
setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)

View File

@ -24,9 +24,10 @@
*/
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): String = client.uploadsWebDavUri.toString()
override fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.uploadsWebDavUri
}

View File

@ -43,8 +43,7 @@ interface FileService : Service {
fun createFolder(
remotePath: String,
createFullPath: Boolean,
isChunkFolder: Boolean = false,
spaceWebDavUrl: String? = null,
isChunkFolder: Boolean = false
): RemoteOperationResult<Unit>
fun downloadFile(
@ -58,18 +57,15 @@ interface FileService : Service {
): RemoteOperationResult<Unit>
fun readFile(
remotePath: String,
spaceWebDavUrl: String? = null,
remotePath: String
): RemoteOperationResult<RemoteFile>
fun refreshFolder(
remotePath: String,
spaceWebDavUrl: String? = null,
remotePath: String
): RemoteOperationResult<ArrayList<RemoteFile>>
fun removeFile(
remotePath: String,
spaceWebDavUrl: String? = null,
remotePath: String
): RemoteOperationResult<Unit>
fun renameFile(
@ -77,6 +73,5 @@ interface FileService : Service {
oldRemotePath: String,
newName: String,
isFolder: Boolean,
spaceWebDavUrl: String? = null,
): RemoteOperationResult<Unit>
}

View File

@ -64,14 +64,12 @@ class OCFileService(override val client: OwnCloudClient) : FileService {
override fun createFolder(
remotePath: String,
createFullPath: Boolean,
isChunkFolder: Boolean,
spaceWebDavUrl: String?,
isChunkFolder: Boolean
): RemoteOperationResult<Unit> =
CreateRemoteFolderOperation(
remotePath = remotePath,
createFullPath = createFullPath,
isChunksFolder = isChunkFolder,
spaceWebDavUrl = spaceWebDavUrl,
isChunksFolder = isChunkFolder
).execute(client)
override fun downloadFile(
@ -93,44 +91,36 @@ class OCFileService(override val client: OwnCloudClient) : FileService {
).execute(client)
override fun readFile(
remotePath: String,
spaceWebDavUrl: String?,
remotePath: String
): RemoteOperationResult<RemoteFile> =
ReadRemoteFileOperation(
remotePath = remotePath,
spaceWebDavUrl = spaceWebDavUrl,
remotePath = remotePath
).execute(client)
override fun refreshFolder(
remotePath: String,
spaceWebDavUrl: String?,
remotePath: String
): RemoteOperationResult<ArrayList<RemoteFile>> =
ReadRemoteFolderOperation(
remotePath = remotePath,
spaceWebDavUrl = spaceWebDavUrl,
remotePath = remotePath
).execute(client)
override fun removeFile(
remotePath: String,
spaceWebDavUrl: String?,
remotePath: String
): RemoteOperationResult<Unit> =
RemoveRemoteFileOperation(
remotePath = remotePath,
spaceWebDavUrl = spaceWebDavUrl,
remotePath = remotePath
).execute(client)
override fun renameFile(
oldName: String,
oldRemotePath: String,
newName: String,
isFolder: Boolean,
spaceWebDavUrl: String?,
isFolder: Boolean
): RemoteOperationResult<Unit> =
RenameRemoteFileOperation(
oldName = oldName,
oldRemotePath = oldRemotePath,
newName = newName,
isFolder = isFolder,
spaceWebDavUrl = spaceWebDavUrl,
isFolder = isFolder
).execute(client)
}

View File

@ -1,59 +0,0 @@
/* 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)
}
}