1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-07-10 07:58:54 +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( class CreateRemoteFolderOperation(
val remotePath: String, val remotePath: String,
private val createFullPath: Boolean, private val createFullPath: Boolean,
private val isChunksFolder: Boolean = false, private val isChunksFolder: Boolean = false
val spaceWebDavUrl: String? = null,
) : RemoteOperation<Unit>() { ) : RemoteOperation<Unit>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
@ -68,13 +67,13 @@ class CreateRemoteFolderOperation(
var result: RemoteOperationResult<Unit> var result: RemoteOperationResult<Unit>
try { try {
val webDavUri = if (isChunksFolder) { val webDavUri = if (isChunksFolder) {
client.uploadsWebDavUri.toString() client.uploadsWebDavUri
} else { } else {
spaceWebDavUrl ?: client.userFilesWebDavUri.toString() client.userFilesWebDavUri
} }
val mkCol = MkColMethod( val mkCol = MkColMethod(
URL(webDavUri + WebdavUtils.encodePath(remotePath)) URL(webDavUri.toString() + WebdavUtils.encodePath(remotePath))
).apply { ).apply {
setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS) setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS) setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)

View File

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

View File

@ -48,8 +48,7 @@ import java.net.URL
* @author David González Verdugo * @author David González Verdugo
*/ */
class ReadRemoteFolderOperation( class ReadRemoteFolderOperation(
val remotePath: String, val remotePath: String
val spaceWebDavUrl: String? = null,
) : RemoteOperation<ArrayList<RemoteFile>>() { ) : RemoteOperation<ArrayList<RemoteFile>>() {
/** /**
@ -62,7 +61,7 @@ class ReadRemoteFolderOperation(
PropertyRegistry.register(OCShareTypes.Factory()) PropertyRegistry.register(OCShareTypes.Factory())
val propfindMethod = PropfindMethod( val propfindMethod = PropfindMethod(
getFinalWebDavUrl(), URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath)),
DavConstants.DEPTH_1, DavConstants.DEPTH_1,
DavUtils.allPropset DavUtils.allPropset
) )
@ -72,11 +71,12 @@ class ReadRemoteFolderOperation(
if (isSuccess(status)) { if (isSuccess(status)) {
val mFolderAndFiles = ArrayList<RemoteFile>() val mFolderAndFiles = ArrayList<RemoteFile>()
// parse data from remote folder
// TODO: Remove that !!
val remoteFolder = RemoteFile.getRemoteFileFromDav( val remoteFolder = RemoteFile.getRemoteFileFromDav(
davResource = propfindMethod.root!!, davResource = propfindMethod.root!!,
userId = AccountUtils.getUserId(mAccount, mContext), userId = AccountUtils.getUserId(mAccount, mContext),
userName = mAccount.name, userName = mAccount.name
spaceWebDavUrl = spaceWebDavUrl,
) )
mFolderAndFiles.add(remoteFolder) mFolderAndFiles.add(remoteFolder)
@ -85,8 +85,7 @@ class ReadRemoteFolderOperation(
val remoteFile = RemoteFile.getRemoteFileFromDav( val remoteFile = RemoteFile.getRemoteFileFromDav(
davResource = resource, davResource = resource,
userId = AccountUtils.getUserId(mAccount, mContext), userId = AccountUtils.getUserId(mAccount, mContext),
userName = mAccount.name, userName = mAccount.name
spaceWebDavUrl = spaceWebDavUrl,
) )
mFolderAndFiles.add(remoteFile) 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) 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.net.Uri
import android.os.Parcelable import android.os.Parcelable
import androidx.annotation.VisibleForTesting
import at.bitfire.dav4jvm.PropStat import at.bitfire.dav4jvm.PropStat
import at.bitfire.dav4jvm.Property import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.Response import at.bitfire.dav4jvm.Response
@ -101,13 +100,8 @@ data class RemoteFile(
const val MIME_DIR = "DIR" const val MIME_DIR = "DIR"
const val MIME_DIR_UNIX = "httpd/unix-directory" const val MIME_DIR_UNIX = "httpd/unix-directory"
fun getRemoteFileFromDav( fun getRemoteFileFromDav(davResource: Response, userId: String, userName: String): RemoteFile {
davResource: Response, val remotePath = getRemotePathFromUrl(davResource.href, userId)
userId: String,
userName: String,
spaceWebDavUrl: String? = null
): RemoteFile {
val remotePath = getRemotePathFromUrl(davResource.href, userId, spaceWebDavUrl)
val remoteFile = RemoteFile(remotePath = remotePath, owner = userName) val remoteFile = RemoteFile(remotePath = remotePath, owner = userName)
val properties = getPropertiesEvenIfPostProcessing(davResource) val properties = getPropertiesEvenIfPostProcessing(davResource)
@ -170,25 +164,15 @@ data class RemoteFile(
* Retrieves a relative path from a remote file url * Retrieves a relative path from a remote file url
* *
* *
* Example legacy: * Example: url:port/remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt
* /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 url remote file url
* @param userId file owner * @param userId file owner
* @param spaceWebDavUrl custom web dav url for space
* @return remote relative path of the file * @return remote relative path of the file
*/ */
@VisibleForTesting private fun getRemotePathFromUrl(url: HttpUrl, userId: String): String {
fun getRemotePathFromUrl( val davFilesPath = OwnCloudClient.WEBDAV_FILES_PATH_4_0 + userId
url: HttpUrl, val absoluteDavPath = Uri.decode(url.encodedPath)
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() val pathToOc = absoluteDavPath.split(davFilesPath).first()
return absoluteDavPath.replace(pathToOc + davFilesPath, "") return absoluteDavPath.replace(pathToOc + davFilesPath, "")
} }

View File

@ -24,6 +24,7 @@
package com.owncloud.android.lib.resources.files package com.owncloud.android.lib.resources.files
import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient 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_NO_CONTENT
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK
@ -45,8 +46,7 @@ import java.net.URL
* @author Abel García de Prada * @author Abel García de Prada
*/ */
open class RemoveRemoteFileOperation( open class RemoveRemoteFileOperation(
private val remotePath: String, private val remotePath: String
val spaceWebDavUrl: String? = null,
) : RemoteOperation<Unit>() { ) : RemoteOperation<Unit>() {
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> { override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
@ -54,7 +54,7 @@ open class RemoveRemoteFileOperation(
try { try {
val srcWebDavUri = getSrcWebDavUriForClient(client) val srcWebDavUri = getSrcWebDavUriForClient(client)
val deleteMethod = DeleteMethod( val deleteMethod = DeleteMethod(
URL(srcWebDavUri + WebdavUtils.encodePath(remotePath)) URL(srcWebDavUri.toString() + WebdavUtils.encodePath(remotePath))
) )
val status = client.executeHttpMethod(deleteMethod) val status = client.executeHttpMethod(deleteMethod)
@ -75,7 +75,7 @@ open class RemoveRemoteFileOperation(
* For standard removals, we will use [OwnCloudClient.getUserFilesWebDavUri]. * For standard removals, we will use [OwnCloudClient.getUserFilesWebDavUri].
* In case we need a different source Uri, override this method. * 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) 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 oldRemotePath: String,
private val newName: String, private val newName: String,
isFolder: Boolean, isFolder: Boolean,
val spaceWebDavUrl: String? = null,
) : RemoteOperation<Unit>() { ) : RemoteOperation<Unit>() {
private var newRemotePath: String private var newRemotePath: String
@ -76,8 +75,8 @@ class RenameRemoteFileOperation(
} }
val moveMethod: MoveMethod = MoveMethod( val moveMethod: MoveMethod = MoveMethod(
url = URL((spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(oldRemotePath)), url = URL(client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(oldRemotePath)),
destinationUrl = (spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(newRemotePath), destinationUrl = client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(newRemotePath),
).apply { ).apply {
setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS) setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.MILLISECONDS)
setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) setConnectionTimeout(RENAME_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)

View File

@ -24,9 +24,10 @@
*/ */
package com.owncloud.android.lib.resources.files.chunks package com.owncloud.android.lib.resources.files.chunks
import android.net.Uri
import com.owncloud.android.lib.common.OwnCloudClient import com.owncloud.android.lib.common.OwnCloudClient
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation
class RemoveRemoteChunksFolderOperation(remotePath: String) : RemoveRemoteFileOperation(remotePath) { 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( fun createFolder(
remotePath: String, remotePath: String,
createFullPath: Boolean, createFullPath: Boolean,
isChunkFolder: Boolean = false, isChunkFolder: Boolean = false
spaceWebDavUrl: String? = null,
): RemoteOperationResult<Unit> ): RemoteOperationResult<Unit>
fun downloadFile( fun downloadFile(
@ -58,18 +57,15 @@ interface FileService : Service {
): RemoteOperationResult<Unit> ): RemoteOperationResult<Unit>
fun readFile( fun readFile(
remotePath: String, remotePath: String
spaceWebDavUrl: String? = null,
): RemoteOperationResult<RemoteFile> ): RemoteOperationResult<RemoteFile>
fun refreshFolder( fun refreshFolder(
remotePath: String, remotePath: String
spaceWebDavUrl: String? = null,
): RemoteOperationResult<ArrayList<RemoteFile>> ): RemoteOperationResult<ArrayList<RemoteFile>>
fun removeFile( fun removeFile(
remotePath: String, remotePath: String
spaceWebDavUrl: String? = null,
): RemoteOperationResult<Unit> ): RemoteOperationResult<Unit>
fun renameFile( fun renameFile(
@ -77,6 +73,5 @@ interface FileService : Service {
oldRemotePath: String, oldRemotePath: String,
newName: String, newName: String,
isFolder: Boolean, isFolder: Boolean,
spaceWebDavUrl: String? = null,
): RemoteOperationResult<Unit> ): RemoteOperationResult<Unit>
} }

View File

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