mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Compare commits
No commits in common. "master" and "1.0.15-beta.1" have entirely different histories.
master
...
1.0.15-bet
@ -49,6 +49,6 @@ ownCloud Android Library uses OkHttp version 4.6.0, licensed under Apache Licens
|
|||||||
|
|
||||||
### Compatibility
|
### Compatibility
|
||||||
|
|
||||||
ownCloud Android Library is valid for Android systems from version Android 6 (android:minSdkVersion="23" android:targetSdkVersion="33").
|
ownCloud Android Library is valid for Android systems from version Android 5 (android:minSdkVersion="21" android:targetSdkVersion="29").
|
||||||
|
|
||||||
ownCloud Android library supports ownCloud server from version 4.5.
|
ownCloud Android library supports ownCloud server from version 4.5.
|
||||||
|
17
build.gradle
17
build.gradle
@ -1,7 +1,7 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext {
|
ext {
|
||||||
orgJetbrainsKotlin = '1.8.10'
|
kotlinVersion = '1.6.21'
|
||||||
comSquareupMoshi = '1.14.0'
|
moshiVersion = "1.13.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
@ -10,16 +10,12 @@ buildscript {
|
|||||||
maven { url "https://plugins.gradle.org/m2/" }
|
maven { url "https://plugins.gradle.org/m2/" }
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "org.jlleitschuh.gradle:ktlint-gradle:11.1.0"
|
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.3.0"
|
||||||
classpath 'com.android.tools.build:gradle:7.4.2'
|
classpath 'com.android.tools.build:gradle:7.1.3'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$orgJetbrainsKotlin"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
|
||||||
id 'com.google.devtools.ksp' version '1.8.10-1.0.9' apply false
|
|
||||||
}
|
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
@ -30,5 +26,4 @@ allprojects {
|
|||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
apply plugin: "org.jlleitschuh.gradle.ktlint"
|
apply plugin: "org.jlleitschuh.gradle.ktlint"
|
||||||
apply plugin: "com.google.devtools.ksp"
|
}
|
||||||
}
|
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@ -1,32 +1,34 @@
|
|||||||
apply plugin: 'com.android.library'
|
apply plugin: 'com.android.library'
|
||||||
apply plugin: 'kotlin-android'
|
apply plugin: 'kotlin-android'
|
||||||
apply plugin: 'com.google.devtools.ksp'
|
apply plugin: 'kotlin-kapt'
|
||||||
apply plugin: 'kotlin-parcelize'
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api 'com.squareup.okhttp3:okhttp:4.6.0'
|
api 'com.squareup.okhttp3:okhttp:4.6.0'
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$orgJetbrainsKotlin"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
|
||||||
api 'com.gitlab.ownclouders:dav4android:oc_support_2.1.5'
|
api 'com.gitlab.ownclouders:dav4android:oc_support_2.1.5'
|
||||||
api 'com.github.AppDevNext.Logcat:LogcatCore:2.2.2'
|
api 'com.github.AppDevNext.Logcat:LogcatCore:2.2.2'
|
||||||
|
|
||||||
// Moshi
|
// Moshi
|
||||||
implementation("com.squareup.moshi:moshi-kotlin:$comSquareupMoshi") {
|
implementation("com.squareup.moshi:moshi-kotlin:$moshiVersion") {
|
||||||
exclude module: "kotlin-reflect"
|
exclude module: "kotlin-reflect"
|
||||||
}
|
}
|
||||||
implementation 'org.apache.commons:commons-lang3:3.12.0'
|
implementation 'org.apache.commons:commons-lang3:3.12.0'
|
||||||
ksp "com.squareup.moshi:moshi-kotlin-codegen:$comSquareupMoshi"
|
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
testImplementation 'org.robolectric:robolectric:4.10'
|
testImplementation 'org.robolectric:robolectric:4.8.1'
|
||||||
debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.6.0'
|
debugImplementation 'com.facebook.stetho:stetho-okhttp3:1.6.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 33
|
compileSdkVersion 31
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 23
|
minSdkVersion 21
|
||||||
targetSdkVersion 33
|
targetSdkVersion 31
|
||||||
|
|
||||||
|
versionCode = 10001401
|
||||||
|
versionName = "1.0.15-beta.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
lint {
|
lint {
|
||||||
@ -34,10 +36,14 @@ android {
|
|||||||
ignoreWarnings true
|
ignoreWarnings true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
||||||
testOptions {
|
testOptions {
|
||||||
unitTests {
|
unitTests {
|
||||||
includeAndroidResources = true
|
includeAndroidResources = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
namespace 'com.owncloud.android.lib'
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!-- ownCloud Android Library is available under MIT license
|
<!-- ownCloud Android Library is available under MIT license
|
||||||
Copyright (C) 2023 ownCloud GmbH.
|
Copyright (C) 2016 ownCloud GmbH.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -23,8 +23,16 @@
|
|||||||
|
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest package="com.owncloud.android.lib"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<!-- USE_CREDENTIALS, MANAGE_ACCOUNTS and AUTHENTICATE_ACCOUNTS are needed for API < 23.
|
||||||
|
In API >= 23 the do not exist anymore -->
|
||||||
|
<uses-permission
|
||||||
|
android:name="android.permission.MANAGE_ACCOUNTS"
|
||||||
|
android:maxSdkVersion="22" />
|
||||||
|
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
|
||||||
|
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -37,6 +37,7 @@ import com.owncloud.android.lib.resources.status.RemoteServerInfo
|
|||||||
import org.apache.commons.lang3.exception.ExceptionUtils
|
import org.apache.commons.lang3.exception.ExceptionUtils
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ConnectionValidator
|
* ConnectionValidator
|
||||||
@ -45,7 +46,7 @@ import java.io.IOException
|
|||||||
*/
|
*/
|
||||||
class ConnectionValidator(
|
class ConnectionValidator(
|
||||||
val context: Context,
|
val context: Context,
|
||||||
private val clearCookiesOnValidation: Boolean
|
val clearCookiesOnValidation: Boolean
|
||||||
) {
|
) {
|
||||||
fun validate(baseClient: OwnCloudClient, singleSessionManager: SingleSessionManager, context: Context): Boolean {
|
fun validate(baseClient: OwnCloudClient, singleSessionManager: SingleSessionManager, context: Context): Boolean {
|
||||||
try {
|
try {
|
||||||
@ -60,12 +61,12 @@ class ConnectionValidator(
|
|||||||
client.account = baseClient.account
|
client.account = baseClient.account
|
||||||
client.credentials = baseClient.credentials
|
client.credentials = baseClient.credentials
|
||||||
while (validationRetryCount < VALIDATION_RETRY_COUNT) {
|
while (validationRetryCount < VALIDATION_RETRY_COUNT) {
|
||||||
Timber.d("validationRetryCount %d", validationRetryCount)
|
Timber.d("validationRetryCout %d", validationRetryCount)
|
||||||
var successCounter = 0
|
var successCounter = 0
|
||||||
var failCounter = 0
|
var failCounter = 0
|
||||||
|
|
||||||
client.setFollowRedirects(true)
|
client.setFollowRedirects(true)
|
||||||
if (isOwnCloudStatusOk(client)) {
|
if (isOnwCloudStatusOk(client)) {
|
||||||
successCounter++
|
successCounter++
|
||||||
} else {
|
} else {
|
||||||
failCounter++
|
failCounter++
|
||||||
@ -102,7 +103,7 @@ class ConnectionValidator(
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isOwnCloudStatusOk(client: OwnCloudClient): Boolean {
|
private fun isOnwCloudStatusOk(client: OwnCloudClient): Boolean {
|
||||||
val reply = getOwnCloudStatus(client)
|
val reply = getOwnCloudStatus(client)
|
||||||
// dont check status code. It currently relais on the broken redirect code of the owncloud client
|
// dont check status code. It currently relais on the broken redirect code of the owncloud client
|
||||||
// TODO: Use okhttp redirect and add this check again
|
// TODO: Use okhttp redirect and add this check again
|
||||||
@ -137,12 +138,6 @@ class ConnectionValidator(
|
|||||||
// test if have all the needed to effectively invalidate ...
|
// test if have all the needed to effectively invalidate ...
|
||||||
shouldInvalidateAccountCredentials =
|
shouldInvalidateAccountCredentials =
|
||||||
shouldInvalidateAccountCredentials and (account.savedAccount != null)
|
shouldInvalidateAccountCredentials and (account.savedAccount != null)
|
||||||
Timber.d(
|
|
||||||
"""Received error: $httpStatusCode,
|
|
||||||
account: ${account.name}
|
|
||||||
credentials are real: ${credentials !is OwnCloudAnonymousCredentials},
|
|
||||||
so we need to invalidate credentials for account ${account.name} : $shouldInvalidateAccountCredentials"""
|
|
||||||
)
|
|
||||||
return shouldInvalidateAccountCredentials
|
return shouldInvalidateAccountCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +150,6 @@ class ConnectionValidator(
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private fun invalidateAccountCredentials(account: OwnCloudAccount, credentials: OwnCloudCredentials) {
|
private fun invalidateAccountCredentials(account: OwnCloudAccount, credentials: OwnCloudCredentials) {
|
||||||
Timber.i("Invalidating account credentials for account $account")
|
|
||||||
val am = AccountManager.get(context)
|
val am = AccountManager.get(context)
|
||||||
am.invalidateAuthToken(
|
am.invalidateAuthToken(
|
||||||
account.savedAccount.type,
|
account.savedAccount.type,
|
||||||
@ -173,6 +167,8 @@ class ConnectionValidator(
|
|||||||
*
|
*
|
||||||
* Refresh current credentials if possible, and marks a retry.
|
* Refresh current credentials if possible, and marks a retry.
|
||||||
*
|
*
|
||||||
|
* @param status
|
||||||
|
* @param repeatCounter
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private fun checkUnauthorizedAccess(client: OwnCloudClient, singleSessionManager: SingleSessionManager, status: Int): Boolean {
|
private fun checkUnauthorizedAccess(client: OwnCloudClient, singleSessionManager: SingleSessionManager, status: Int): Boolean {
|
||||||
@ -185,7 +181,6 @@ class ConnectionValidator(
|
|||||||
if (credentials.authTokenCanBeRefreshed()) {
|
if (credentials.authTokenCanBeRefreshed()) {
|
||||||
try {
|
try {
|
||||||
// This command does the actual refresh
|
// This command does the actual refresh
|
||||||
Timber.i("Trying to refresh auth token for account $account")
|
|
||||||
account.loadCredentials(context)
|
account.loadCredentials(context)
|
||||||
// if mAccount.getCredentials().length() == 0 --> refresh failed
|
// if mAccount.getCredentials().length() == 0 --> refresh failed
|
||||||
client.credentials = account.credentials
|
client.credentials = account.credentials
|
||||||
@ -206,7 +201,6 @@ class ConnectionValidator(
|
|||||||
if (!credentialsWereRefreshed) {
|
if (!credentialsWereRefreshed) {
|
||||||
// if credentials are not refreshed, client must be removed
|
// if credentials are not refreshed, client must be removed
|
||||||
// from the OwnCloudClientManager to prevent it is reused once and again
|
// from the OwnCloudClientManager to prevent it is reused once and again
|
||||||
Timber.w("Credentials were not refreshed, client will be removed from the Session Manager to prevent using it over and over")
|
|
||||||
singleSessionManager.removeClientFor(account)
|
singleSessionManager.removeClientFor(account)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -216,6 +210,6 @@ class ConnectionValidator(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val VALIDATION_RETRY_COUNT = 3
|
private val VALIDATION_RETRY_COUNT = 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ import com.owncloud.android.lib.common.http.HttpClient;
|
|||||||
import com.owncloud.android.lib.common.http.HttpConstants;
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
|
import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
|
||||||
import com.owncloud.android.lib.common.utils.RandomUtils;
|
import com.owncloud.android.lib.common.utils.RandomUtils;
|
||||||
|
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
||||||
import okhttp3.Cookie;
|
import okhttp3.Cookie;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
@ -43,7 +44,6 @@ import timber.log.Timber;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
|
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
|
||||||
import static com.owncloud.android.lib.common.http.HttpConstants.HTTP_MOVED_PERMANENTLY;
|
import static com.owncloud.android.lib.common.http.HttpConstants.HTTP_MOVED_PERMANENTLY;
|
||||||
@ -59,6 +59,7 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
private OwnCloudCredentials mCredentials = null;
|
private OwnCloudCredentials mCredentials = null;
|
||||||
private int mInstanceNumber;
|
private int mInstanceNumber;
|
||||||
private Uri mBaseUri;
|
private Uri mBaseUri;
|
||||||
|
private OwnCloudVersion mVersion = null;
|
||||||
private OwnCloudAccount mAccount;
|
private OwnCloudAccount mAccount;
|
||||||
private final ConnectionValidator mConnectionValidator;
|
private final ConnectionValidator mConnectionValidator;
|
||||||
private Object mRequestMutex = new Object();
|
private Object mRequestMutex = new Object();
|
||||||
@ -129,7 +130,6 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
Timber.d("Executing in request with id %s", requestId);
|
Timber.d("Executing in request with id %s", requestId);
|
||||||
method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID, requestId);
|
method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID, requestId);
|
||||||
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
|
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
|
||||||
method.setRequestHeader(HttpConstants.ACCEPT_LANGUAGE_HEADER, Locale.getDefault().getLanguage());
|
|
||||||
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
|
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
|
||||||
if (mCredentials.getHeaderAuth() != null && !mCredentials.getHeaderAuth().isEmpty()) {
|
if (mCredentials.getHeaderAuth() != null && !mCredentials.getHeaderAuth().isEmpty()) {
|
||||||
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
|
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
|
||||||
@ -185,7 +185,7 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
return (mCredentials instanceof OwnCloudAnonymousCredentials || mAccount == null)
|
return (mCredentials instanceof OwnCloudAnonymousCredentials || mAccount == null)
|
||||||
? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0)
|
? Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0)
|
||||||
: Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0 + AccountUtils.getUserId(
|
: Uri.parse(mBaseUri + WEBDAV_FILES_PATH_4_0 + AccountUtils.getUserId(
|
||||||
mAccount.getSavedAccount(), getContext()
|
mAccount.getSavedAccount(), getContext()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -194,7 +194,7 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
return mCredentials instanceof OwnCloudAnonymousCredentials
|
return mCredentials instanceof OwnCloudAnonymousCredentials
|
||||||
? Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0)
|
? Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0)
|
||||||
: Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0 + AccountUtils.getUserId(
|
: Uri.parse(mBaseUri + WEBDAV_UPLOADS_PATH_4_0 + AccountUtils.getUserId(
|
||||||
mAccount.getSavedAccount(), getContext()
|
mAccount.getSavedAccount(), getContext()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -241,6 +241,14 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
HttpUrl.parse(mBaseUri.toString()));
|
HttpUrl.parse(mBaseUri.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public OwnCloudVersion getOwnCloudVersion() {
|
||||||
|
return mVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwnCloudVersion(OwnCloudVersion version) {
|
||||||
|
mVersion = version;
|
||||||
|
}
|
||||||
|
|
||||||
public OwnCloudAccount getAccount() {
|
public OwnCloudAccount getAccount() {
|
||||||
return mAccount;
|
return mAccount;
|
||||||
}
|
}
|
||||||
@ -252,4 +260,4 @@ public class OwnCloudClient extends HttpClient {
|
|||||||
public void setFollowRedirects(boolean followRedirects) {
|
public void setFollowRedirects(boolean followRedirects) {
|
||||||
this.mFollowRedirects = followRedirects;
|
this.mFollowRedirects = followRedirects;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,6 +31,7 @@ import android.net.Uri;
|
|||||||
|
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||||
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
|
import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpClient;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -123,24 +124,6 @@ public class SingleSessionManager {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Timber.v("reusing client for account %s", accountName);
|
Timber.v("reusing client for account %s", accountName);
|
||||||
if (client.getAccount() != null &&
|
|
||||||
client.getAccount().getCredentials() != null &&
|
|
||||||
(client.getAccount().getCredentials().getAuthToken() == null || client.getAccount().getCredentials().getAuthToken().isEmpty())
|
|
||||||
) {
|
|
||||||
Timber.i("Client " + client.getAccount().getName() + " needs to refresh credentials");
|
|
||||||
|
|
||||||
//the next two lines are a hack because okHttpclient is used as a singleton instead of being an
|
|
||||||
//injected instance that can be deleted when required
|
|
||||||
client.clearCookies();
|
|
||||||
client.clearCredentials();
|
|
||||||
|
|
||||||
client.setAccount(account);
|
|
||||||
|
|
||||||
account.loadCredentials(context);
|
|
||||||
client.setCredentials(account.getCredentials());
|
|
||||||
|
|
||||||
Timber.i("Client " + account.getName() + " with credentials size" + client.getAccount().getCredentials().getAuthToken().length());
|
|
||||||
}
|
|
||||||
reusingKnown = true;
|
reusingKnown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,26 @@ public class AccountUtils {
|
|||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the stored server version corresponding to an OC account.
|
||||||
|
*
|
||||||
|
* @param account An OC account
|
||||||
|
* @param context Application context
|
||||||
|
* @return Version of the OC server, according to last check
|
||||||
|
*/
|
||||||
|
public static OwnCloudVersion getServerVersionForAccount(Account account, Context context) {
|
||||||
|
AccountManager ama = AccountManager.get(context);
|
||||||
|
OwnCloudVersion version = null;
|
||||||
|
try {
|
||||||
|
String versionString = ama.getUserData(account, Constants.KEY_OC_VERSION);
|
||||||
|
version = new OwnCloudVersion(versionString);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Timber.e(e, "Couldn't get a the server version for an account");
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
@ -112,7 +132,6 @@ public class AccountUtils {
|
|||||||
String username = AccountUtils.getUsernameForAccount(account);
|
String username = AccountUtils.getUsernameForAccount(account);
|
||||||
|
|
||||||
if (isOauth2) {
|
if (isOauth2) {
|
||||||
Timber.i("Trying to retrieve credentials for oAuth account" + account.name);
|
|
||||||
String accessToken = am.blockingGetAuthToken(
|
String accessToken = am.blockingGetAuthToken(
|
||||||
account,
|
account,
|
||||||
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
|
AccountTypeUtils.getAuthTokenTypeAccessToken(account.type),
|
||||||
@ -190,6 +209,11 @@ public class AccountUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Constants {
|
public static class Constants {
|
||||||
|
/**
|
||||||
|
* Version should be 3 numbers separated by dot so it can be parsed by
|
||||||
|
* {@link OwnCloudVersion}
|
||||||
|
*/
|
||||||
|
public static final String KEY_OC_VERSION = "oc_version";
|
||||||
/**
|
/**
|
||||||
* Base url should point to owncloud installation without trailing / ie:
|
* Base url should point to owncloud installation without trailing / ie:
|
||||||
* http://server/path or https://owncloud.server
|
* http://server/path or https://owncloud.server
|
||||||
|
@ -40,7 +40,6 @@ public class HttpConstants {
|
|||||||
public static final String IF_MATCH_HEADER = "If-Match";
|
public static final String IF_MATCH_HEADER = "If-Match";
|
||||||
public static final String IF_NONE_MATCH_HEADER = "If-None-Match";
|
public static final String IF_NONE_MATCH_HEADER = "If-None-Match";
|
||||||
public static final String CONTENT_TYPE_HEADER = "Content-Type";
|
public static final String CONTENT_TYPE_HEADER = "Content-Type";
|
||||||
public static final String ACCEPT_LANGUAGE_HEADER = "Accept-Language";
|
|
||||||
public static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
public static final String CONTENT_LENGTH_HEADER = "Content-Length";
|
||||||
public static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length";
|
public static final String OC_TOTAL_LENGTH_HEADER = "OC-Total-Length";
|
||||||
public static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
public static final String OC_X_OC_MTIME_HEADER = "X-OC-Mtime";
|
||||||
@ -185,7 +184,6 @@ public class HttpConstants {
|
|||||||
public static final int HTTP_LOCKED = 423;
|
public static final int HTTP_LOCKED = 423;
|
||||||
// 424 Failed Dependency (WebDAV - RFC 2518)
|
// 424 Failed Dependency (WebDAV - RFC 2518)
|
||||||
public static final int HTTP_FAILED_DEPENDENCY = 424;
|
public static final int HTTP_FAILED_DEPENDENCY = 424;
|
||||||
public static final int HTTP_TOO_EARLY = 425;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 5xx Client Error
|
* 5xx Client Error
|
||||||
|
@ -51,7 +51,7 @@ class LogInterceptor : Interceptor {
|
|||||||
|
|
||||||
val request = chain.request().also {
|
val request = chain.request().also {
|
||||||
val requestId = it.headers[OC_X_REQUEST_ID]
|
val requestId = it.headers[OC_X_REQUEST_ID]
|
||||||
logHttp(REQUEST, INFO, requestId, "Method: ${it.method} URL: ${it.url}")
|
logHttp(REQUEST, INFO, requestId, "Type: ${it.method} URL: ${it.url}")
|
||||||
logHeaders(requestId, it.headers, REQUEST)
|
logHeaders(requestId, it.headers, REQUEST)
|
||||||
logRequestBody(requestId, it.body)
|
logRequestBody(requestId, it.body)
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ class LogInterceptor : Interceptor {
|
|||||||
RESPONSE,
|
RESPONSE,
|
||||||
INFO,
|
INFO,
|
||||||
requestId,
|
requestId,
|
||||||
"Method: ${request.method} URL: ${request.url} Code: ${it.code} Message: ${it.message}"
|
"Code: ${it.code} Message: ${it.message} IsSuccessful: ${it.isSuccessful}"
|
||||||
)
|
)
|
||||||
logHeaders(requestId, it.headers, RESPONSE)
|
logHeaders(requestId, it.headers, RESPONSE)
|
||||||
logResponseBody(requestId, it.body)
|
logResponseBody(requestId, it.body)
|
||||||
|
@ -45,7 +45,7 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
|||||||
var call: Call? = null
|
var call: Call? = null
|
||||||
|
|
||||||
var followRedirects: Boolean = true
|
var followRedirects: Boolean = true
|
||||||
var retryOnConnectionFailure: Boolean = true
|
var retryOnConnectionFailure: Boolean = false
|
||||||
var connectionTimeoutVal: Long? = null
|
var connectionTimeoutVal: Long? = null
|
||||||
var connectionTimeoutUnit: TimeUnit? = null
|
var connectionTimeoutUnit: TimeUnit? = null
|
||||||
var readTimeoutVal: Long? = null
|
var readTimeoutVal: Long? = null
|
||||||
@ -62,8 +62,8 @@ abstract class HttpBaseMethod constructor(url: URL) {
|
|||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
open fun execute(httpClient: HttpClient): Int {
|
open fun execute(httpClient: HttpClient): Int {
|
||||||
val okHttpClient = httpClient.okHttpClient.newBuilder().apply {
|
val okHttpClient = httpClient.okHttpClient.newBuilder().apply {
|
||||||
retryOnConnectionFailure(retryOnConnectionFailure)
|
retryOnConnectionFailure.let { retryOnConnectionFailure(it) }
|
||||||
followRedirects(followRedirects)
|
followRedirects.let { followRedirects(it) }
|
||||||
readTimeoutUnit?.let { unit ->
|
readTimeoutUnit?.let { unit ->
|
||||||
readTimeoutVal?.let { readTimeout(it, unit) }
|
readTimeoutVal?.let { readTimeout(it, unit) }
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ import java.net.URL
|
|||||||
class CopyMethod(
|
class CopyMethod(
|
||||||
val url: URL,
|
val url: URL,
|
||||||
private val destinationUrl: String,
|
private val destinationUrl: String,
|
||||||
val forceOverride: Boolean = false
|
private val forceOverride: Boolean
|
||||||
) : DavMethod(url) {
|
) : DavMethod(url) {
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
public override fun onDavExecute(davResource: DavOCResource): Int {
|
public override fun onDavExecute(davResource: DavOCResource): Int {
|
||||||
|
@ -24,36 +24,12 @@
|
|||||||
package com.owncloud.android.lib.common.http.methods.webdav
|
package com.owncloud.android.lib.common.http.methods.webdav
|
||||||
|
|
||||||
import at.bitfire.dav4jvm.Property
|
import at.bitfire.dav4jvm.Property
|
||||||
|
import at.bitfire.dav4jvm.PropertyUtils.getAllPropSet
|
||||||
import at.bitfire.dav4jvm.PropertyUtils.getQuotaPropset
|
import at.bitfire.dav4jvm.PropertyUtils.getQuotaPropset
|
||||||
import at.bitfire.dav4jvm.property.CreationDate
|
|
||||||
import at.bitfire.dav4jvm.property.DisplayName
|
|
||||||
import at.bitfire.dav4jvm.property.GetContentLength
|
|
||||||
import at.bitfire.dav4jvm.property.GetContentType
|
|
||||||
import at.bitfire.dav4jvm.property.GetETag
|
|
||||||
import at.bitfire.dav4jvm.property.GetLastModified
|
|
||||||
import at.bitfire.dav4jvm.property.OCId
|
|
||||||
import at.bitfire.dav4jvm.property.OCPermissions
|
|
||||||
import at.bitfire.dav4jvm.property.OCPrivatelink
|
|
||||||
import at.bitfire.dav4jvm.property.OCSize
|
|
||||||
import at.bitfire.dav4jvm.property.ResourceType
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.properties.OCShareTypes
|
|
||||||
|
|
||||||
object DavUtils {
|
object DavUtils {
|
||||||
@JvmStatic val allPropSet: Array<Property.Name>
|
@JvmStatic val allPropset: Array<Property.Name>
|
||||||
get() = arrayOf(
|
get() = getAllPropSet()
|
||||||
DisplayName.NAME,
|
|
||||||
GetContentType.NAME,
|
|
||||||
ResourceType.NAME,
|
|
||||||
GetContentLength.NAME,
|
|
||||||
GetLastModified.NAME,
|
|
||||||
CreationDate.NAME,
|
|
||||||
GetETag.NAME,
|
|
||||||
OCPermissions.NAME,
|
|
||||||
OCId.NAME,
|
|
||||||
OCSize.NAME,
|
|
||||||
OCPrivatelink.NAME,
|
|
||||||
OCShareTypes.NAME,
|
|
||||||
)
|
|
||||||
|
|
||||||
val quotaPropSet: Array<Property.Name>
|
val quotaPropSet: Array<Property.Name>
|
||||||
get() = getQuotaPropset()
|
get() = getQuotaPropset()
|
||||||
|
@ -36,7 +36,7 @@ import java.net.URL
|
|||||||
class MoveMethod(
|
class MoveMethod(
|
||||||
url: URL,
|
url: URL,
|
||||||
private val destinationUrl: String,
|
private val destinationUrl: String,
|
||||||
val forceOverride: Boolean = false
|
private val forceOverride: Boolean
|
||||||
) : DavMethod(url) {
|
) : DavMethod(url) {
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
override fun onDavExecute(davResource: DavOCResource): Int {
|
override fun onDavExecute(davResource: DavOCResource): Int {
|
||||||
|
@ -53,7 +53,7 @@ class PropfindMethod(
|
|||||||
depth = depth,
|
depth = depth,
|
||||||
reqProp = propertiesToRequest,
|
reqProp = propertiesToRequest,
|
||||||
listOfHeaders = super.getRequestHeadersAsHashMap(),
|
listOfHeaders = super.getRequestHeadersAsHashMap(),
|
||||||
callback = { response: Response, hrefRelation: HrefRelation ->
|
callback = { response: Response, hrefRelation: HrefRelation? ->
|
||||||
when (hrefRelation) {
|
when (hrefRelation) {
|
||||||
HrefRelation.MEMBER -> members.add(response)
|
HrefRelation.MEMBER -> members.add(response)
|
||||||
HrefRelation.SELF -> this.root = response
|
HrefRelation.SELF -> this.root = response
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.common.http.methods.webdav.properties
|
|
||||||
|
|
||||||
import at.bitfire.dav4jvm.Property
|
|
||||||
import at.bitfire.dav4jvm.XmlUtils
|
|
||||||
import org.xmlpull.v1.XmlPullParser
|
|
||||||
|
|
||||||
class OCShareTypes : ShareTypeListProperty() {
|
|
||||||
|
|
||||||
class Factory : ShareTypeListProperty.Factory() {
|
|
||||||
|
|
||||||
override fun create(parser: XmlPullParser) =
|
|
||||||
create(parser, OCShareTypes())
|
|
||||||
|
|
||||||
override fun getName(): Property.Name = NAME
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
@JvmField
|
|
||||||
val NAME = Property.Name(XmlUtils.NS_OWNCLOUD, "share-types")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.common.http.methods.webdav.properties
|
|
||||||
|
|
||||||
import at.bitfire.dav4jvm.Property
|
|
||||||
import at.bitfire.dav4jvm.PropertyFactory
|
|
||||||
import at.bitfire.dav4jvm.XmlUtils
|
|
||||||
import org.xmlpull.v1.XmlPullParser
|
|
||||||
import java.util.LinkedList
|
|
||||||
|
|
||||||
abstract class ShareTypeListProperty : Property {
|
|
||||||
|
|
||||||
val shareTypes = LinkedList<String>()
|
|
||||||
|
|
||||||
override fun toString() = "share types =[" + shareTypes.joinToString(", ") + "]"
|
|
||||||
|
|
||||||
abstract class Factory : PropertyFactory {
|
|
||||||
|
|
||||||
fun create(parser: XmlPullParser, list: ShareTypeListProperty): ShareTypeListProperty {
|
|
||||||
XmlUtils.readTextPropertyList(parser, Property.Name(XmlUtils.NS_OWNCLOUD, "share-type"), list.shareTypes)
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,114 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.common.network;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Request body that represents a file chunk and include information about the progress when uploading it
|
||||||
|
*
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class ChunkFromFileRequestBody extends FileRequestBody {
|
||||||
|
|
||||||
|
private final FileChannel mChannel;
|
||||||
|
private final long mChunkSize;
|
||||||
|
private long mOffset;
|
||||||
|
private long mTransferred;
|
||||||
|
private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
|
||||||
|
|
||||||
|
public ChunkFromFileRequestBody(File file, MediaType contentType, FileChannel channel, long chunkSize) {
|
||||||
|
super(file, contentType);
|
||||||
|
if (channel == null) {
|
||||||
|
throw new IllegalArgumentException("File may not be null");
|
||||||
|
}
|
||||||
|
if (chunkSize <= 0) {
|
||||||
|
throw new IllegalArgumentException("Chunk size must be greater than zero");
|
||||||
|
}
|
||||||
|
this.mChannel = channel;
|
||||||
|
this.mChunkSize = chunkSize;
|
||||||
|
mOffset = 0;
|
||||||
|
mTransferred = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long contentLength() {
|
||||||
|
try {
|
||||||
|
return Math.min(mChunkSize, mChannel.size() - mChannel.position());
|
||||||
|
} catch (IOException e) {
|
||||||
|
return mChunkSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeTo(BufferedSink sink) {
|
||||||
|
int readCount;
|
||||||
|
Iterator<OnDatatransferProgressListener> it;
|
||||||
|
|
||||||
|
try {
|
||||||
|
mChannel.position(mOffset);
|
||||||
|
long size = mFile.length();
|
||||||
|
if (size == 0) {
|
||||||
|
size = -1;
|
||||||
|
}
|
||||||
|
long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
|
||||||
|
while (mChannel.position() < maxCount) {
|
||||||
|
|
||||||
|
readCount = mChannel.read(mBuffer);
|
||||||
|
|
||||||
|
int bytesToWriteInBuffer = (int) Math.min(readCount, mFile.length() - mTransferred);
|
||||||
|
sink.getBuffer().write(mBuffer.array(), 0, bytesToWriteInBuffer);
|
||||||
|
|
||||||
|
sink.flush();
|
||||||
|
|
||||||
|
mBuffer.clear();
|
||||||
|
if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
|
||||||
|
mTransferred += readCount;
|
||||||
|
}
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
it = mDataTransferListeners.iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next().onTransferProgress(readCount, mTransferred, size, mFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception exception) {
|
||||||
|
Timber.e(exception, "Transferred " + mTransferred + " bytes from a total of " + mFile.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffset(long offset) {
|
||||||
|
this.mOffset = offset;
|
||||||
|
}
|
||||||
|
}
|
@ -1,92 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.common.network
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.resources.files.chunks.ChunkedUploadFromFileSystemOperation.Companion.CHUNK_SIZE
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okio.BufferedSink
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
import java.nio.ByteBuffer
|
|
||||||
import java.nio.channels.FileChannel
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Request body that represents a file chunk and include information about the progress when uploading it
|
|
||||||
*
|
|
||||||
* @author David González Verdugo
|
|
||||||
*/
|
|
||||||
class ChunkFromFileRequestBody(
|
|
||||||
file: File,
|
|
||||||
contentType: MediaType?,
|
|
||||||
private val channel: FileChannel,
|
|
||||||
private val chunkSize: Long = CHUNK_SIZE
|
|
||||||
) : FileRequestBody(file, contentType) {
|
|
||||||
|
|
||||||
private var offset: Long = 0
|
|
||||||
private var alreadyTransferred: Long = 0
|
|
||||||
private val buffer = ByteBuffer.allocate(4_096)
|
|
||||||
|
|
||||||
init {
|
|
||||||
require(chunkSize > 0) { "Chunk size must be greater than zero" }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun contentLength(): Long {
|
|
||||||
return chunkSize.coerceAtMost(channel.size() - channel.position())
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun writeTo(sink: BufferedSink) {
|
|
||||||
var readCount: Int
|
|
||||||
var iterator: Iterator<OnDatatransferProgressListener>
|
|
||||||
try {
|
|
||||||
channel.position(offset)
|
|
||||||
|
|
||||||
val maxCount = (offset + chunkSize).coerceAtMost(channel.size())
|
|
||||||
while (channel.position() < maxCount) {
|
|
||||||
readCount = channel.read(buffer)
|
|
||||||
val bytesToWriteInBuffer = readCount.toLong().coerceAtMost(file.length() - alreadyTransferred).toInt()
|
|
||||||
sink.buffer.write(buffer.array(), 0, bytesToWriteInBuffer)
|
|
||||||
sink.flush()
|
|
||||||
buffer.clear()
|
|
||||||
|
|
||||||
if (alreadyTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
|
|
||||||
alreadyTransferred += readCount.toLong()
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
iterator = dataTransferListeners.iterator()
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
iterator.next().onTransferProgress(readCount.toLong(), alreadyTransferred, file.length(), file.absolutePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
Timber.e(exception, "Transferred " + alreadyTransferred + " bytes from a total of " + file.length())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setOffset(newOffset: Long) {
|
|
||||||
offset = newOffset
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.common.network
|
|
||||||
|
|
||||||
import android.content.ContentResolver
|
|
||||||
import android.net.Uri
|
|
||||||
import android.provider.OpenableColumns
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import okio.BufferedSink
|
|
||||||
import okio.Source
|
|
||||||
import okio.source
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
class ContentUriRequestBody(
|
|
||||||
private val contentResolver: ContentResolver,
|
|
||||||
private val contentUri: Uri
|
|
||||||
) : RequestBody(), ProgressiveDataTransferer {
|
|
||||||
|
|
||||||
private val dataTransferListeners: MutableSet<OnDatatransferProgressListener> = HashSet()
|
|
||||||
|
|
||||||
val fileSize: Long = contentResolver.query(contentUri, null, null, null, null)?.use { cursor ->
|
|
||||||
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
|
|
||||||
cursor.moveToFirst()
|
|
||||||
cursor.getLong(sizeIndex)
|
|
||||||
} ?: -1
|
|
||||||
|
|
||||||
override fun contentType(): MediaType? {
|
|
||||||
val contentType = contentResolver.getType(contentUri) ?: return null
|
|
||||||
return contentType.toMediaTypeOrNull()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun contentLength(): Long {
|
|
||||||
return fileSize
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun writeTo(sink: BufferedSink) {
|
|
||||||
val inputStream = contentResolver.openInputStream(contentUri)
|
|
||||||
?: throw IOException("Couldn't open content URI for reading: $contentUri")
|
|
||||||
|
|
||||||
val previousTime = System.currentTimeMillis()
|
|
||||||
|
|
||||||
sink.writeAndUpdateProgress(inputStream.source())
|
|
||||||
inputStream.source().close()
|
|
||||||
|
|
||||||
val laterTime = System.currentTimeMillis()
|
|
||||||
|
|
||||||
Timber.d("Difference - ${laterTime - previousTime} milliseconds")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun BufferedSink.writeAndUpdateProgress(source: Source) {
|
|
||||||
var iterator: Iterator<OnDatatransferProgressListener>
|
|
||||||
|
|
||||||
try {
|
|
||||||
var totalBytesRead = 0L
|
|
||||||
var read: Long
|
|
||||||
while (source.read(this.buffer, BYTES_TO_READ).also { read = it } != -1L) {
|
|
||||||
totalBytesRead += read
|
|
||||||
this.flush()
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
iterator = dataTransferListeners.iterator()
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
iterator.next().onTransferProgress(read, totalBytesRead, fileSize, contentUri.toString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.add(listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addDatatransferProgressListeners(listeners: MutableCollection<OnDatatransferProgressListener>) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.addAll(listeners)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.remove(listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val BYTES_TO_READ = 4_096L
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,119 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.common.network;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okio.BufferedSink;
|
||||||
|
import okio.Okio;
|
||||||
|
import okio.Source;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Request body that represents a file and include information about the progress when uploading it
|
||||||
|
*
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class FileRequestBody extends RequestBody implements ProgressiveDataTransferer {
|
||||||
|
|
||||||
|
final Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
|
||||||
|
protected File mFile;
|
||||||
|
private MediaType mContentType;
|
||||||
|
|
||||||
|
public FileRequestBody(File file, MediaType contentType) {
|
||||||
|
mFile = file;
|
||||||
|
mContentType = contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isOneShot() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MediaType contentType() {
|
||||||
|
return mContentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long contentLength() {
|
||||||
|
return mFile.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeTo(BufferedSink sink) {
|
||||||
|
Source source;
|
||||||
|
Iterator<OnDatatransferProgressListener> it;
|
||||||
|
try {
|
||||||
|
source = Okio.source(mFile);
|
||||||
|
|
||||||
|
long transferred = 0;
|
||||||
|
long read;
|
||||||
|
|
||||||
|
while ((read = source.read(sink.buffer(), 4096)) != -1) {
|
||||||
|
transferred += read;
|
||||||
|
sink.flush();
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
it = mDataTransferListeners.iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next().onTransferProgress(read, transferred, mFile.length(), mFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.d("File with name " + mFile.getName() + " and size " + mFile.length() + " written in request body");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Timber.e(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.addAll(listeners);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,97 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.common.network
|
|
||||||
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import okio.BufferedSink
|
|
||||||
import okio.Source
|
|
||||||
import okio.source
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
import java.util.HashSet
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Request body that represents a file and include information about the progress when uploading it
|
|
||||||
*
|
|
||||||
* @author David González Verdugo
|
|
||||||
*/
|
|
||||||
open class FileRequestBody(
|
|
||||||
val file: File,
|
|
||||||
private val contentType: MediaType?,
|
|
||||||
) : RequestBody(), ProgressiveDataTransferer {
|
|
||||||
|
|
||||||
val dataTransferListeners: MutableSet<OnDatatransferProgressListener> = HashSet()
|
|
||||||
|
|
||||||
override fun isOneShot(): Boolean = true
|
|
||||||
|
|
||||||
override fun contentType(): MediaType? = contentType
|
|
||||||
|
|
||||||
override fun contentLength(): Long = file.length()
|
|
||||||
|
|
||||||
override fun writeTo(sink: BufferedSink) {
|
|
||||||
val source: Source
|
|
||||||
var it: Iterator<OnDatatransferProgressListener>
|
|
||||||
try {
|
|
||||||
source = file.source()
|
|
||||||
var transferred: Long = 0
|
|
||||||
var read: Long
|
|
||||||
while (source.read(sink.buffer, BYTES_TO_READ).also { read = it } != -1L) {
|
|
||||||
transferred += read
|
|
||||||
sink.flush()
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
it = dataTransferListeners.iterator()
|
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next().onTransferProgress(read, transferred, file.length(), file.absolutePath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Timber.d("File with name ${file.name} and size ${file.length()} written in request body")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.add(listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addDatatransferProgressListeners(listeners: Collection<OnDatatransferProgressListener>) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.addAll(listeners)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
dataTransferListeners.remove(listener)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val BYTES_TO_READ = 4_096L
|
|
||||||
}
|
|
||||||
}
|
|
@ -46,7 +46,6 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.ProtocolException;
|
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
@ -167,10 +166,7 @@ public class RemoteOperationResult<T>
|
|||||||
} else if (e instanceof FileNotFoundException) {
|
} else if (e instanceof FileNotFoundException) {
|
||||||
mCode = ResultCode.LOCAL_FILE_NOT_FOUND;
|
mCode = ResultCode.LOCAL_FILE_NOT_FOUND;
|
||||||
|
|
||||||
} else if (e instanceof ProtocolException) {
|
} else {
|
||||||
mCode = ResultCode.NETWORK_ERROR;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mCode = ResultCode.UNKNOWN_ERROR;
|
mCode = ResultCode.UNKNOWN_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -241,10 +237,6 @@ public class RemoteOperationResult<T>
|
|||||||
httpMethod.getResponseBodyAsString(),
|
httpMethod.getResponseBodyAsString(),
|
||||||
ResultCode.SPECIFIC_METHOD_NOT_ALLOWED
|
ResultCode.SPECIFIC_METHOD_NOT_ALLOWED
|
||||||
);
|
);
|
||||||
break;
|
|
||||||
case HttpConstants.HTTP_TOO_EARLY:
|
|
||||||
mCode = ResultCode.TOO_EARLY;
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -591,8 +583,6 @@ public class RemoteOperationResult<T>
|
|||||||
SPECIFIC_SERVICE_UNAVAILABLE,
|
SPECIFIC_SERVICE_UNAVAILABLE,
|
||||||
SPECIFIC_UNSUPPORTED_MEDIA_TYPE,
|
SPECIFIC_UNSUPPORTED_MEDIA_TYPE,
|
||||||
SPECIFIC_METHOD_NOT_ALLOWED,
|
SPECIFIC_METHOD_NOT_ALLOWED,
|
||||||
SPECIFIC_BAD_REQUEST,
|
SPECIFIC_BAD_REQUEST
|
||||||
TOO_EARLY,
|
|
||||||
NETWORK_ERROR,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ data class CommonOcsResponse<T>(
|
|||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class OCSResponse<T>(
|
data class OCSResponse<T>(
|
||||||
val meta: MetaData,
|
val meta: MetaData,
|
||||||
val data: T?
|
val data: T
|
||||||
)
|
)
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
|
@ -1,108 +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.resources.appregistry
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.squareup.moshi.Json
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import okhttp3.FormBody
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
class CreateRemoteFileWithAppProviderOperation(
|
|
||||||
private val createFileWithAppProviderEndpoint: String,
|
|
||||||
private val parentContainerId: String,
|
|
||||||
private val filename: String,
|
|
||||||
) : RemoteOperation<String>() {
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
|
|
||||||
return try {
|
|
||||||
|
|
||||||
val createFileWithAppProviderRequestBody = CreateFileWithAppProviderParams(parentContainerId, filename)
|
|
||||||
.toRequestBody()
|
|
||||||
|
|
||||||
val stringUrl = client.baseUri.toString() + WebdavUtils.encodePath(createFileWithAppProviderEndpoint)
|
|
||||||
|
|
||||||
val postMethod = PostMethod(URL(stringUrl), createFileWithAppProviderRequestBody).apply {
|
|
||||||
setReadTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
|
||||||
setConnectionTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(postMethod)
|
|
||||||
Timber.d("Create file $filename with app provider in folder with ID $parentContainerId - $status${if (!isSuccess(status)) "(FAIL)" else ""}")
|
|
||||||
|
|
||||||
if (isSuccess(status)) RemoteOperationResult<String>(ResultCode.OK).apply {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
val adapter: JsonAdapter<CreateFileWithAppProviderResponse> = moshi.adapter(CreateFileWithAppProviderResponse::class.java)
|
|
||||||
|
|
||||||
data = postMethod.getResponseBodyAsString()?.let { adapter.fromJson(it)!!.fileId }
|
|
||||||
}
|
|
||||||
else RemoteOperationResult<String>(postMethod).apply { data = "" }
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
val result = RemoteOperationResult<String>(e)
|
|
||||||
Timber.e(e, "Create file $filename with app provider in folder with ID $parentContainerId failed")
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
|
|
||||||
|
|
||||||
data class CreateFileWithAppProviderParams(
|
|
||||||
val parentContainerId: String,
|
|
||||||
val filename: String,
|
|
||||||
) {
|
|
||||||
fun toRequestBody(): RequestBody =
|
|
||||||
FormBody.Builder()
|
|
||||||
.add(PARAM_PARENT_CONTAINER_ID, parentContainerId)
|
|
||||||
.add(PARAM_FILENAME, filename)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val PARAM_PARENT_CONTAINER_ID = "parent_container_id"
|
|
||||||
const val PARAM_FILENAME = "filename"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class CreateFileWithAppProviderResponse(
|
|
||||||
@Json(name = "file_id")
|
|
||||||
val fileId: String,
|
|
||||||
)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val TIMEOUT: Long = 5_000
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* @author Abel García de Prada
|
|
||||||
*
|
|
||||||
* 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.resources.appregistry
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class GetRemoteAppRegistryOperation(private val appUrl: String?) : RemoteOperation<AppRegistryResponse>() {
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<AppRegistryResponse> {
|
|
||||||
var result: RemoteOperationResult<AppRegistryResponse>
|
|
||||||
|
|
||||||
try {
|
|
||||||
val uriBuilder = client.baseUri.buildUpon().apply {
|
|
||||||
appendEncodedPath(appUrl)
|
|
||||||
}
|
|
||||||
val getMethod = GetMethod(URL(uriBuilder.build().toString()))
|
|
||||||
val status = client.executeHttpMethod(getMethod)
|
|
||||||
|
|
||||||
val response = getMethod.getResponseBodyAsString()
|
|
||||||
|
|
||||||
if (status == HttpConstants.HTTP_OK) {
|
|
||||||
Timber.d("Successful response $response")
|
|
||||||
|
|
||||||
// Parse the response
|
|
||||||
val moshi: Moshi = Moshi.Builder().build()
|
|
||||||
val adapter: JsonAdapter<AppRegistryResponse> = moshi.adapter(AppRegistryResponse::class.java)
|
|
||||||
val appRegistryResponse: AppRegistryResponse = response?.let { adapter.fromJson(it) } ?: AppRegistryResponse(value = emptyList())
|
|
||||||
|
|
||||||
result = RemoteOperationResult(OK)
|
|
||||||
result.data = appRegistryResponse
|
|
||||||
|
|
||||||
Timber.d("Get AppRegistry completed and parsed to ${result.data}")
|
|
||||||
} else {
|
|
||||||
result = RemoteOperationResult(getMethod)
|
|
||||||
Timber.e("Failed response while getting app registry from the server status code: $status; response message: $response")
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
result = RemoteOperationResult(e)
|
|
||||||
Timber.e(e, "Exception while getting app registry")
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,107 +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.resources.appregistry
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import okhttp3.FormBody
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
class GetUrlToOpenInWebRemoteOperation(
|
|
||||||
private val openWithWebEndpoint: String,
|
|
||||||
private val fileId: String,
|
|
||||||
private val appName: String,
|
|
||||||
) : RemoteOperation<String>() {
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
|
|
||||||
return try {
|
|
||||||
|
|
||||||
val openInWebRequestBody = OpenInWebParams(fileId, appName).toRequestBody()
|
|
||||||
|
|
||||||
val stringUrl =
|
|
||||||
client.baseUri.toString() + WebdavUtils.encodePath(openWithWebEndpoint)
|
|
||||||
|
|
||||||
val postMethod = PostMethod(URL(stringUrl), openInWebRequestBody).apply {
|
|
||||||
setReadTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
|
||||||
setConnectionTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(postMethod)
|
|
||||||
Timber.d("Open in web for file: $fileId - $status${if (!isSuccess(status)) "(FAIL)" else ""}")
|
|
||||||
|
|
||||||
if (isSuccess(status)) RemoteOperationResult<String>(ResultCode.OK).apply {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
val adapter: JsonAdapter<OpenInWebResponse> = moshi.adapter(OpenInWebResponse::class.java)
|
|
||||||
|
|
||||||
data = postMethod.getResponseBodyAsString()?.let { adapter.fromJson(it)!!.uri }
|
|
||||||
}
|
|
||||||
else RemoteOperationResult<String>(postMethod).apply { data = "" }
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
val result = RemoteOperationResult<String>(e)
|
|
||||||
Timber.e(e, "Open in web for file: $fileId failed")
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
|
|
||||||
|
|
||||||
data class OpenInWebParams(
|
|
||||||
val fileId: String,
|
|
||||||
val appName: String,
|
|
||||||
) {
|
|
||||||
fun toRequestBody(): RequestBody =
|
|
||||||
FormBody.Builder()
|
|
||||||
.add(PARAM_FILE_ID, fileId)
|
|
||||||
.add(PARAM_APP_NAME, appName)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val PARAM_FILE_ID = "file_id"
|
|
||||||
const val PARAM_APP_NAME = "app_name"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class OpenInWebResponse(val uri: String)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
/**
|
|
||||||
* Maximum time to wait for a response from the server in milliseconds.
|
|
||||||
*/
|
|
||||||
private const val TIMEOUT = 5_000L
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* @author Abel García de Prada
|
|
||||||
*
|
|
||||||
* 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.resources.appregistry.responses
|
|
||||||
|
|
||||||
import com.squareup.moshi.Json
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class AppRegistryResponse(
|
|
||||||
@Json(name = "mime-types")
|
|
||||||
val value: List<AppRegistryMimeTypeResponse>
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class AppRegistryMimeTypeResponse(
|
|
||||||
@Json(name = "mime_type") val mimeType: String,
|
|
||||||
val ext: String? = null,
|
|
||||||
@Json(name = "app_providers")
|
|
||||||
val appProviders: List<AppRegistryProviderResponse>,
|
|
||||||
val name: String? = null,
|
|
||||||
val icon: String? = null,
|
|
||||||
val description: String? = null,
|
|
||||||
@Json(name = "allow_creation")
|
|
||||||
val allowCreation: Boolean? = null,
|
|
||||||
@Json(name = "default_application")
|
|
||||||
val defaultApplication: String? = null
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class AppRegistryProviderResponse(
|
|
||||||
val name: String,
|
|
||||||
val icon: String,
|
|
||||||
)
|
|
@ -1,44 +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.resources.appregistry.services
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.Service
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
|
|
||||||
|
|
||||||
interface AppRegistryService : Service {
|
|
||||||
fun getAppRegistry(appUrl: String?): RemoteOperationResult<AppRegistryResponse>
|
|
||||||
|
|
||||||
fun getUrlToOpenInWeb(
|
|
||||||
openWebEndpoint: String,
|
|
||||||
fileId: String,
|
|
||||||
appName: String,
|
|
||||||
): RemoteOperationResult<String>
|
|
||||||
|
|
||||||
fun createFileWithAppProvider(
|
|
||||||
createFileWithAppProviderEndpoint: String,
|
|
||||||
parentContainerId: String,
|
|
||||||
filename: String,
|
|
||||||
): RemoteOperationResult<String>
|
|
||||||
}
|
|
@ -1,54 +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.resources.appregistry.services
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.CreateRemoteFileWithAppProviderOperation
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.GetRemoteAppRegistryOperation
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.GetUrlToOpenInWebRemoteOperation
|
|
||||||
import com.owncloud.android.lib.resources.appregistry.responses.AppRegistryResponse
|
|
||||||
|
|
||||||
class OCAppRegistryService(override val client: OwnCloudClient) : AppRegistryService {
|
|
||||||
override fun getAppRegistry(appUrl: String?): RemoteOperationResult<AppRegistryResponse> =
|
|
||||||
GetRemoteAppRegistryOperation(appUrl).execute(client)
|
|
||||||
|
|
||||||
override fun getUrlToOpenInWeb(openWebEndpoint: String, fileId: String, appName: String): RemoteOperationResult<String> =
|
|
||||||
GetUrlToOpenInWebRemoteOperation(
|
|
||||||
openWithWebEndpoint = openWebEndpoint,
|
|
||||||
fileId = fileId,
|
|
||||||
appName = appName
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun createFileWithAppProvider(
|
|
||||||
createFileWithAppProviderEndpoint: String,
|
|
||||||
parentContainerId: String,
|
|
||||||
filename: String
|
|
||||||
): RemoteOperationResult<String> =
|
|
||||||
CreateRemoteFileWithAppProviderOperation(
|
|
||||||
createFileWithAppProviderEndpoint = createFileWithAppProviderEndpoint,
|
|
||||||
parentContainerId = parentContainerId,
|
|
||||||
filename = filename,
|
|
||||||
).execute(client)
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2023 ownCloud GmbH.
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -25,7 +25,7 @@ package com.owncloud.android.lib.resources.files
|
|||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
import com.owncloud.android.lib.common.http.HttpConstants
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils.allPropSet
|
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils.allPropset
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
|
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
import com.owncloud.android.lib.common.network.WebdavUtils
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
import com.owncloud.android.lib.common.operations.RemoteOperation
|
||||||
@ -41,7 +41,6 @@ import java.util.concurrent.TimeUnit
|
|||||||
* @author David A. Velasco
|
* @author David A. Velasco
|
||||||
* @author David González Verdugo
|
* @author David González Verdugo
|
||||||
* @author Abel García de Prada
|
* @author Abel García de Prada
|
||||||
* @author Juan Carlos Garrote Gascón
|
|
||||||
*
|
*
|
||||||
* @param remotePath Path to append to the URL owned by the client instance.
|
* @param remotePath Path to append to the URL owned by the client instance.
|
||||||
* @param isUserLoggedIn When `true`, the username won't be added at the end of the PROPFIND url since is not
|
* @param isUserLoggedIn When `true`, the username won't be added at the end of the PROPFIND url since is not
|
||||||
@ -49,21 +48,21 @@ import java.util.concurrent.TimeUnit
|
|||||||
*/
|
*/
|
||||||
class CheckPathExistenceRemoteOperation(
|
class CheckPathExistenceRemoteOperation(
|
||||||
val remotePath: String? = "",
|
val remotePath: String? = "",
|
||||||
val isUserLoggedIn: Boolean,
|
val isUserLoggedIn: Boolean
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Boolean>() {
|
) : RemoteOperation<Boolean>() {
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> {
|
override fun run(client: OwnCloudClient): RemoteOperationResult<Boolean> {
|
||||||
val baseStringUrl = spaceWebDavUrl ?: if (isUserLoggedIn) client.userFilesWebDavUri.toString() else client.baseFilesWebDavUri.toString()
|
|
||||||
val stringUrl = if (isUserLoggedIn) baseStringUrl + WebdavUtils.encodePath(remotePath) else baseStringUrl
|
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
val propFindMethod = PropfindMethod(URL(stringUrl), 0, allPropSet).apply {
|
val stringUrl =
|
||||||
|
if (isUserLoggedIn) client.baseFilesWebDavUri.toString()
|
||||||
|
else client.userFilesWebDavUri.toString() + WebdavUtils.encodePath(remotePath)
|
||||||
|
|
||||||
|
val propFindMethod = PropfindMethod(URL(stringUrl), 0, allPropset).apply {
|
||||||
setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
|
setReadTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
|
||||||
setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
|
setConnectionTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
|
||||||
}
|
}
|
||||||
|
|
||||||
val status = client.executeHttpMethod(propFindMethod)
|
var status = client.executeHttpMethod(propFindMethod)
|
||||||
/* PROPFIND method
|
/* PROPFIND method
|
||||||
* 404 NOT FOUND: path doesn't exist,
|
* 404 NOT FOUND: path doesn't exist,
|
||||||
* 207 MULTI_STATUS: path exists.
|
* 207 MULTI_STATUS: path exists.
|
||||||
@ -78,9 +77,8 @@ class CheckPathExistenceRemoteOperation(
|
|||||||
val result = RemoteOperationResult<Boolean>(e)
|
val result = RemoteOperationResult<Boolean>(e)
|
||||||
Timber.e(
|
Timber.e(
|
||||||
e,
|
e,
|
||||||
"Existence check for $stringUrl : ${result.logMessage}"
|
"Existence check for ${client.userFilesWebDavUri}${WebdavUtils.encodePath(remotePath)} : ${result.logMessage}"
|
||||||
)
|
)
|
||||||
result.data = false
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,130 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.CopyMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation moving a remote file or folder in the ownCloud server to a different folder
|
||||||
|
* in the same account.
|
||||||
|
*
|
||||||
|
* Allows renaming the moving file/folder at the same time.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author Christian Schabesberger
|
||||||
|
* @author David González V.
|
||||||
|
*/
|
||||||
|
public class CopyRemoteFileOperation extends RemoteOperation<String> {
|
||||||
|
|
||||||
|
private static final int COPY_READ_TIMEOUT = 600000;
|
||||||
|
private static final int COPY_CONNECTION_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
private String mSrcRemotePath;
|
||||||
|
private String mTargetRemotePath;
|
||||||
|
|
||||||
|
private boolean mOverwrite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* <p/>
|
||||||
|
* TODO Paths should finish in "/" in the case of folders. ?
|
||||||
|
*
|
||||||
|
* @param srcRemotePath Remote path of the file/folder to move.
|
||||||
|
* @param targetRemotePath Remove path desired for the file/folder after moving it.
|
||||||
|
*/
|
||||||
|
public CopyRemoteFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite
|
||||||
|
) {
|
||||||
|
mSrcRemotePath = srcRemotePath;
|
||||||
|
mTargetRemotePath = targetRemotePath;
|
||||||
|
mOverwrite = overwrite;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the rename operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult<String> run(OwnCloudClient client) {
|
||||||
|
|
||||||
|
if (mTargetRemotePath.equals(mSrcRemotePath)) {
|
||||||
|
// nothing to do!
|
||||||
|
return new RemoteOperationResult<>(ResultCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
|
||||||
|
return new RemoteOperationResult<>(ResultCode.INVALID_COPY_INTO_DESCENDANT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// perform remote operation
|
||||||
|
RemoteOperationResult result;
|
||||||
|
try {
|
||||||
|
CopyMethod copyMethod =
|
||||||
|
new CopyMethod(
|
||||||
|
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mSrcRemotePath)),
|
||||||
|
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
|
||||||
|
mOverwrite);
|
||||||
|
|
||||||
|
copyMethod.setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
copyMethod.setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
final int status = client.executeHttpMethod(copyMethod);
|
||||||
|
|
||||||
|
if (status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT) {
|
||||||
|
String fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID);
|
||||||
|
result = new RemoteOperationResult<>(ResultCode.OK);
|
||||||
|
result.setData(fileRemoteId);
|
||||||
|
} else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !mOverwrite) {
|
||||||
|
result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
|
||||||
|
client.exhaustResponse(copyMethod.getResponseBodyAsStream());
|
||||||
|
|
||||||
|
/// for other errors that could be explicitly handled, check first:
|
||||||
|
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
|
||||||
|
} else {
|
||||||
|
|
||||||
|
result = new RemoteOperationResult<>(copyMethod);
|
||||||
|
client.exhaustResponse(copyMethod.getResponseBodyAsStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.i("Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Copy " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -1,133 +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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.CopyMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation copying a remote file or folder in the ownCloud server to a different folder
|
|
||||||
* in the same account.
|
|
||||||
*
|
|
||||||
* Allows renaming the copying file/folder at the same time.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author Christian Schabesberger
|
|
||||||
* @author David González V.
|
|
||||||
* @author Juan Carlos Garrote Gascón
|
|
||||||
* @author Manuel Plazas Palacio
|
|
||||||
*
|
|
||||||
* @param sourceRemotePath Remote path of the file/folder to copy.
|
|
||||||
* @param targetRemotePath Remote path desired for the file/folder to copy it.
|
|
||||||
*/
|
|
||||||
class CopyRemoteFileOperation(
|
|
||||||
private val sourceRemotePath: String,
|
|
||||||
private val targetRemotePath: String,
|
|
||||||
private val sourceSpaceWebDavUrl: String? = null,
|
|
||||||
private val targetSpaceWebDavUrl: String? = null,
|
|
||||||
private val forceOverride: Boolean = false,
|
|
||||||
) : RemoteOperation<String>() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs the rename operation.
|
|
||||||
*
|
|
||||||
* @param client Client object to communicate with the remote ownCloud server.
|
|
||||||
*/
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<String> {
|
|
||||||
if (targetRemotePath == sourceRemotePath && sourceSpaceWebDavUrl == targetSpaceWebDavUrl) {
|
|
||||||
// nothing to do!
|
|
||||||
return RemoteOperationResult(ResultCode.OK)
|
|
||||||
}
|
|
||||||
if (targetRemotePath.startsWith(sourceRemotePath) && sourceSpaceWebDavUrl == targetSpaceWebDavUrl) {
|
|
||||||
return RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// perform remote operation
|
|
||||||
var result: RemoteOperationResult<String>
|
|
||||||
try {
|
|
||||||
val copyMethod = CopyMethod(
|
|
||||||
url = URL((sourceSpaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(sourceRemotePath)),
|
|
||||||
destinationUrl = (targetSpaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(targetRemotePath),
|
|
||||||
forceOverride = forceOverride,
|
|
||||||
).apply {
|
|
||||||
addRequestHeaders(this)
|
|
||||||
setReadTimeout(COPY_READ_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
setConnectionTimeout(COPY_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
}
|
|
||||||
val status = client.executeHttpMethod(copyMethod)
|
|
||||||
when {
|
|
||||||
isSuccess(status) -> {
|
|
||||||
val fileRemoteId = copyMethod.getResponseHeader(HttpConstants.OC_FILE_REMOTE_ID)
|
|
||||||
result = RemoteOperationResult(ResultCode.OK)
|
|
||||||
result.setData(fileRemoteId)
|
|
||||||
}
|
|
||||||
|
|
||||||
isPreconditionFailed(status) -> {
|
|
||||||
result = RemoteOperationResult(ResultCode.INVALID_OVERWRITE)
|
|
||||||
client.exhaustResponse(copyMethod.getResponseBodyAsStream())
|
|
||||||
|
|
||||||
/// for other errors that could be explicitly handled, check first:
|
|
||||||
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
result = RemoteOperationResult(copyMethod)
|
|
||||||
client.exhaustResponse(copyMethod.getResponseBodyAsStream())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Timber.i("Copy $sourceRemotePath to $targetRemotePath: ${result.logMessage}")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
result = RemoteOperationResult(e)
|
|
||||||
Timber.e(e, "Copy $sourceRemotePath to $targetRemotePath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun addRequestHeaders(copyMethod: CopyMethod) {
|
|
||||||
//Adding this because the library has an error with override
|
|
||||||
if (copyMethod.forceOverride) {
|
|
||||||
copyMethod.setRequestHeader(OVERWRITE, TRUE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT)
|
|
||||||
|
|
||||||
private fun isPreconditionFailed(status: Int) = status == HttpConstants.HTTP_PRECONDITION_FAILED
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val COPY_READ_TIMEOUT = 10L
|
|
||||||
private const val COPY_CONNECTION_TIMEOUT = 6L
|
|
||||||
private const val OVERWRITE = "overwrite"
|
|
||||||
private const val TRUE = "T"
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,114 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2019 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.resources.files;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.MkColMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the creation of a new folder in the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
*/
|
||||||
|
public class CreateRemoteFolderOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
private static final int READ_TIMEOUT = 30000;
|
||||||
|
private static final int CONNECTION_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
private String mRemotePath;
|
||||||
|
private boolean mCreateFullPath;
|
||||||
|
protected boolean createChunksFolder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath Full path to the new directory to create in the remote server.
|
||||||
|
* @param createFullPath 'True' means that all the ancestor folders should be created.
|
||||||
|
*/
|
||||||
|
public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) {
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
mCreateFullPath = createFullPath;
|
||||||
|
createChunksFolder = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the operation
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult result = createFolder(client);
|
||||||
|
if (!result.isSuccess() && mCreateFullPath &&
|
||||||
|
RemoteOperationResult.ResultCode.CONFLICT == result.getCode()) {
|
||||||
|
result = createParentFolder(FileUtils.getParentPath(mRemotePath), client);
|
||||||
|
if (result.isSuccess()) {
|
||||||
|
result = createFolder(client); // second (and last) try
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private RemoteOperationResult createFolder(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult result;
|
||||||
|
try {
|
||||||
|
Uri webDavUri = createChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
|
||||||
|
final MkColMethod mkcol = new MkColMethod(
|
||||||
|
new URL(webDavUri + WebdavUtils.encodePath(mRemotePath)));
|
||||||
|
mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
final int status = client.executeHttpMethod(mkcol);
|
||||||
|
|
||||||
|
result = (status == HttpConstants.HTTP_CREATED)
|
||||||
|
? new RemoteOperationResult<>(ResultCode.OK)
|
||||||
|
: new RemoteOperationResult<>(mkcol);
|
||||||
|
Timber.d("Create directory " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
client.exhaustResponse(mkcol.getResponseBodyAsStream());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Create directory " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private RemoteOperationResult createParentFolder(String parentPath, OwnCloudClient client) {
|
||||||
|
RemoteOperation operation = new CreateRemoteFolderOperation(parentPath, mCreateFullPath);
|
||||||
|
return operation.execute(client);
|
||||||
|
}
|
||||||
|
}
|
@ -1,110 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.MkColMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the creation of a new folder in the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
*
|
|
||||||
* @param remotePath Full path to the new directory to create in the remote server.
|
|
||||||
* @param createFullPath 'True' means that all the ancestor folders should be created.
|
|
||||||
*/
|
|
||||||
class CreateRemoteFolderOperation(
|
|
||||||
val remotePath: String,
|
|
||||||
private val createFullPath: Boolean,
|
|
||||||
private val isChunksFolder: Boolean = false,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
|
|
||||||
var result = createFolder(client)
|
|
||||||
if (!result.isSuccess && createFullPath && result.code == ResultCode.CONFLICT) {
|
|
||||||
result = createParentFolder(FileUtils.getParentPath(remotePath), client)
|
|
||||||
|
|
||||||
if (result.isSuccess) {
|
|
||||||
// Second and last try
|
|
||||||
result = createFolder(client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createFolder(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
try {
|
|
||||||
val webDavUri = if (isChunksFolder) {
|
|
||||||
client.uploadsWebDavUri.toString()
|
|
||||||
} else {
|
|
||||||
spaceWebDavUrl ?: client.userFilesWebDavUri.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
val mkCol = MkColMethod(
|
|
||||||
URL(webDavUri + WebdavUtils.encodePath(remotePath))
|
|
||||||
).apply {
|
|
||||||
setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(mkCol)
|
|
||||||
result =
|
|
||||||
if (status == HttpConstants.HTTP_CREATED) {
|
|
||||||
RemoteOperationResult(ResultCode.OK)
|
|
||||||
} else {
|
|
||||||
RemoteOperationResult(mkCol)
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.d("Create directory $remotePath: ${result.logMessage}")
|
|
||||||
client.exhaustResponse(mkCol.getResponseBodyAsStream())
|
|
||||||
|
|
||||||
} catch (e: Exception) {
|
|
||||||
result = RemoteOperationResult(e)
|
|
||||||
Timber.e(e, "Create directory $remotePath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createParentFolder(parentPath: String, client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
val operation: RemoteOperation<Unit> = CreateRemoteFolderOperation(parentPath, createFullPath)
|
|
||||||
return operation.execute(client)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val READ_TIMEOUT: Long = 30_000
|
|
||||||
private const val CONNECTION_TIMEOUT: Long = 5_000
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,221 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2016 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.resources.files;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.OperationCancelledException;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the download of a remote file in the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class DownloadRemoteFileOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
private static final int FORBIDDEN_ERROR = 403;
|
||||||
|
private static final int SERVICE_UNAVAILABLE_ERROR = 503;
|
||||||
|
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
|
||||||
|
private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
|
||||||
|
private long mModificationTimestamp = 0;
|
||||||
|
private String mEtag = "";
|
||||||
|
private GetMethod mGet;
|
||||||
|
|
||||||
|
private String mRemotePath;
|
||||||
|
private String mLocalFolderPath;
|
||||||
|
|
||||||
|
public DownloadRemoteFileOperation(String remotePath, String localFolderPath) {
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
mLocalFolderPath = localFolderPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult result;
|
||||||
|
|
||||||
|
/// download will be performed to a temporal file, then moved to the final location
|
||||||
|
File tmpFile = new File(getTmpPath());
|
||||||
|
|
||||||
|
/// perform the download
|
||||||
|
try {
|
||||||
|
tmpFile.getParentFile().mkdirs();
|
||||||
|
result = downloadFile(client, tmpFile);
|
||||||
|
Timber.i("Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Download of " + mRemotePath + " to " + getTmpPath() + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private RemoteOperationResult downloadFile(OwnCloudClient client, File targetFile) throws
|
||||||
|
Exception {
|
||||||
|
|
||||||
|
RemoteOperationResult result;
|
||||||
|
int status;
|
||||||
|
boolean savedFile = false;
|
||||||
|
mGet = new GetMethod(new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)));
|
||||||
|
Iterator<OnDatatransferProgressListener> it;
|
||||||
|
|
||||||
|
FileOutputStream fos = null;
|
||||||
|
BufferedInputStream bis = null;
|
||||||
|
try {
|
||||||
|
status = client.executeHttpMethod(mGet);
|
||||||
|
if (isSuccess(status)) {
|
||||||
|
targetFile.createNewFile();
|
||||||
|
bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
|
||||||
|
fos = new FileOutputStream(targetFile);
|
||||||
|
long transferred = 0;
|
||||||
|
|
||||||
|
String contentLength = mGet.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER);
|
||||||
|
long totalToTransfer =
|
||||||
|
(contentLength != null
|
||||||
|
&& contentLength.length() > 0)
|
||||||
|
? Long.parseLong(contentLength)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
byte[] bytes = new byte[4096];
|
||||||
|
int readResult;
|
||||||
|
while ((readResult = bis.read(bytes)) != -1) {
|
||||||
|
synchronized (mCancellationRequested) {
|
||||||
|
if (mCancellationRequested.get()) {
|
||||||
|
mGet.abort();
|
||||||
|
throw new OperationCancelledException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fos.write(bytes, 0, readResult);
|
||||||
|
transferred += readResult;
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
it = mDataTransferListeners.iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next().onTransferProgress(readResult, transferred, totalToTransfer,
|
||||||
|
targetFile.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (transferred == totalToTransfer) { // Check if the file is completed
|
||||||
|
savedFile = true;
|
||||||
|
final String modificationTime =
|
||||||
|
mGet.getResponseHeaders().get("Last-Modified") != null
|
||||||
|
? mGet.getResponseHeaders().get("Last-Modified")
|
||||||
|
: mGet.getResponseHeader("last-modified");
|
||||||
|
|
||||||
|
if (modificationTime != null) {
|
||||||
|
final Date d = WebdavUtils.parseResponseDate(modificationTime);
|
||||||
|
mModificationTimestamp = (d != null) ? d.getTime() : 0;
|
||||||
|
} else {
|
||||||
|
Timber.e("Could not read modification time from response downloading %s", mRemotePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
mEtag = WebdavUtils.getEtagFromResponse(mGet);
|
||||||
|
|
||||||
|
// Get rid of extra quotas
|
||||||
|
mEtag = mEtag.replace("\"", "");
|
||||||
|
|
||||||
|
if (mEtag.length() == 0) {
|
||||||
|
Timber.e("Could not read eTag from response downloading %s", mRemotePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
Timber.e("Content-Length not equal to transferred bytes.");
|
||||||
|
Timber.d("totalToTransfer = %d, transferred = %d", totalToTransfer, transferred);
|
||||||
|
client.exhaustResponse(mGet.getResponseBodyAsStream());
|
||||||
|
// TODO some kind of error control!
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
|
||||||
|
client.exhaustResponse(mGet.getResponseBodyAsStream());
|
||||||
|
|
||||||
|
} // else, body read by RemoteOperationResult constructor
|
||||||
|
|
||||||
|
result = isSuccess(status)
|
||||||
|
? new RemoteOperationResult<>(RemoteOperationResult.ResultCode.OK)
|
||||||
|
: new RemoteOperationResult<>(mGet);
|
||||||
|
} finally {
|
||||||
|
if (fos != null) {
|
||||||
|
fos.close();
|
||||||
|
}
|
||||||
|
if (bis != null) {
|
||||||
|
bis.close();
|
||||||
|
}
|
||||||
|
if (!savedFile && targetFile.exists()) {
|
||||||
|
targetFile.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSuccess(int status) {
|
||||||
|
return (status == HttpConstants.HTTP_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getTmpPath() {
|
||||||
|
return mLocalFolderPath + mRemotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.remove(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
mCancellationRequested.set(true); // atomic set; there is no need of synchronizing it
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getModificationTimestamp() {
|
||||||
|
return mModificationTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEtag() {
|
||||||
|
return mEtag;
|
||||||
|
}
|
||||||
|
}
|
@ -1,185 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.OperationCancelledException
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.BufferedInputStream
|
|
||||||
import java.io.File
|
|
||||||
import java.io.FileOutputStream
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the download of a remote file in the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
*/
|
|
||||||
class DownloadRemoteFileOperation(
|
|
||||||
private val remotePath: String,
|
|
||||||
localFolderPath: String,
|
|
||||||
private val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
private val cancellationRequested = AtomicBoolean(false)
|
|
||||||
private val dataTransferListeners: MutableSet<OnDatatransferProgressListener> = HashSet()
|
|
||||||
|
|
||||||
var modificationTimestamp: Long = 0
|
|
||||||
private set
|
|
||||||
|
|
||||||
var etag: String = ""
|
|
||||||
private set
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
// download will be performed to a temporal file, then moved to the final location
|
|
||||||
val tmpFile = File(tmpPath)
|
|
||||||
|
|
||||||
// perform the download
|
|
||||||
return try {
|
|
||||||
tmpFile.parentFile?.mkdirs()
|
|
||||||
downloadFile(client, tmpFile).also { result ->
|
|
||||||
Timber.i("Download of $remotePath to $tmpPath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
RemoteOperationResult<Unit>(e).also { result ->
|
|
||||||
Timber.e(e, "Download of $remotePath to $tmpPath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(Exception::class)
|
|
||||||
private fun downloadFile(client: OwnCloudClient, targetFile: File): RemoteOperationResult<Unit> {
|
|
||||||
val result: RemoteOperationResult<Unit>
|
|
||||||
var it: Iterator<OnDatatransferProgressListener>
|
|
||||||
var fos: FileOutputStream? = null
|
|
||||||
var bis: BufferedInputStream? = null
|
|
||||||
var savedFile = false
|
|
||||||
|
|
||||||
val webDavUri = spaceWebDavUrl ?: client.userFilesWebDavUri.toString()
|
|
||||||
val getMethod = GetMethod(URL(webDavUri + WebdavUtils.encodePath(remotePath)))
|
|
||||||
|
|
||||||
try {
|
|
||||||
val status = client.executeHttpMethod(getMethod)
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
targetFile.createNewFile()
|
|
||||||
bis = BufferedInputStream(getMethod.getResponseBodyAsStream())
|
|
||||||
fos = FileOutputStream(targetFile)
|
|
||||||
var transferred: Long = 0
|
|
||||||
val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER)
|
|
||||||
val totalToTransfer = if (!contentLength.isNullOrEmpty()) {
|
|
||||||
contentLength.toLong()
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
val bytes = ByteArray(4096)
|
|
||||||
var readResult: Int
|
|
||||||
while (bis.read(bytes).also { readResult = it } != -1) {
|
|
||||||
synchronized(cancellationRequested) {
|
|
||||||
if (cancellationRequested.get()) {
|
|
||||||
getMethod.abort()
|
|
||||||
throw OperationCancelledException()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fos.write(bytes, 0, readResult)
|
|
||||||
transferred += readResult.toLong()
|
|
||||||
synchronized(dataTransferListeners) {
|
|
||||||
it = dataTransferListeners.iterator()
|
|
||||||
while (it.hasNext()) {
|
|
||||||
it.next()
|
|
||||||
.onTransferProgress(readResult.toLong(), transferred, totalToTransfer, targetFile.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (transferred == totalToTransfer) { // Check if the file is completed
|
|
||||||
savedFile = true
|
|
||||||
val modificationTime =
|
|
||||||
getMethod.getResponseHeaders()?.get("Last-Modified")
|
|
||||||
?: getMethod.getResponseHeader("last-modified")
|
|
||||||
|
|
||||||
if (modificationTime != null) {
|
|
||||||
val modificationDate = WebdavUtils.parseResponseDate(modificationTime)
|
|
||||||
modificationTimestamp = modificationDate?.time ?: 0
|
|
||||||
} else {
|
|
||||||
Timber.e("Could not read modification time from response downloading %s", remotePath)
|
|
||||||
}
|
|
||||||
etag = WebdavUtils.getEtagFromResponse(getMethod)
|
|
||||||
|
|
||||||
// Get rid of extra quotas
|
|
||||||
etag = etag.replace("\"", "")
|
|
||||||
if (etag.isEmpty()) {
|
|
||||||
Timber.e("Could not read eTag from response downloading %s", remotePath)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Timber.e("Content-Length not equal to transferred bytes.")
|
|
||||||
Timber.d("totalToTransfer = $totalToTransfer, transferred = $transferred")
|
|
||||||
client.exhaustResponse(getMethod.getResponseBodyAsStream())
|
|
||||||
// TODO some kind of error control!
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (status != HttpConstants.HTTP_FORBIDDEN && status != HttpConstants.HTTP_SERVICE_UNAVAILABLE) {
|
|
||||||
client.exhaustResponse(getMethod.getResponseBodyAsStream())
|
|
||||||
} // else, body read by RemoteOperationResult constructor
|
|
||||||
|
|
||||||
result =
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
|
|
||||||
} else {
|
|
||||||
RemoteOperationResult(getMethod)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
fos?.close()
|
|
||||||
bis?.close()
|
|
||||||
if (!savedFile && targetFile.exists()) {
|
|
||||||
targetFile.delete()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
|
|
||||||
|
|
||||||
private val tmpPath: String = localFolderPath + remotePath
|
|
||||||
|
|
||||||
fun addDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListeners) { dataTransferListeners.add(listener) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener?) {
|
|
||||||
synchronized(dataTransferListeners) { dataTransferListeners.remove(listener) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cancel() {
|
|
||||||
cancellationRequested.set(true) // atomic set; there is no need of synchronizing it
|
|
||||||
}
|
|
||||||
}
|
|
@ -32,7 +32,6 @@ public class FileUtils {
|
|||||||
public static final String FINAL_CHUNKS_FILE = ".file";
|
public static final String FINAL_CHUNKS_FILE = ".file";
|
||||||
public static final String MIME_DIR = "DIR";
|
public static final String MIME_DIR = "DIR";
|
||||||
public static final String MIME_DIR_UNIX = "httpd/unix-directory";
|
public static final String MIME_DIR_UNIX = "httpd/unix-directory";
|
||||||
public static final String MODE_READ_ONLY = "r";
|
|
||||||
|
|
||||||
static String getParentPath(String remotePath) {
|
static String getParentPath(String remotePath) {
|
||||||
String parentPath = new File(remotePath).getParent();
|
String parentPath = new File(remotePath).getParent();
|
||||||
|
@ -44,7 +44,7 @@ class GetBaseUrlRemoteOperation : RemoteOperation<String?>() {
|
|||||||
return try {
|
return try {
|
||||||
val stringUrl = client.baseFilesWebDavUri.toString()
|
val stringUrl = client.baseFilesWebDavUri.toString()
|
||||||
|
|
||||||
val propFindMethod = PropfindMethod(URL(stringUrl), 0, DavUtils.allPropSet).apply {
|
val propFindMethod = PropfindMethod(URL(stringUrl), 0, DavUtils.allPropset).apply {
|
||||||
setReadTimeout(TIMEOUT, TimeUnit.SECONDS)
|
setReadTimeout(TIMEOUT, TimeUnit.SECONDS)
|
||||||
setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS)
|
setConnectionTimeout(TIMEOUT, TimeUnit.SECONDS)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,146 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation moving a remote file or folder in the ownCloud server to a different folder
|
||||||
|
* in the same account.
|
||||||
|
* <p>
|
||||||
|
* Allows renaming the moving file/folder at the same time.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class MoveRemoteFileOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
private static final int MOVE_READ_TIMEOUT = 600000;
|
||||||
|
private static final int MOVE_CONNECTION_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
private String mSrcRemotePath;
|
||||||
|
private String mTargetRemotePath;
|
||||||
|
private boolean mOverwrite;
|
||||||
|
|
||||||
|
protected boolean moveChunkedFile = false;
|
||||||
|
protected String mFileLastModifTimestamp;
|
||||||
|
protected long mFileLength;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* <p>
|
||||||
|
* TODO Paths should finish in "/" in the case of folders. ?
|
||||||
|
*
|
||||||
|
* @param srcRemotePath Remote path of the file/folder to move.
|
||||||
|
* @param targetRemotePath Remote path desired for the file/folder after moving it.
|
||||||
|
*/
|
||||||
|
public MoveRemoteFileOperation(String srcRemotePath,
|
||||||
|
String targetRemotePath,
|
||||||
|
boolean overwrite) {
|
||||||
|
|
||||||
|
mSrcRemotePath = srcRemotePath;
|
||||||
|
mTargetRemotePath = targetRemotePath;
|
||||||
|
mOverwrite = overwrite;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the rename operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
if (mTargetRemotePath.equals(mSrcRemotePath)) {
|
||||||
|
// nothing to do!
|
||||||
|
return new RemoteOperationResult<>(ResultCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mTargetRemotePath.startsWith(mSrcRemotePath)) {
|
||||||
|
return new RemoteOperationResult<>(ResultCode.INVALID_MOVE_INTO_DESCENDANT);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// perform remote operation
|
||||||
|
RemoteOperationResult result;
|
||||||
|
try {
|
||||||
|
// After finishing a chunked upload, we have to move the resulting file from uploads folder to files one,
|
||||||
|
// so this uri has to be customizable
|
||||||
|
Uri srcWebDavUri = moveChunkedFile ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
|
||||||
|
|
||||||
|
final MoveMethod move = new MoveMethod(
|
||||||
|
new URL(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)),
|
||||||
|
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath),
|
||||||
|
mOverwrite);
|
||||||
|
|
||||||
|
if (moveChunkedFile) {
|
||||||
|
move.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
||||||
|
move.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(mFileLength));
|
||||||
|
}
|
||||||
|
|
||||||
|
move.setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
move.setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
final int status = client.executeHttpMethod(move);
|
||||||
|
/// process response
|
||||||
|
if (isSuccess(status)) {
|
||||||
|
result = new RemoteOperationResult<>(ResultCode.OK);
|
||||||
|
} else if (status == HttpConstants.HTTP_PRECONDITION_FAILED && !mOverwrite) {
|
||||||
|
|
||||||
|
result = new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
|
||||||
|
client.exhaustResponse(move.getResponseBodyAsStream());
|
||||||
|
|
||||||
|
/// for other errors that could be explicitly handled, check first:
|
||||||
|
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
|
||||||
|
|
||||||
|
} else {
|
||||||
|
result = new RemoteOperationResult<>(move);
|
||||||
|
client.exhaustResponse(move.getResponseBodyAsStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.i("Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Move " + mSrcRemotePath + " to " + mTargetRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean isSuccess(int status) {
|
||||||
|
return status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT;
|
||||||
|
}
|
||||||
|
}
|
@ -1,149 +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.resources.files
|
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation moving a remote file or folder in the ownCloud server to a different folder
|
|
||||||
* in the same account and space.
|
|
||||||
*
|
|
||||||
* Allows renaming the moving file/folder at the same time.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author David González Verdugo
|
|
||||||
* @author Abel García de Prada
|
|
||||||
* @author Juan Carlos Garrote Gascón
|
|
||||||
* @author Manuel Plazas Palacio
|
|
||||||
*
|
|
||||||
* @param sourceRemotePath Remote path of the file/folder to copy.
|
|
||||||
* @param targetRemotePath Remote path desired for the file/folder to copy it.
|
|
||||||
*/
|
|
||||||
open class MoveRemoteFileOperation(
|
|
||||||
private val sourceRemotePath: String,
|
|
||||||
private val targetRemotePath: String,
|
|
||||||
private val spaceWebDavUrl: String? = null,
|
|
||||||
private val forceOverride: Boolean = false,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs the rename operation.
|
|
||||||
*
|
|
||||||
* @param client Client object to communicate with the remote ownCloud server.
|
|
||||||
*/
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
if (targetRemotePath == sourceRemotePath) {
|
|
||||||
// nothing to do!
|
|
||||||
return RemoteOperationResult(ResultCode.OK)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetRemotePath.startsWith(sourceRemotePath)) {
|
|
||||||
return RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// perform remote operation
|
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
try {
|
|
||||||
// After finishing a chunked upload, we have to move the resulting file from uploads folder to files one,
|
|
||||||
// so this uri has to be customizable
|
|
||||||
val srcWebDavUri = getSrcWebDavUriForClient(client)
|
|
||||||
val moveMethod = MoveMethod(
|
|
||||||
url = URL((spaceWebDavUrl ?: srcWebDavUri.toString()) + WebdavUtils.encodePath(sourceRemotePath)),
|
|
||||||
destinationUrl = (spaceWebDavUrl ?: client.userFilesWebDavUri.toString()) + WebdavUtils.encodePath(targetRemotePath),
|
|
||||||
forceOverride = forceOverride,
|
|
||||||
).apply {
|
|
||||||
addRequestHeaders(this)
|
|
||||||
setReadTimeout(MOVE_READ_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
setConnectionTimeout(MOVE_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(moveMethod)
|
|
||||||
|
|
||||||
when {
|
|
||||||
isSuccess(status) -> {
|
|
||||||
result = RemoteOperationResult<Unit>(ResultCode.OK)
|
|
||||||
}
|
|
||||||
|
|
||||||
isPreconditionFailed(status) -> {
|
|
||||||
result = RemoteOperationResult<Unit>(ResultCode.INVALID_OVERWRITE)
|
|
||||||
client.exhaustResponse(moveMethod.getResponseBodyAsStream())
|
|
||||||
|
|
||||||
/// for other errors that could be explicitly handled, check first:
|
|
||||||
/// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.9.4
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
result = RemoteOperationResult<Unit>(moveMethod)
|
|
||||||
client.exhaustResponse(moveMethod.getResponseBodyAsStream())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.i("Move $sourceRemotePath to $targetRemotePath: ${result.logMessage}")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
result = RemoteOperationResult<Unit>(e)
|
|
||||||
Timber.e(e, "Move $sourceRemotePath to $targetRemotePath: ${result.logMessage}")
|
|
||||||
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For standard moves, we will use [OwnCloudClient.getUserFilesWebDavUri].
|
|
||||||
* In case we need a different source Uri, override this method.
|
|
||||||
*/
|
|
||||||
open fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.userFilesWebDavUri
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For standard moves, we won't need any special headers.
|
|
||||||
* In case new headers are needed, override this method
|
|
||||||
*/
|
|
||||||
open fun addRequestHeaders(moveMethod: MoveMethod) {
|
|
||||||
//Adding this because the library has an error with override
|
|
||||||
if (moveMethod.forceOverride) {
|
|
||||||
moveMethod.setRequestHeader(OVERWRITE, TRUE)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT)
|
|
||||||
|
|
||||||
private fun isPreconditionFailed(status: Int) = status == HttpConstants.HTTP_PRECONDITION_FAILED
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val MOVE_READ_TIMEOUT = 10L
|
|
||||||
private const val MOVE_CONNECTION_TIMEOUT = 6L
|
|
||||||
private const val OVERWRITE = "overwrite"
|
|
||||||
private const val TRUE = "T"
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,108 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2016 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.resources.files;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0;
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the read a file from the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ReadRemoteFileOperation extends RemoteOperation<RemoteFile> {
|
||||||
|
|
||||||
|
private static final int SYNC_READ_TIMEOUT = 40000;
|
||||||
|
private static final int SYNC_CONNECTION_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
private String mRemotePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath Remote path of the file.
|
||||||
|
*/
|
||||||
|
public ReadRemoteFileOperation(String remotePath) {
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the read operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult<RemoteFile> run(OwnCloudClient client) {
|
||||||
|
PropfindMethod propfind;
|
||||||
|
RemoteOperationResult<RemoteFile> result;
|
||||||
|
|
||||||
|
/// take the duty of check the server for the current state of the file there
|
||||||
|
try {
|
||||||
|
// remote request
|
||||||
|
propfind = new PropfindMethod(
|
||||||
|
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
|
||||||
|
DEPTH_0,
|
||||||
|
DavUtils.getAllPropset());
|
||||||
|
|
||||||
|
propfind.setReadTimeout(SYNC_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
propfind.setConnectionTimeout(SYNC_CONNECTION_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
final int status = client.executeHttpMethod(propfind);
|
||||||
|
|
||||||
|
if (status == HttpConstants.HTTP_MULTI_STATUS
|
||||||
|
|| status == HttpConstants.HTTP_OK) {
|
||||||
|
|
||||||
|
final RemoteFile file = new RemoteFile(propfind.getRoot(), AccountUtils.getUserId(mAccount, mContext));
|
||||||
|
|
||||||
|
result = new RemoteOperationResult<>(OK);
|
||||||
|
result.setData(file);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
result = new RemoteOperationResult<>(propfind);
|
||||||
|
client.exhaustResponse(propfind.getResponseBodyAsStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Synchronizing file %s", mRemotePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -1,107 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2016 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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_MULTI_STATUS
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.DavConstants.DEPTH_0
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the read a file from the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
* @author David González Verdugo
|
|
||||||
*/
|
|
||||||
|
|
||||||
class ReadRemoteFileOperation(
|
|
||||||
val remotePath: String,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<RemoteFile>() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs the read operation.
|
|
||||||
*
|
|
||||||
* @param client Client object to communicate with the remote ownCloud server.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<RemoteFile> {
|
|
||||||
try {
|
|
||||||
val propFind = PropfindMethod(
|
|
||||||
url = getFinalWebDavUrl(),
|
|
||||||
depth = DEPTH_0,
|
|
||||||
propertiesToRequest = DavUtils.allPropSet
|
|
||||||
).apply {
|
|
||||||
setReadTimeout(SYNC_READ_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
setConnectionTimeout(SYNC_CONNECTION_TIMEOUT, TimeUnit.SECONDS)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(propFind)
|
|
||||||
Timber.i("Read remote file $remotePath with status ${propFind.statusCode}")
|
|
||||||
|
|
||||||
return if (isSuccess(status)) {
|
|
||||||
val remoteFile = RemoteFile.getRemoteFileFromDav(
|
|
||||||
davResource = propFind.root!!,
|
|
||||||
userId = AccountUtils.getUserId(mAccount, mContext),
|
|
||||||
userName = mAccount.name,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
)
|
|
||||||
|
|
||||||
RemoteOperationResult<RemoteFile>(RemoteOperationResult.ResultCode.OK).apply {
|
|
||||||
data = remoteFile
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
RemoteOperationResult<RemoteFile>(propFind).also {
|
|
||||||
client.exhaustResponse(propFind.getResponseBodyAsStream())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
return RemoteOperationResult(exception)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
private const val SYNC_READ_TIMEOUT = 40_000L
|
|
||||||
private const val SYNC_CONNECTION_TIMEOUT = 5_000L
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,125 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import at.bitfire.dav4jvm.Response;
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.accounts.AccountUtils;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.DavConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the read of remote file or folder in the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ReadRemoteFolderOperation extends RemoteOperation<ArrayList<RemoteFile>> {
|
||||||
|
|
||||||
|
private String mRemotePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath Remote path of the file.
|
||||||
|
*/
|
||||||
|
public ReadRemoteFolderOperation(String remotePath) {
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the read operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult<ArrayList<RemoteFile>> run(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult<ArrayList<RemoteFile>> result = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
PropfindMethod propfindMethod = new PropfindMethod(
|
||||||
|
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)),
|
||||||
|
DavConstants.DEPTH_1,
|
||||||
|
DavUtils.getAllPropset());
|
||||||
|
|
||||||
|
int status = client.executeHttpMethod(propfindMethod);
|
||||||
|
|
||||||
|
if (isSuccess(status)) {
|
||||||
|
ArrayList<RemoteFile> mFolderAndFiles = new ArrayList<>();
|
||||||
|
|
||||||
|
// parse data from remote folder
|
||||||
|
mFolderAndFiles.add(
|
||||||
|
new RemoteFile(propfindMethod.getRoot(), AccountUtils.getUserId(mAccount, mContext))
|
||||||
|
);
|
||||||
|
|
||||||
|
// loop to update every child
|
||||||
|
for (Response resource : propfindMethod.getMembers()) {
|
||||||
|
RemoteFile file = new RemoteFile(resource, AccountUtils.getUserId(mAccount, mContext));
|
||||||
|
mFolderAndFiles.add(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result of the operation
|
||||||
|
result = new RemoteOperationResult<>(OK);
|
||||||
|
result.setData(mFolderAndFiles);
|
||||||
|
|
||||||
|
} else { // synchronization failed
|
||||||
|
result = new RemoteOperationResult<>(propfindMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
} finally {
|
||||||
|
if (result == null) {
|
||||||
|
Timber.e("Synchronized " + mRemotePath + ": result is null");
|
||||||
|
} else if (result.isSuccess()) {
|
||||||
|
Timber.i("Synchronized " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
} else {
|
||||||
|
if (result.isException()) {
|
||||||
|
Timber.e(result.getException(), "Synchronized " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
} else {
|
||||||
|
Timber.e("Synchronized " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSuccess(int status) {
|
||||||
|
return status == HttpConstants.HTTP_MULTI_STATUS || status == HttpConstants.HTTP_OK;
|
||||||
|
}
|
||||||
|
}
|
@ -1,118 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 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.resources.files
|
|
||||||
|
|
||||||
import at.bitfire.dav4jvm.PropertyRegistry
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.accounts.AccountUtils
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_MULTI_STATUS
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.DavConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.DavUtils
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PropfindMethod
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.properties.OCShareTypes
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the read of remote file or folder in the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
* @author David González Verdugo
|
|
||||||
*/
|
|
||||||
class ReadRemoteFolderOperation(
|
|
||||||
val remotePath: String,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<ArrayList<RemoteFile>>() {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Performs the read operation.
|
|
||||||
*
|
|
||||||
* @param client Client object to communicate with the remote ownCloud server.
|
|
||||||
*/
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<ArrayList<RemoteFile>> {
|
|
||||||
try {
|
|
||||||
PropertyRegistry.register(OCShareTypes.Factory())
|
|
||||||
|
|
||||||
val propfindMethod = PropfindMethod(
|
|
||||||
getFinalWebDavUrl(),
|
|
||||||
DavConstants.DEPTH_1,
|
|
||||||
DavUtils.allPropSet
|
|
||||||
)
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(propfindMethod)
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
val mFolderAndFiles = ArrayList<RemoteFile>()
|
|
||||||
|
|
||||||
val remoteFolder = RemoteFile.getRemoteFileFromDav(
|
|
||||||
davResource = propfindMethod.root!!,
|
|
||||||
userId = AccountUtils.getUserId(mAccount, mContext),
|
|
||||||
userName = mAccount.name,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
)
|
|
||||||
mFolderAndFiles.add(remoteFolder)
|
|
||||||
|
|
||||||
// loop to update every child
|
|
||||||
propfindMethod.members.forEach { resource ->
|
|
||||||
val remoteFile = RemoteFile.getRemoteFileFromDav(
|
|
||||||
davResource = resource,
|
|
||||||
userId = AccountUtils.getUserId(mAccount, mContext),
|
|
||||||
userName = mAccount.name,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
)
|
|
||||||
mFolderAndFiles.add(remoteFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Result of the operation
|
|
||||||
return RemoteOperationResult<ArrayList<RemoteFile>>(ResultCode.OK).apply {
|
|
||||||
data = mFolderAndFiles
|
|
||||||
Timber.i("Synchronized $remotePath with ${mFolderAndFiles.size} files. ${this.logMessage}")
|
|
||||||
}
|
|
||||||
} else { // synchronization failed
|
|
||||||
return RemoteOperationResult<ArrayList<RemoteFile>>(propfindMethod).also {
|
|
||||||
Timber.w("Synchronized $remotePath ${it.logMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
return RemoteOperationResult<ArrayList<RemoteFile>>(e).also {
|
|
||||||
Timber.e(it.exception, "Synchronized $remotePath")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
@ -0,0 +1,322 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import android.os.Parcel;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
|
||||||
|
import at.bitfire.dav4jvm.Property;
|
||||||
|
import at.bitfire.dav4jvm.Response;
|
||||||
|
import at.bitfire.dav4jvm.property.CreationDate;
|
||||||
|
import at.bitfire.dav4jvm.property.GetContentLength;
|
||||||
|
import at.bitfire.dav4jvm.property.GetContentType;
|
||||||
|
import at.bitfire.dav4jvm.property.GetETag;
|
||||||
|
import at.bitfire.dav4jvm.property.GetLastModified;
|
||||||
|
import at.bitfire.dav4jvm.property.OCId;
|
||||||
|
import at.bitfire.dav4jvm.property.OCPermissions;
|
||||||
|
import at.bitfire.dav4jvm.property.OCPrivatelink;
|
||||||
|
import at.bitfire.dav4jvm.property.OCSize;
|
||||||
|
import at.bitfire.dav4jvm.property.QuotaAvailableBytes;
|
||||||
|
import at.bitfire.dav4jvm.property.QuotaUsedBytes;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains the data of a Remote File from a WebDavEntry
|
||||||
|
*
|
||||||
|
* @author masensio
|
||||||
|
* @author Christian Schabesberger
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class RemoteFile implements Parcelable, Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parcelable Methods
|
||||||
|
*/
|
||||||
|
public static final Parcelable.Creator<RemoteFile> CREATOR = new Parcelable.Creator<RemoteFile>() {
|
||||||
|
@Override
|
||||||
|
public RemoteFile createFromParcel(Parcel source) {
|
||||||
|
return new RemoteFile(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RemoteFile[] newArray(int size) {
|
||||||
|
return new RemoteFile[size];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Generated - should be refreshed every time the class changes!!
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -8965995357413958539L;
|
||||||
|
private String mRemotePath;
|
||||||
|
private String mMimeType;
|
||||||
|
private long mLength;
|
||||||
|
private long mCreationTimestamp;
|
||||||
|
private long mModifiedTimestamp;
|
||||||
|
private String mEtag;
|
||||||
|
private String mPermissions;
|
||||||
|
private String mRemoteId;
|
||||||
|
private long mSize;
|
||||||
|
private BigDecimal mQuotaUsedBytes;
|
||||||
|
private BigDecimal mQuotaAvailableBytes;
|
||||||
|
private String mPrivateLink;
|
||||||
|
|
||||||
|
public RemoteFile() {
|
||||||
|
resetData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new {@link RemoteFile} with given path.
|
||||||
|
* <p>
|
||||||
|
* The path received must be URL-decoded. Path separator must be File.separator, and it must be the first
|
||||||
|
* character in 'path'.
|
||||||
|
*
|
||||||
|
* @param path The remote path of the file.
|
||||||
|
*/
|
||||||
|
public RemoteFile(String path) {
|
||||||
|
resetData();
|
||||||
|
if (path == null || path.length() <= 0 || !path.startsWith(File.separator)) {
|
||||||
|
throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
|
||||||
|
}
|
||||||
|
mRemotePath = path;
|
||||||
|
mCreationTimestamp = 0;
|
||||||
|
mLength = 0;
|
||||||
|
mMimeType = FileUtils.MIME_DIR;
|
||||||
|
mQuotaUsedBytes = BigDecimal.ZERO;
|
||||||
|
mQuotaAvailableBytes = BigDecimal.ZERO;
|
||||||
|
mPrivateLink = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteFile(final Response davResource, String userId) {
|
||||||
|
this(RemoteFileUtil.Companion.getRemotePathFromUrl(davResource.getHref(), userId));
|
||||||
|
final List<Property> properties = davResource.getProperties();
|
||||||
|
|
||||||
|
for (Property property : properties) {
|
||||||
|
if (property instanceof CreationDate) {
|
||||||
|
this.setCreationTimestamp(
|
||||||
|
Long.parseLong(((CreationDate) property).getCreationDate()));
|
||||||
|
}
|
||||||
|
if (property instanceof GetContentLength) {
|
||||||
|
this.setLength(((GetContentLength) property).getContentLength());
|
||||||
|
}
|
||||||
|
if (property instanceof GetContentType) {
|
||||||
|
this.setMimeType(((GetContentType) property).getType());
|
||||||
|
}
|
||||||
|
if (property instanceof GetLastModified) {
|
||||||
|
this.setModifiedTimestamp(((GetLastModified) property).getLastModified());
|
||||||
|
}
|
||||||
|
if (property instanceof GetETag) {
|
||||||
|
this.setEtag(((GetETag) property).getETag());
|
||||||
|
}
|
||||||
|
if (property instanceof OCPermissions) {
|
||||||
|
this.setPermissions(((OCPermissions) property).getPermission());
|
||||||
|
}
|
||||||
|
if (property instanceof OCId) {
|
||||||
|
this.setRemoteId(((OCId) property).getId());
|
||||||
|
}
|
||||||
|
if (property instanceof OCSize) {
|
||||||
|
this.setSize(((OCSize) property).getSize());
|
||||||
|
}
|
||||||
|
if (property instanceof QuotaUsedBytes) {
|
||||||
|
this.setQuotaUsedBytes(
|
||||||
|
BigDecimal.valueOf(((QuotaUsedBytes) property).getQuotaUsedBytes()));
|
||||||
|
}
|
||||||
|
if (property instanceof QuotaAvailableBytes) {
|
||||||
|
this.setQuotaAvailableBytes(
|
||||||
|
BigDecimal.valueOf(((QuotaAvailableBytes) property).getQuotaAvailableBytes()));
|
||||||
|
}
|
||||||
|
if (property instanceof OCPrivatelink) {
|
||||||
|
this.setPrivateLink(((OCPrivatelink) property).getLink());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reconstruct from parcel
|
||||||
|
*
|
||||||
|
* @param source The source parcel
|
||||||
|
*/
|
||||||
|
protected RemoteFile(Parcel source) {
|
||||||
|
readFromParcel(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use this to find out if this file is a folder.
|
||||||
|
*
|
||||||
|
* @return true if it is a folder
|
||||||
|
*/
|
||||||
|
public boolean isFolder() {
|
||||||
|
return mMimeType != null && (mMimeType.equals(FileUtils.MIME_DIR) || mMimeType.equals(FileUtils.MIME_DIR_UNIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getters and Setters
|
||||||
|
*/
|
||||||
|
|
||||||
|
public String getRemotePath() {
|
||||||
|
return mRemotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRemotePath(String remotePath) {
|
||||||
|
this.mRemotePath = remotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMimeType() {
|
||||||
|
return mMimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMimeType(String mimeType) {
|
||||||
|
this.mMimeType = mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLength() {
|
||||||
|
return mLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLength(long length) {
|
||||||
|
this.mLength = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreationTimestamp() {
|
||||||
|
return mCreationTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreationTimestamp(long creationTimestamp) {
|
||||||
|
this.mCreationTimestamp = creationTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getModifiedTimestamp() {
|
||||||
|
return mModifiedTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModifiedTimestamp(long modifiedTimestamp) {
|
||||||
|
this.mModifiedTimestamp = modifiedTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEtag() {
|
||||||
|
return mEtag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEtag(String etag) {
|
||||||
|
this.mEtag = etag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPermissions() {
|
||||||
|
return mPermissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissions(String permissions) {
|
||||||
|
this.mPermissions = permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRemoteId() {
|
||||||
|
return mRemoteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRemoteId(String remoteId) {
|
||||||
|
this.mRemoteId = remoteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getSize() {
|
||||||
|
return mSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSize(long size) {
|
||||||
|
mSize = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuotaUsedBytes(BigDecimal quotaUsedBytes) {
|
||||||
|
mQuotaUsedBytes = quotaUsedBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQuotaAvailableBytes(BigDecimal quotaAvailableBytes) {
|
||||||
|
mQuotaAvailableBytes = quotaAvailableBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrivateLink() {
|
||||||
|
return mPrivateLink;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrivateLink(String privateLink) {
|
||||||
|
mPrivateLink = privateLink;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used internally. Reset all file properties
|
||||||
|
*/
|
||||||
|
private void resetData() {
|
||||||
|
mRemotePath = null;
|
||||||
|
mMimeType = null;
|
||||||
|
mLength = 0;
|
||||||
|
mCreationTimestamp = 0;
|
||||||
|
mModifiedTimestamp = 0;
|
||||||
|
mEtag = null;
|
||||||
|
mPermissions = null;
|
||||||
|
mRemoteId = null;
|
||||||
|
mSize = 0;
|
||||||
|
mQuotaUsedBytes = null;
|
||||||
|
mQuotaAvailableBytes = null;
|
||||||
|
mPrivateLink = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void readFromParcel(Parcel source) {
|
||||||
|
mRemotePath = source.readString();
|
||||||
|
mMimeType = source.readString();
|
||||||
|
mLength = source.readLong();
|
||||||
|
mCreationTimestamp = source.readLong();
|
||||||
|
mModifiedTimestamp = source.readLong();
|
||||||
|
mEtag = source.readString();
|
||||||
|
mPermissions = source.readString();
|
||||||
|
mRemoteId = source.readString();
|
||||||
|
mSize = source.readLong();
|
||||||
|
mQuotaUsedBytes = (BigDecimal) source.readSerializable();
|
||||||
|
mQuotaAvailableBytes = (BigDecimal) source.readSerializable();
|
||||||
|
mPrivateLink = source.readString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int describeContents() {
|
||||||
|
return this.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
|
dest.writeString(mRemotePath);
|
||||||
|
dest.writeString(mMimeType);
|
||||||
|
dest.writeLong(mLength);
|
||||||
|
dest.writeLong(mCreationTimestamp);
|
||||||
|
dest.writeLong(mModifiedTimestamp);
|
||||||
|
dest.writeString(mEtag);
|
||||||
|
dest.writeString(mPermissions);
|
||||||
|
dest.writeString(mRemoteId);
|
||||||
|
dest.writeLong(mSize);
|
||||||
|
dest.writeSerializable(mQuotaUsedBytes);
|
||||||
|
dest.writeSerializable(mQuotaAvailableBytes);
|
||||||
|
dest.writeString(mPrivateLink);
|
||||||
|
}
|
||||||
|
}
|
@ -1,194 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 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.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
|
|
||||||
import at.bitfire.dav4jvm.property.CreationDate
|
|
||||||
import at.bitfire.dav4jvm.property.GetContentLength
|
|
||||||
import at.bitfire.dav4jvm.property.GetContentType
|
|
||||||
import at.bitfire.dav4jvm.property.GetETag
|
|
||||||
import at.bitfire.dav4jvm.property.GetLastModified
|
|
||||||
import at.bitfire.dav4jvm.property.OCId
|
|
||||||
import at.bitfire.dav4jvm.property.OCPermissions
|
|
||||||
import at.bitfire.dav4jvm.property.OCPrivatelink
|
|
||||||
import at.bitfire.dav4jvm.property.OCSize
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.properties.OCShareTypes
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import com.owncloud.android.lib.resources.shares.ShareType
|
|
||||||
import com.owncloud.android.lib.resources.shares.ShareType.Companion.fromValue
|
|
||||||
import kotlinx.parcelize.Parcelize
|
|
||||||
import okhttp3.HttpUrl
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Contains the data of a Remote File from a WebDavEntry
|
|
||||||
*
|
|
||||||
* The path received must be URL-decoded. Path separator must be File.separator, and it must be the first character in 'path'.
|
|
||||||
*
|
|
||||||
* @author masensio
|
|
||||||
* @author Christian Schabesberger
|
|
||||||
* @author Abel García de Prada
|
|
||||||
*/
|
|
||||||
@Parcelize
|
|
||||||
data class RemoteFile(
|
|
||||||
var remotePath: String,
|
|
||||||
var mimeType: String = "DIR",
|
|
||||||
var length: Long = 0,
|
|
||||||
var creationTimestamp: Long = 0,
|
|
||||||
var modifiedTimestamp: Long = 0,
|
|
||||||
var etag: String? = null,
|
|
||||||
var permissions: String? = null,
|
|
||||||
var remoteId: String? = null,
|
|
||||||
var size: Long = 0,
|
|
||||||
var privateLink: String? = null,
|
|
||||||
var owner: String,
|
|
||||||
var sharedByLink: Boolean = false,
|
|
||||||
var sharedWithSharee: Boolean = false,
|
|
||||||
) : Parcelable {
|
|
||||||
|
|
||||||
// TODO: Quotas not used. Use or remove them.
|
|
||||||
init {
|
|
||||||
require(
|
|
||||||
!(remotePath.isEmpty() || !remotePath.startsWith(File.separator))
|
|
||||||
) { "Trying to create a OCFile with a non valid remote path: $remotePath" }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use this to find out if this file is a folder.
|
|
||||||
*
|
|
||||||
* @return true if it is a folder
|
|
||||||
*/
|
|
||||||
val isFolder
|
|
||||||
get() = mimeType.isOneOf(MIME_DIR, MIME_DIR_UNIX)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
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)
|
|
||||||
val remoteFile = RemoteFile(remotePath = remotePath, owner = userName)
|
|
||||||
val properties = getPropertiesEvenIfPostProcessing(davResource)
|
|
||||||
|
|
||||||
for (property in properties) {
|
|
||||||
when (property) {
|
|
||||||
is CreationDate -> {
|
|
||||||
remoteFile.creationTimestamp = property.creationDate.toLong()
|
|
||||||
}
|
|
||||||
is GetContentLength -> {
|
|
||||||
remoteFile.length = property.contentLength
|
|
||||||
}
|
|
||||||
is GetContentType -> {
|
|
||||||
property.type?.let { remoteFile.mimeType = it }
|
|
||||||
}
|
|
||||||
is GetLastModified -> {
|
|
||||||
remoteFile.modifiedTimestamp = property.lastModified
|
|
||||||
}
|
|
||||||
is GetETag -> {
|
|
||||||
remoteFile.etag = property.eTag
|
|
||||||
}
|
|
||||||
is OCPermissions -> {
|
|
||||||
remoteFile.permissions = property.permission
|
|
||||||
}
|
|
||||||
is OCId -> {
|
|
||||||
remoteFile.remoteId = property.id
|
|
||||||
}
|
|
||||||
is OCSize -> {
|
|
||||||
remoteFile.size = property.size
|
|
||||||
}
|
|
||||||
is OCPrivatelink -> {
|
|
||||||
remoteFile.privateLink = property.link
|
|
||||||
}
|
|
||||||
is OCShareTypes -> {
|
|
||||||
val list = property.shareTypes
|
|
||||||
for (i in list.indices) {
|
|
||||||
val shareType = fromValue(list[i].toInt())
|
|
||||||
if (shareType == null) {
|
|
||||||
Timber.d("Illegal share type value: " + list[i])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (shareType == ShareType.PUBLIC_LINK) {
|
|
||||||
remoteFile.sharedByLink = true
|
|
||||||
} else if (shareType == ShareType.USER || shareType == ShareType.FEDERATED || shareType == ShareType.GROUP) {
|
|
||||||
remoteFile.sharedWithSharee = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 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
|
|
||||||
*
|
|
||||||
* @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)
|
|
||||||
val pathToOc = absoluteDavPath.split(davFilesPath).first()
|
|
||||||
return absoluteDavPath.replace(pathToOc + davFilesPath, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getPropertiesEvenIfPostProcessing(response: Response): List<Property> {
|
|
||||||
return if (response.isSuccess())
|
|
||||||
response.propstat.filter { propStat -> propStat.isSuccessOrPostProcessing() }.map { it.properties }.flatten()
|
|
||||||
else
|
|
||||||
emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun PropStat.isSuccessOrPostProcessing() = (status.code / 100 == 2 || status.code == HttpConstants.HTTP_TOO_EARLY)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -19,16 +19,31 @@
|
|||||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
package com.owncloud.android.lib.resources.spaces.services
|
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.operations.RemoteOperationResult
|
import okhttp3.HttpUrl
|
||||||
import com.owncloud.android.lib.resources.spaces.GetRemoteSpacesOperation
|
|
||||||
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
|
|
||||||
|
|
||||||
class OCSpacesService(override val client: OwnCloudClient) : SpacesService {
|
class RemoteFileUtil {
|
||||||
override fun getSpaces(): RemoteOperationResult<List<SpaceResponse>> {
|
companion object {
|
||||||
return GetRemoteSpacesOperation().execute(client)
|
/**
|
||||||
|
* Retrieves a relative path from a remote file url
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Example: url:port/remote.php/dav/files/username/Documents/text.txt => /Documents/text.txt
|
||||||
|
*
|
||||||
|
* @param url remote file url
|
||||||
|
* @param userId file owner
|
||||||
|
* @return remote relative path of the file
|
||||||
|
*/
|
||||||
|
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)[0]
|
||||||
|
return absoluteDavPath.replace(pathToOc + davFilesPath, "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the removal of a remote file or folder in the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class RemoveRemoteFileOperation extends RemoteOperation {
|
||||||
|
private String mRemotePath;
|
||||||
|
|
||||||
|
protected boolean removeChunksFolder = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath RemotePath of the remote file or folder to remove from the server
|
||||||
|
*/
|
||||||
|
public RemoveRemoteFileOperation(String remotePath) {
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the rename operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult result;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Uri srcWebDavUri = removeChunksFolder ? client.getUploadsWebDavUri() : client.getUserFilesWebDavUri();
|
||||||
|
|
||||||
|
DeleteMethod deleteMethod = new DeleteMethod(
|
||||||
|
new URL(srcWebDavUri + WebdavUtils.encodePath(mRemotePath)));
|
||||||
|
|
||||||
|
int status = client.executeHttpMethod(deleteMethod);
|
||||||
|
|
||||||
|
result = isSuccess(status) ?
|
||||||
|
new RemoteOperationResult<>(OK) :
|
||||||
|
new RemoteOperationResult<>(deleteMethod);
|
||||||
|
|
||||||
|
Timber.i("Remove " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Remove " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSuccess(int status) {
|
||||||
|
return status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_NO_CONTENT;
|
||||||
|
}
|
||||||
|
}
|
@ -1,81 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2020 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.resources.files
|
|
||||||
|
|
||||||
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
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the removal of a remote file or folder in the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
* @author David González Verdugo
|
|
||||||
* @author Abel García de Prada
|
|
||||||
*/
|
|
||||||
open class RemoveRemoteFileOperation(
|
|
||||||
private val remotePath: String,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
try {
|
|
||||||
val srcWebDavUri = getSrcWebDavUriForClient(client)
|
|
||||||
val deleteMethod = DeleteMethod(
|
|
||||||
URL(srcWebDavUri + WebdavUtils.encodePath(remotePath))
|
|
||||||
)
|
|
||||||
val status = client.executeHttpMethod(deleteMethod)
|
|
||||||
|
|
||||||
result = if (isSuccess(status)) {
|
|
||||||
RemoteOperationResult<Unit>(ResultCode.OK)
|
|
||||||
} else {
|
|
||||||
RemoteOperationResult<Unit>(deleteMethod)
|
|
||||||
}
|
|
||||||
Timber.i("Remove $remotePath: ${result.logMessage}")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
result = RemoteOperationResult<Unit>(e)
|
|
||||||
Timber.e(e, "Remove $remotePath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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()
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status.isOneOf(HTTP_OK, HTTP_NO_CONTENT)
|
|
||||||
}
|
|
@ -0,0 +1,131 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2019 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.resources.files;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the rename of a remote file or folder in the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
*/
|
||||||
|
public class RenameRemoteFileOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
private static final int RENAME_READ_TIMEOUT = 600000;
|
||||||
|
private static final int RENAME_CONNECTION_TIMEOUT = 5000;
|
||||||
|
|
||||||
|
private String mOldName;
|
||||||
|
private String mOldRemotePath;
|
||||||
|
private String mNewName;
|
||||||
|
private String mNewRemotePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param oldName Old name of the file.
|
||||||
|
* @param oldRemotePath Old remote path of the file.
|
||||||
|
* @param newName New name to set as the name of file.
|
||||||
|
* @param isFolder 'true' for folder and 'false' for files
|
||||||
|
*/
|
||||||
|
public RenameRemoteFileOperation(String oldName, String oldRemotePath, String newName,
|
||||||
|
boolean isFolder) {
|
||||||
|
mOldName = oldName;
|
||||||
|
mOldRemotePath = oldRemotePath;
|
||||||
|
mNewName = newName;
|
||||||
|
|
||||||
|
String parent = (new File(mOldRemotePath)).getParent();
|
||||||
|
parent = (parent.endsWith(File.separator)) ? parent : parent + File.separator;
|
||||||
|
mNewRemotePath = parent + mNewName;
|
||||||
|
if (isFolder) {
|
||||||
|
mNewRemotePath += File.separator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the rename operation.
|
||||||
|
*
|
||||||
|
* @param client Client object to communicate with the remote ownCloud server.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
try {
|
||||||
|
if (mNewName.equals(mOldName)) {
|
||||||
|
return new RemoteOperationResult<>(ResultCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetPathIsUsed(client)) {
|
||||||
|
return new RemoteOperationResult<>(ResultCode.INVALID_OVERWRITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
final MoveMethod move = new MoveMethod(
|
||||||
|
new URL(client.getUserFilesWebDavUri() +
|
||||||
|
WebdavUtils.encodePath(mOldRemotePath)),
|
||||||
|
client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mNewRemotePath), false);
|
||||||
|
|
||||||
|
move.setReadTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
move.setConnectionTimeout(RENAME_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
final int status = client.executeHttpMethod(move);
|
||||||
|
final RemoteOperationResult result =
|
||||||
|
(status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT)
|
||||||
|
? new RemoteOperationResult<>(ResultCode.OK)
|
||||||
|
: new RemoteOperationResult<>(move);
|
||||||
|
|
||||||
|
Timber.i("Rename " + mOldRemotePath + " to " + mNewRemotePath + ": " + result.getLogMessage());
|
||||||
|
client.exhaustResponse(move.getResponseBodyAsStream());
|
||||||
|
return result;
|
||||||
|
} catch (Exception e) {
|
||||||
|
final RemoteOperationResult result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e,
|
||||||
|
"Rename " + mOldRemotePath + " to " + ((mNewRemotePath == null) ? mNewName : mNewRemotePath) + ":" +
|
||||||
|
" " + result.getLogMessage());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a file with the new name already exists.
|
||||||
|
*
|
||||||
|
* @return 'True' if the target path is already used by an existing file.
|
||||||
|
*/
|
||||||
|
private boolean targetPathIsUsed(OwnCloudClient client) {
|
||||||
|
CheckPathExistenceRemoteOperation checkPathExistenceRemoteOperation =
|
||||||
|
new CheckPathExistenceRemoteOperation(mNewRemotePath, false);
|
||||||
|
RemoteOperationResult exists = checkPathExistenceRemoteOperation.execute(client);
|
||||||
|
return exists.isSuccess();
|
||||||
|
}
|
||||||
|
}
|
@ -1,120 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2021 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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the rename of a remote file or folder in the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
*/
|
|
||||||
class RenameRemoteFileOperation(
|
|
||||||
private val oldName: String,
|
|
||||||
private val oldRemotePath: String,
|
|
||||||
private val newName: String,
|
|
||||||
isFolder: Boolean,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
private var newRemotePath: String
|
|
||||||
|
|
||||||
init {
|
|
||||||
var parent = (File(oldRemotePath)).parent ?: throw IllegalArgumentException()
|
|
||||||
if (!parent.endsWith(File.separator)) {
|
|
||||||
parent = parent.plus(File.separator)
|
|
||||||
}
|
|
||||||
newRemotePath = parent.plus(newName)
|
|
||||||
if (isFolder) {
|
|
||||||
newRemotePath.plus(File.separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
try {
|
|
||||||
if (newName == oldName) {
|
|
||||||
return RemoteOperationResult<Unit>(ResultCode.OK)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetPathIsUsed(client)) {
|
|
||||||
return RemoteOperationResult<Unit>(ResultCode.INVALID_OVERWRITE)
|
|
||||||
}
|
|
||||||
|
|
||||||
val moveMethod: MoveMethod = MoveMethod(
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
val status = client.executeHttpMethod(moveMethod)
|
|
||||||
|
|
||||||
result = if (isSuccess(status)) {
|
|
||||||
RemoteOperationResult<Unit>(ResultCode.OK)
|
|
||||||
} else {
|
|
||||||
RemoteOperationResult<Unit>(moveMethod)
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.i("Rename $oldRemotePath to $newRemotePath: ${result.logMessage}")
|
|
||||||
client.exhaustResponse(moveMethod.getResponseBodyAsStream())
|
|
||||||
return result
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
result = RemoteOperationResult<Unit>(exception)
|
|
||||||
Timber.e(exception, "Rename $oldRemotePath to $newName: ${result.logMessage}")
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a file with the new name already exists.
|
|
||||||
*
|
|
||||||
* @return 'True' if the target path is already used by an existing file.
|
|
||||||
*/
|
|
||||||
private fun targetPathIsUsed(client: OwnCloudClient): Boolean {
|
|
||||||
val checkPathExistenceRemoteOperation = CheckPathExistenceRemoteOperation(newRemotePath, true)
|
|
||||||
val exists = checkPathExistenceRemoteOperation.execute(client)
|
|
||||||
return exists.isSuccess
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status.isOneOf(HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val RENAME_READ_TIMEOUT = 10_000L
|
|
||||||
private const val RENAME_CONNECTION_TIMEOUT = 5_000L
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* Copyright (C) 2021 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -24,15 +24,22 @@
|
|||||||
|
|
||||||
package com.owncloud.android.lib.resources.files
|
package com.owncloud.android.lib.resources.files
|
||||||
|
|
||||||
|
import android.content.ContentResolver
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.OpenableColumns
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
import com.owncloud.android.lib.common.http.HttpConstants
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod
|
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod
|
||||||
import com.owncloud.android.lib.common.network.ContentUriRequestBody
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
import com.owncloud.android.lib.common.network.WebdavUtils
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
import com.owncloud.android.lib.common.operations.RemoteOperation
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
import okhttp3.MediaType
|
||||||
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
|
import okhttp3.RequestBody
|
||||||
|
import okio.BufferedSink
|
||||||
|
import okio.source
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.io.IOException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
class UploadFileFromContentUriOperation(
|
class UploadFileFromContentUriOperation(
|
||||||
@ -63,6 +70,34 @@ class UploadFileFromContentUriOperation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun isSuccess(status: Int): Boolean {
|
fun isSuccess(status: Int): Boolean {
|
||||||
return status.isOneOf(HttpConstants.HTTP_OK, HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT)
|
return status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_CREATED || status == HttpConstants.HTTP_NO_CONTENT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ContentUriRequestBody(
|
||||||
|
private val contentResolver: ContentResolver,
|
||||||
|
private val contentUri: Uri
|
||||||
|
) : RequestBody() {
|
||||||
|
|
||||||
|
override fun contentType(): MediaType? {
|
||||||
|
val contentType = contentResolver.getType(contentUri) ?: return null
|
||||||
|
return contentType.toMediaTypeOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun contentLength(): Long {
|
||||||
|
contentResolver.query(contentUri, null, null, null, null)?.use { cursor ->
|
||||||
|
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
|
||||||
|
cursor.moveToFirst()
|
||||||
|
return cursor.getLong(sizeIndex)
|
||||||
|
} ?: return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun writeTo(sink: BufferedSink) {
|
||||||
|
val inputStream = contentResolver.openInputStream(contentUri)
|
||||||
|
?: throw IOException("Couldn't open content URI for reading: $contentUri")
|
||||||
|
|
||||||
|
inputStream.source().use { source ->
|
||||||
|
sink.writeAll(source)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,148 +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
|
|
||||||
* NONINFINGEMENT. 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.resources.files
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod
|
|
||||||
import com.owncloud.android.lib.common.network.FileRequestBody
|
|
||||||
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
|
|
||||||
import com.owncloud.android.lib.common.network.WebdavUtils
|
|
||||||
import com.owncloud.android.lib.common.operations.OperationCancelledException
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.common.utils.isOneOf
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the upload of a remote file to the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author masensio
|
|
||||||
* @author David González Verdugo
|
|
||||||
* @author Abel García de Prada
|
|
||||||
* @author Juan Carlos Garrote Gascón
|
|
||||||
*/
|
|
||||||
open class UploadFileFromFileSystemOperation(
|
|
||||||
val localPath: String,
|
|
||||||
val remotePath: String,
|
|
||||||
val mimeType: String,
|
|
||||||
val lastModifiedTimestamp: String,
|
|
||||||
val requiredEtag: String?,
|
|
||||||
val spaceWebDavUrl: String? = null,
|
|
||||||
) : RemoteOperation<Unit>() {
|
|
||||||
|
|
||||||
protected val cancellationRequested = AtomicBoolean(false)
|
|
||||||
protected var putMethod: PutMethod? = null
|
|
||||||
protected val dataTransferListener: MutableSet<OnDatatransferProgressListener> = HashSet()
|
|
||||||
protected var fileRequestBody: FileRequestBody? = null
|
|
||||||
|
|
||||||
var etag: String = ""
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
try {
|
|
||||||
if (cancellationRequested.get()) {
|
|
||||||
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
|
||||||
result = RemoteOperationResult<Unit>(OperationCancelledException())
|
|
||||||
Timber.i("Upload of $localPath to $remotePath has been cancelled")
|
|
||||||
} else {
|
|
||||||
// perform the upload
|
|
||||||
result = uploadFile(client)
|
|
||||||
Timber.i("Upload of $localPath to $remotePath: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
if (putMethod?.isAborted == true) {
|
|
||||||
result = RemoteOperationResult<Unit>(OperationCancelledException())
|
|
||||||
Timber.e(result.exception, "Upload of $localPath to $remotePath has been aborted with this message: ${result.logMessage}")
|
|
||||||
} else {
|
|
||||||
result = RemoteOperationResult<Unit>(e)
|
|
||||||
Timber.e(result.exception, "Upload of $localPath to $remotePath has failed with this message: ${result.logMessage}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(Exception::class)
|
|
||||||
protected open fun uploadFile(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
val fileToUpload = File(localPath)
|
|
||||||
val mediaType: MediaType? = mimeType.toMediaTypeOrNull()
|
|
||||||
|
|
||||||
fileRequestBody = FileRequestBody(fileToUpload, mediaType).also {
|
|
||||||
synchronized(dataTransferListener) { it.addDatatransferProgressListeners(dataTransferListener) }
|
|
||||||
}
|
|
||||||
|
|
||||||
val baseStringUrl = spaceWebDavUrl ?: client.userFilesWebDavUri.toString()
|
|
||||||
putMethod = PutMethod(URL(baseStringUrl + WebdavUtils.encodePath(remotePath)), fileRequestBody!!).apply {
|
|
||||||
retryOnConnectionFailure = false
|
|
||||||
if (!requiredEtag.isNullOrBlank()) {
|
|
||||||
addRequestHeader(HttpConstants.IF_MATCH_HEADER, requiredEtag)
|
|
||||||
}
|
|
||||||
addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, fileToUpload.length().toString())
|
|
||||||
addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, lastModifiedTimestamp)
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(putMethod)
|
|
||||||
return if (isSuccess(status)) {
|
|
||||||
etag = WebdavUtils.getEtagFromResponse(putMethod)
|
|
||||||
// Get rid of extra quotas
|
|
||||||
etag = etag.replace("\"", "")
|
|
||||||
if (etag.isEmpty()) {
|
|
||||||
Timber.e("Could not read eTag from response uploading %s", localPath)
|
|
||||||
} else {
|
|
||||||
Timber.d("File uploaded successfully. New etag for file ${fileToUpload.name} is $etag")
|
|
||||||
}
|
|
||||||
RemoteOperationResult<Unit>(ResultCode.OK).apply { data = Unit }
|
|
||||||
} else { // synchronization failed
|
|
||||||
RemoteOperationResult<Unit>(putMethod)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addDataTransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListener) { dataTransferListener.add(listener) }
|
|
||||||
fileRequestBody?.addDatatransferProgressListener(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun removeDataTransferProgressListener(listener: OnDatatransferProgressListener) {
|
|
||||||
synchronized(dataTransferListener) { dataTransferListener.remove(listener) }
|
|
||||||
fileRequestBody?.removeDatatransferProgressListener(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun cancel() {
|
|
||||||
synchronized(cancellationRequested) {
|
|
||||||
cancellationRequested.set(true)
|
|
||||||
putMethod?.abort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isSuccess(status: Int): Boolean {
|
|
||||||
return status.isOneOf(HttpConstants.HTTP_OK, HttpConstants.HTTP_CREATED, HttpConstants.HTTP_NO_CONTENT)
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,181 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.HttpConstants;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.FileRequestBody;
|
||||||
|
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
|
||||||
|
import com.owncloud.android.lib.common.network.WebdavUtils;
|
||||||
|
import com.owncloud.android.lib.common.operations.OperationCancelledException;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the upload of a remote file to the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author masensio
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class UploadRemoteFileOperation extends RemoteOperation {
|
||||||
|
|
||||||
|
protected final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
|
||||||
|
protected String mLocalPath;
|
||||||
|
protected String mRemotePath;
|
||||||
|
protected String mMimeType;
|
||||||
|
protected String mFileLastModifTimestamp;
|
||||||
|
protected PutMethod mPutMethod = null;
|
||||||
|
protected String mRequiredEtag = null;
|
||||||
|
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
|
||||||
|
|
||||||
|
protected FileRequestBody mFileRequestBody = null;
|
||||||
|
|
||||||
|
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType,
|
||||||
|
String fileLastModifTimestamp) {
|
||||||
|
mLocalPath = localPath;
|
||||||
|
mRemotePath = remotePath;
|
||||||
|
mMimeType = mimeType;
|
||||||
|
mFileLastModifTimestamp = fileLastModifTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UploadRemoteFileOperation(String localPath, String remotePath, String mimeType,
|
||||||
|
String requiredEtag, String fileLastModifTimestamp) {
|
||||||
|
this(localPath, remotePath, mimeType, fileLastModifTimestamp);
|
||||||
|
mRequiredEtag = requiredEtag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
|
RemoteOperationResult result;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
if (mCancellationRequested.get()) {
|
||||||
|
// the operation was cancelled before getting it's turn to be executed in the queue of uploads
|
||||||
|
result = new RemoteOperationResult<>(new OperationCancelledException());
|
||||||
|
} else {
|
||||||
|
// perform the upload
|
||||||
|
result = uploadFile(client);
|
||||||
|
Timber.i("Upload of " + mLocalPath + " to " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
if (mPutMethod != null && mPutMethod.isAborted()) {
|
||||||
|
result = new RemoteOperationResult<>(new OperationCancelledException());
|
||||||
|
Timber.e(result.getException(),
|
||||||
|
"Upload of " + mLocalPath + " to " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
} else {
|
||||||
|
result = new RemoteOperationResult<>(e);
|
||||||
|
Timber.e(e, "Upload of " + mLocalPath + " to " + mRemotePath + ": " + result.getLogMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected RemoteOperationResult<?> uploadFile(OwnCloudClient client) throws Exception {
|
||||||
|
|
||||||
|
File fileToUpload = new File(mLocalPath);
|
||||||
|
|
||||||
|
MediaType mediaType = MediaType.parse(mMimeType);
|
||||||
|
|
||||||
|
mFileRequestBody = new FileRequestBody(fileToUpload, mediaType);
|
||||||
|
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
mPutMethod = new PutMethod(
|
||||||
|
new URL(client.getUserFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)), mFileRequestBody);
|
||||||
|
|
||||||
|
mPutMethod.setRetryOnConnectionFailure(false);
|
||||||
|
|
||||||
|
if (mRequiredEtag != null && mRequiredEtag.length() > 0) {
|
||||||
|
mPutMethod.addRequestHeader(HttpConstants.IF_MATCH_HEADER, mRequiredEtag);
|
||||||
|
}
|
||||||
|
|
||||||
|
mPutMethod.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(fileToUpload.length()));
|
||||||
|
mPutMethod.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp);
|
||||||
|
|
||||||
|
int status = client.executeHttpMethod(mPutMethod);
|
||||||
|
|
||||||
|
if (isSuccess(status)) {
|
||||||
|
return new RemoteOperationResult<>(OK);
|
||||||
|
|
||||||
|
} else { // synchronization failed
|
||||||
|
return new RemoteOperationResult<>(mPutMethod);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<OnDatatransferProgressListener> getDataTransferListeners() {
|
||||||
|
return mDataTransferListeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.add(listener);
|
||||||
|
}
|
||||||
|
if (mFileRequestBody != null) {
|
||||||
|
mFileRequestBody.addDatatransferProgressListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mDataTransferListeners.remove(listener);
|
||||||
|
}
|
||||||
|
if (mFileRequestBody != null) {
|
||||||
|
mFileRequestBody.removeDatatransferProgressListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
synchronized (mCancellationRequested) {
|
||||||
|
mCancellationRequested.set(true);
|
||||||
|
if (mPutMethod != null) {
|
||||||
|
mPutMethod.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSuccess(int status) {
|
||||||
|
return ((status == HttpConstants.HTTP_OK || status == HttpConstants.HTTP_CREATED ||
|
||||||
|
status == HttpConstants.HTTP_NO_CONTENT));
|
||||||
|
}
|
||||||
|
}
|
@ -1,122 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.resources.files.chunks
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod
|
|
||||||
import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody
|
|
||||||
import com.owncloud.android.lib.common.operations.OperationCancelledException
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
|
|
||||||
import com.owncloud.android.lib.resources.files.FileUtils.MODE_READ_ONLY
|
|
||||||
import com.owncloud.android.lib.resources.files.UploadFileFromFileSystemOperation
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.io.File
|
|
||||||
import java.io.RandomAccessFile
|
|
||||||
import java.net.URL
|
|
||||||
import java.nio.channels.FileChannel
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import kotlin.math.ceil
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remote operation performing the chunked upload of a remote file to the ownCloud server.
|
|
||||||
*
|
|
||||||
* @author David A. Velasco
|
|
||||||
* @author David González Verdugo
|
|
||||||
* @author Abel García de Prada
|
|
||||||
*/
|
|
||||||
class ChunkedUploadFromFileSystemOperation(
|
|
||||||
private val transferId: String,
|
|
||||||
localPath: String,
|
|
||||||
remotePath: String,
|
|
||||||
mimeType: String,
|
|
||||||
lastModifiedTimestamp: String,
|
|
||||||
requiredEtag: String?,
|
|
||||||
) : UploadFileFromFileSystemOperation(
|
|
||||||
localPath = localPath,
|
|
||||||
remotePath = remotePath,
|
|
||||||
mimeType = mimeType,
|
|
||||||
lastModifiedTimestamp = lastModifiedTimestamp,
|
|
||||||
requiredEtag = requiredEtag
|
|
||||||
) {
|
|
||||||
|
|
||||||
@Throws(Exception::class)
|
|
||||||
override fun uploadFile(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
|
||||||
lateinit var result: RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
val fileToUpload = File(localPath)
|
|
||||||
val mediaType: MediaType? = mimeType.toMediaTypeOrNull()
|
|
||||||
val raf = RandomAccessFile(fileToUpload, MODE_READ_ONLY)
|
|
||||||
val channel: FileChannel = raf.channel
|
|
||||||
|
|
||||||
val fileRequestBody = ChunkFromFileRequestBody(fileToUpload, mediaType, channel).also {
|
|
||||||
synchronized(dataTransferListener) { it.addDatatransferProgressListeners(dataTransferListener) }
|
|
||||||
}
|
|
||||||
|
|
||||||
val uriPrefix = client.uploadsWebDavUri.toString() + File.separator + transferId
|
|
||||||
val totalLength = fileToUpload.length()
|
|
||||||
val chunkCount = ceil(totalLength.toDouble() / CHUNK_SIZE).toLong()
|
|
||||||
var offset: Long = 0
|
|
||||||
|
|
||||||
for (chunkIndex in 0..chunkCount) {
|
|
||||||
fileRequestBody.setOffset(offset)
|
|
||||||
|
|
||||||
if (cancellationRequested.get()) {
|
|
||||||
result = RemoteOperationResult<Unit>(OperationCancelledException())
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
putMethod = PutMethod(URL(uriPrefix + File.separator + chunkIndex), fileRequestBody).apply {
|
|
||||||
if (chunkIndex == chunkCount - 1) {
|
|
||||||
// Added a high timeout to the last chunk due to when the last chunk
|
|
||||||
// arrives to the server with the last PUT, all chunks get assembled
|
|
||||||
// within that PHP request, so last one takes longer.
|
|
||||||
setReadTimeout(LAST_CHUNK_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val status = client.executeHttpMethod(putMethod)
|
|
||||||
|
|
||||||
Timber.d("Upload of $localPath to $remotePath, chunk index $chunkIndex, count $chunkCount, HTTP result status $status")
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
result = RemoteOperationResult<Unit>(ResultCode.OK)
|
|
||||||
} else {
|
|
||||||
result = RemoteOperationResult<Unit>(putMethod)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
offset += CHUNK_SIZE
|
|
||||||
}
|
|
||||||
channel.close()
|
|
||||||
raf.close()
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val CHUNK_SIZE = 10_240_000L // 10 MB
|
|
||||||
private const val LAST_CHUNK_TIMEOUT = 900_000 // 15 mins.
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,130 @@
|
|||||||
|
/* ownCloud Android Library is available under MIT license
|
||||||
|
* Copyright (C) 2020 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.resources.files.chunks;
|
||||||
|
|
||||||
|
import com.owncloud.android.lib.common.OwnCloudClient;
|
||||||
|
import com.owncloud.android.lib.common.http.methods.webdav.PutMethod;
|
||||||
|
import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody;
|
||||||
|
import com.owncloud.android.lib.common.operations.OperationCancelledException;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remote operation performing the chunked upload of a remote file to the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David A. Velasco
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation {
|
||||||
|
|
||||||
|
public static final long CHUNK_SIZE = 1024000;
|
||||||
|
private static final int LAST_CHUNK_TIMEOUT = 900000; //15 mins.
|
||||||
|
|
||||||
|
private String mTransferId;
|
||||||
|
|
||||||
|
public ChunkedUploadRemoteFileOperation(String transferId, String localPath, String remotePath, String mimeType,
|
||||||
|
String requiredEtag, String fileLastModifTimestamp) {
|
||||||
|
super(localPath, remotePath, mimeType, requiredEtag, fileLastModifTimestamp);
|
||||||
|
mTransferId = transferId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RemoteOperationResult uploadFile(OwnCloudClient client) throws Exception {
|
||||||
|
int status;
|
||||||
|
RemoteOperationResult result = null;
|
||||||
|
FileChannel channel;
|
||||||
|
RandomAccessFile raf;
|
||||||
|
|
||||||
|
File fileToUpload = new File(mLocalPath);
|
||||||
|
MediaType mediaType = MediaType.parse(mMimeType);
|
||||||
|
|
||||||
|
raf = new RandomAccessFile(fileToUpload, "r");
|
||||||
|
channel = raf.getChannel();
|
||||||
|
|
||||||
|
mFileRequestBody = new ChunkFromFileRequestBody(fileToUpload, mediaType, channel, CHUNK_SIZE);
|
||||||
|
|
||||||
|
synchronized (mDataTransferListeners) {
|
||||||
|
mFileRequestBody.addDatatransferProgressListeners(mDataTransferListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
long offset = 0;
|
||||||
|
String uriPrefix = client.getUploadsWebDavUri() + File.separator + mTransferId;
|
||||||
|
long totalLength = fileToUpload.length();
|
||||||
|
long chunkCount = (long) Math.ceil((double) totalLength / CHUNK_SIZE);
|
||||||
|
|
||||||
|
for (int chunkIndex = 0; chunkIndex < chunkCount; chunkIndex++, offset += CHUNK_SIZE) {
|
||||||
|
|
||||||
|
((ChunkFromFileRequestBody) mFileRequestBody).setOffset(offset);
|
||||||
|
|
||||||
|
if (mCancellationRequested.get()) {
|
||||||
|
result = new RemoteOperationResult<>(new OperationCancelledException());
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
mPutMethod = new PutMethod(
|
||||||
|
new URL(uriPrefix + File.separator + chunkIndex), mFileRequestBody);
|
||||||
|
|
||||||
|
if (chunkIndex == chunkCount - 1) {
|
||||||
|
// Added a high timeout to the last chunk due to when the last chunk
|
||||||
|
// arrives to the server with the last PUT, all chunks get assembled
|
||||||
|
// within that PHP request, so last one takes longer.
|
||||||
|
mPutMethod.setReadTimeout(LAST_CHUNK_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
status = client.executeHttpMethod(mPutMethod);
|
||||||
|
|
||||||
|
Timber.d("Upload of " + mLocalPath + " to " + mRemotePath +
|
||||||
|
", chunk index " + chunkIndex + ", count " + chunkCount +
|
||||||
|
", HTTP result status " + status);
|
||||||
|
|
||||||
|
if (isSuccess(status)) {
|
||||||
|
result = new RemoteOperationResult<>(OK);
|
||||||
|
} else {
|
||||||
|
result = new RemoteOperationResult<>(mPutMethod);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel != null) {
|
||||||
|
channel.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raf != null) {
|
||||||
|
raf.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -22,8 +22,24 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.owncloud.android.lib.common.utils
|
package com.owncloud.android.lib.resources.files.chunks;
|
||||||
|
|
||||||
fun Any.isOneOf(vararg values: Any): Boolean {
|
import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
|
||||||
return this in values
|
|
||||||
}
|
/**
|
||||||
|
* Remote operation performing the creation of a new folder to save chunks during an upload to the ownCloud server.
|
||||||
|
*
|
||||||
|
* @author David González Verdugo
|
||||||
|
*/
|
||||||
|
public class CreateRemoteChunkFolderOperation extends CreateRemoteFolderOperation {
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath Full path to the new directory to create in the remote server.
|
||||||
|
* @param createFullPath 'True' means that all the ancestor folders should be created.
|
||||||
|
*/
|
||||||
|
public CreateRemoteChunkFolderOperation(String remotePath, boolean createFullPath) {
|
||||||
|
super(remotePath, createFullPath);
|
||||||
|
createChunksFolder = true;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2021 ownCloud GmbH.
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -21,38 +21,30 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.owncloud.android.lib.resources.files.chunks
|
|
||||||
|
|
||||||
import android.net.Uri
|
package com.owncloud.android.lib.resources.files.chunks;
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation;
|
||||||
import com.owncloud.android.lib.common.http.methods.webdav.MoveMethod
|
|
||||||
import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remote operation to move the file built from chunks after uploading it
|
* Remote operation to move the file built from chunks after uploading it
|
||||||
*
|
*
|
||||||
* @author David González Verdugo
|
* @author David González Verdugo
|
||||||
* @author Abel García de Prada
|
|
||||||
*/
|
*/
|
||||||
class MoveRemoteChunksFileOperation(
|
public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation {
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
private val fileLastModificationTimestamp: String,
|
|
||||||
private val fileLength: Long
|
|
||||||
) : MoveRemoteFileOperation(
|
|
||||||
sourceRemotePath = sourceRemotePath,
|
|
||||||
targetRemotePath = targetRemotePath,
|
|
||||||
) {
|
|
||||||
|
|
||||||
override fun getSrcWebDavUriForClient(client: OwnCloudClient): Uri = client.uploadsWebDavUri
|
/**
|
||||||
|
* Constructor.
|
||||||
override fun addRequestHeaders(moveMethod: MoveMethod) {
|
*
|
||||||
super.addRequestHeaders(moveMethod)
|
* @param srcRemotePath Remote path of the file/folder to move.
|
||||||
|
* @param targetRemotePath Remove path desired for the file/folder after moving it.
|
||||||
moveMethod.apply {
|
* @param overwrite
|
||||||
addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, fileLastModificationTimestamp)
|
*/
|
||||||
addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, fileLength.toString())
|
public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite,
|
||||||
}
|
String fileLastModifTimestamp, long fileLength) {
|
||||||
|
super(srcRemotePath, targetRemotePath, overwrite);
|
||||||
|
moveChunkedFile = true;
|
||||||
|
mFileLastModifTimestamp = fileLastModifTimestamp;
|
||||||
|
mFileLength = fileLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,11 +22,20 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package com.owncloud.android.lib.resources.files.chunks
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
package com.owncloud.android.lib.resources.files.chunks;
|
||||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation
|
|
||||||
|
|
||||||
class RemoveRemoteChunksFolderOperation(remotePath: String) : RemoveRemoteFileOperation(remotePath) {
|
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation;
|
||||||
override fun getSrcWebDavUriForClient(client: OwnCloudClient): String = client.uploadsWebDavUri.toString()
|
|
||||||
}
|
public class RemoveRemoteChunksFolderOperation extends RemoveRemoteFileOperation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param remotePath RemotePath of the remote file or folder to remove from the server
|
||||||
|
*/
|
||||||
|
public RemoveRemoteChunksFolderOperation(String remotePath) {
|
||||||
|
super(remotePath);
|
||||||
|
removeChunksFolder = true;
|
||||||
|
}
|
||||||
|
}
|
@ -1,41 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2021 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.resources.files.services
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.Service
|
|
||||||
|
|
||||||
interface ChunkService : Service {
|
|
||||||
fun removeFile(
|
|
||||||
remotePath: String
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
fun moveFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
fileLastModificationTimestamp: String,
|
|
||||||
fileLength: Long
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2023 ownCloud GmbH.
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -25,62 +25,7 @@ package com.owncloud.android.lib.resources.files.services
|
|||||||
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
import com.owncloud.android.lib.resources.Service
|
import com.owncloud.android.lib.resources.Service
|
||||||
import com.owncloud.android.lib.resources.files.RemoteFile
|
|
||||||
|
|
||||||
interface FileService : Service {
|
interface FileService : Service {
|
||||||
fun checkPathExistence(
|
fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Boolean>
|
||||||
path: String,
|
|
||||||
isUserLogged: Boolean,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<Boolean>
|
|
||||||
|
|
||||||
fun copyFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
sourceSpaceWebDavUrl: String?,
|
|
||||||
targetSpaceWebDavUrl: String?,
|
|
||||||
replace: Boolean,
|
|
||||||
): RemoteOperationResult<String?>
|
|
||||||
|
|
||||||
fun createFolder(
|
|
||||||
remotePath: String,
|
|
||||||
createFullPath: Boolean,
|
|
||||||
isChunkFolder: Boolean = false,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
fun downloadFile(
|
|
||||||
remotePath: String,
|
|
||||||
localTempPath: String
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
fun moveFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
replace: Boolean,
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
fun readFile(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<RemoteFile>
|
|
||||||
|
|
||||||
fun refreshFolder(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<ArrayList<RemoteFile>>
|
|
||||||
|
|
||||||
fun removeFile(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
fun renameFile(
|
|
||||||
oldName: String,
|
|
||||||
oldRemotePath: String,
|
|
||||||
newName: String,
|
|
||||||
isFolder: Boolean,
|
|
||||||
spaceWebDavUrl: String? = null,
|
|
||||||
): RemoteOperationResult<Unit>
|
|
||||||
}
|
}
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2021 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.resources.files.services.implementation
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.files.chunks.MoveRemoteChunksFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.chunks.RemoveRemoteChunksFolderOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.services.ChunkService
|
|
||||||
|
|
||||||
class OCChunkService(override val client: OwnCloudClient) : ChunkService {
|
|
||||||
|
|
||||||
override fun removeFile(remotePath: String): RemoteOperationResult<Unit> =
|
|
||||||
RemoveRemoteChunksFolderOperation(remotePath = remotePath).execute(client)
|
|
||||||
|
|
||||||
override fun moveFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
fileLastModificationTimestamp: String,
|
|
||||||
fileLength: Long
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
MoveRemoteChunksFileOperation(
|
|
||||||
sourceRemotePath = sourceRemotePath,
|
|
||||||
targetRemotePath = targetRemotePath,
|
|
||||||
fileLastModificationTimestamp = fileLastModificationTimestamp,
|
|
||||||
fileLength = fileLength,
|
|
||||||
).execute(client)
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2023 ownCloud GmbH.
|
* Copyright (C) 2022 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@ -26,118 +26,13 @@ package com.owncloud.android.lib.resources.files.services.implementation
|
|||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
|
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
|
||||||
import com.owncloud.android.lib.resources.files.CopyRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.RemoteFile
|
|
||||||
import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation
|
|
||||||
import com.owncloud.android.lib.resources.files.services.FileService
|
import com.owncloud.android.lib.resources.files.services.FileService
|
||||||
|
|
||||||
class OCFileService(override val client: OwnCloudClient) : FileService {
|
class OCFileService(override val client: OwnCloudClient) :
|
||||||
override fun checkPathExistence(
|
FileService {
|
||||||
path: String,
|
override fun checkPathExistence(path: String, isUserLogged: Boolean): RemoteOperationResult<Boolean> =
|
||||||
isUserLogged: Boolean,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<Boolean> =
|
|
||||||
CheckPathExistenceRemoteOperation(
|
CheckPathExistenceRemoteOperation(
|
||||||
remotePath = path,
|
remotePath = path,
|
||||||
isUserLoggedIn = isUserLogged,
|
isUserLoggedIn = isUserLogged
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun copyFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
sourceSpaceWebDavUrl: String?,
|
|
||||||
targetSpaceWebDavUrl: String?,
|
|
||||||
replace: Boolean,
|
|
||||||
): RemoteOperationResult<String?> =
|
|
||||||
CopyRemoteFileOperation(
|
|
||||||
sourceRemotePath = sourceRemotePath,
|
|
||||||
targetRemotePath = targetRemotePath,
|
|
||||||
sourceSpaceWebDavUrl = sourceSpaceWebDavUrl,
|
|
||||||
targetSpaceWebDavUrl = targetSpaceWebDavUrl,
|
|
||||||
forceOverride = replace,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun createFolder(
|
|
||||||
remotePath: String,
|
|
||||||
createFullPath: Boolean,
|
|
||||||
isChunkFolder: Boolean,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
CreateRemoteFolderOperation(
|
|
||||||
remotePath = remotePath,
|
|
||||||
createFullPath = createFullPath,
|
|
||||||
isChunksFolder = isChunkFolder,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun downloadFile(
|
|
||||||
remotePath: String,
|
|
||||||
localTempPath: String
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
DownloadRemoteFileOperation(
|
|
||||||
remotePath = remotePath,
|
|
||||||
localFolderPath = localTempPath
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun moveFile(
|
|
||||||
sourceRemotePath: String,
|
|
||||||
targetRemotePath: String,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
replace: Boolean,
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
MoveRemoteFileOperation(
|
|
||||||
sourceRemotePath = sourceRemotePath,
|
|
||||||
targetRemotePath = targetRemotePath,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
forceOverride = replace,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun readFile(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<RemoteFile> =
|
|
||||||
ReadRemoteFileOperation(
|
|
||||||
remotePath = remotePath,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun refreshFolder(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<ArrayList<RemoteFile>> =
|
|
||||||
ReadRemoteFolderOperation(
|
|
||||||
remotePath = remotePath,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun removeFile(
|
|
||||||
remotePath: String,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
RemoveRemoteFileOperation(
|
|
||||||
remotePath = remotePath,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
|
||||||
|
|
||||||
override fun renameFile(
|
|
||||||
oldName: String,
|
|
||||||
oldRemotePath: String,
|
|
||||||
newName: String,
|
|
||||||
isFolder: Boolean,
|
|
||||||
spaceWebDavUrl: String?,
|
|
||||||
): RemoteOperationResult<Unit> =
|
|
||||||
RenameRemoteFileOperation(
|
|
||||||
oldName = oldName,
|
|
||||||
oldRemotePath = oldRemotePath,
|
|
||||||
newName = newName,
|
|
||||||
isFolder = isFolder,
|
|
||||||
spaceWebDavUrl = spaceWebDavUrl,
|
|
||||||
).execute(client)
|
).execute(client)
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,8 @@ class CreateRemoteShareOperation(
|
|||||||
|
|
||||||
var expirationDateInMillis: Long = INIT_EXPIRATION_DATE_IN_MILLIS // Expiration date to set for the public link
|
var expirationDateInMillis: Long = INIT_EXPIRATION_DATE_IN_MILLIS // Expiration date to set for the public link
|
||||||
|
|
||||||
|
var publicUpload: Boolean = false // Upload permissions for the public link (only folders)
|
||||||
|
|
||||||
var retrieveShareDetails = false // To retrieve more info about the just created share
|
var retrieveShareDetails = false // To retrieve more info about the just created share
|
||||||
|
|
||||||
private fun buildRequestUri(baseUri: Uri) =
|
private fun buildRequestUri(baseUri: Uri) =
|
||||||
@ -97,7 +99,7 @@ class CreateRemoteShareOperation(
|
|||||||
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private fun parseResponse(response: String): ShareResponse {
|
private fun parseResponse(response: String): ShareResponse? {
|
||||||
val moshi = Moshi.Builder().build()
|
val moshi = Moshi.Builder().build()
|
||||||
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java)
|
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java)
|
||||||
val adapter: JsonAdapter<CommonOcsResponse<ShareItem>> = moshi.adapter(commonOcsType)
|
val adapter: JsonAdapter<CommonOcsResponse<ShareItem>> = moshi.adapter(commonOcsType)
|
||||||
@ -153,10 +155,12 @@ class CreateRemoteShareOperation(
|
|||||||
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
|
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (publicUpload) {
|
||||||
|
formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString())
|
||||||
|
}
|
||||||
if (password.isNotEmpty()) {
|
if (password.isNotEmpty()) {
|
||||||
formBodyBuilder.add(PARAM_PASSWORD, password)
|
formBodyBuilder.add(PARAM_PASSWORD, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RemoteShare.DEFAULT_PERMISSION != permissions) {
|
if (RemoteShare.DEFAULT_PERMISSION != permissions) {
|
||||||
formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
|
formBodyBuilder.add(PARAM_PERMISSIONS, permissions.toString())
|
||||||
}
|
}
|
||||||
@ -203,6 +207,7 @@ class CreateRemoteShareOperation(
|
|||||||
private const val PARAM_SHARE_TYPE = "shareType"
|
private const val PARAM_SHARE_TYPE = "shareType"
|
||||||
private const val PARAM_SHARE_WITH = "shareWith"
|
private const val PARAM_SHARE_WITH = "shareWith"
|
||||||
private const val PARAM_PASSWORD = "password"
|
private const val PARAM_PASSWORD = "password"
|
||||||
|
private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
|
||||||
private const val PARAM_PERMISSIONS = "permissions"
|
private const val PARAM_PERMISSIONS = "permissions"
|
||||||
|
|
||||||
//Arguments - constant values
|
//Arguments - constant values
|
||||||
|
@ -99,11 +99,11 @@ class GetRemoteShareesOperation
|
|||||||
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
|
.appendQueryParameter(PARAM_PER_PAGE, perPage.toString())
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private fun parseResponse(response: String?): ShareeOcsResponse? {
|
private fun parseResponse(response: String): ShareeOcsResponse? {
|
||||||
val moshi = Moshi.Builder().build()
|
val moshi = Moshi.Builder().build()
|
||||||
val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareeOcsResponse::class.java)
|
val type: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareeOcsResponse::class.java)
|
||||||
val adapter: JsonAdapter<CommonOcsResponse<ShareeOcsResponse>> = moshi.adapter(type)
|
val adapter: JsonAdapter<CommonOcsResponse<ShareeOcsResponse>> = moshi.adapter(type)
|
||||||
return response?.let { adapter.fromJson(it)?.ocs?.data }
|
return adapter.fromJson(response)!!.ocs.data
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onResultUnsuccessful(
|
private fun onResultUnsuccessful(
|
||||||
@ -123,7 +123,7 @@ class GetRemoteShareesOperation
|
|||||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareeOcsResponse> {
|
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareeOcsResponse> {
|
||||||
val result = RemoteOperationResult<ShareeOcsResponse>(OK)
|
val result = RemoteOperationResult<ShareeOcsResponse>(OK)
|
||||||
Timber.d("Successful response: $response")
|
Timber.d("Successful response: $response")
|
||||||
result.data = parseResponse(response)
|
result.data = parseResponse(response!!)
|
||||||
Timber.d("*** Get Users or groups completed ")
|
Timber.d("*** Get Users or groups completed ")
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,13 @@ import com.owncloud.android.lib.common.http.HttpConstants.VALUE_FORMAT
|
|||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
|
import com.owncloud.android.lib.common.http.methods.nonwebdav.DeleteMethod
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
import com.owncloud.android.lib.common.operations.RemoteOperation
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
|
import com.owncloud.android.lib.resources.CommonOcsResponse
|
||||||
|
import com.owncloud.android.lib.resources.shares.responses.ShareItem
|
||||||
|
import com.squareup.moshi.JsonAdapter
|
||||||
|
import com.squareup.moshi.Moshi
|
||||||
|
import com.squareup.moshi.Types
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
import java.lang.reflect.Type
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,10 +52,14 @@ import java.net.URL
|
|||||||
* @author David A. Velasco
|
* @author David A. Velasco
|
||||||
* @author David González Verdugo
|
* @author David González Verdugo
|
||||||
* @author Fernando Sanz Velasco
|
* @author Fernando Sanz Velasco
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param remoteShareId Share ID
|
* @param remoteShareId Share ID
|
||||||
*/
|
*/
|
||||||
class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation<Unit>() {
|
class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOperation<ShareResponse>() {
|
||||||
|
|
||||||
private fun buildRequestUri(baseUri: Uri) =
|
private fun buildRequestUri(baseUri: Uri) =
|
||||||
baseUri.buildUpon()
|
baseUri.buildUpon()
|
||||||
@ -58,12 +68,24 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
|||||||
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
private fun parseResponse(response: String): ShareResponse? {
|
||||||
|
val moshi = Moshi.Builder().build()
|
||||||
|
val listOfShareItemType: Type = Types.newParameterizedType(List::class.java, ShareItem::class.java)
|
||||||
|
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, listOfShareItemType)
|
||||||
|
val adapter: JsonAdapter<CommonOcsResponse<List<ShareItem>>> = moshi.adapter(commonOcsType)
|
||||||
|
return adapter.fromJson(response)?.ocs?.data?.let { listOfShareItems ->
|
||||||
|
ShareResponse(listOfShareItems.map { shareItem ->
|
||||||
|
shareItem.toRemoteShare()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun onResultUnsuccessful(
|
private fun onResultUnsuccessful(
|
||||||
method: DeleteMethod,
|
method: DeleteMethod,
|
||||||
response: String?,
|
response: String?,
|
||||||
status: Int
|
status: Int
|
||||||
): RemoteOperationResult<Unit> {
|
): RemoteOperationResult<ShareResponse> {
|
||||||
Timber.e("Failed response while removing share ")
|
Timber.e("Failed response while unshare link ")
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Timber.e("*** status code: $status; response message: $response")
|
Timber.e("*** status code: $status; response message: $response")
|
||||||
} else {
|
} else {
|
||||||
@ -72,14 +94,17 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
|||||||
return RemoteOperationResult(method)
|
return RemoteOperationResult(method)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<Unit> {
|
private fun onRequestSuccessful(response: String?): RemoteOperationResult<ShareResponse> {
|
||||||
val result = RemoteOperationResult<Unit>(RemoteOperationResult.ResultCode.OK)
|
val result = RemoteOperationResult<ShareResponse>(RemoteOperationResult.ResultCode.OK)
|
||||||
Timber.d("Successful response: $response")
|
Timber.d("Successful response: $response")
|
||||||
|
result.data = parseResponse(response!!)
|
||||||
Timber.d("*** Unshare link completed ")
|
Timber.d("*** Unshare link completed ")
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
override fun run(client: OwnCloudClient): RemoteOperationResult<ShareResponse> {
|
||||||
|
|
||||||
|
|
||||||
val requestUri = buildRequestUri(client.baseUri)
|
val requestUri = buildRequestUri(client.baseUri)
|
||||||
|
|
||||||
val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply {
|
val deleteMethod = DeleteMethod(URL(requestUri.toString())).apply {
|
||||||
@ -104,7 +129,7 @@ class RemoveRemoteShareOperation(private val remoteShareId: String) : RemoteOper
|
|||||||
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// OCS Route
|
//OCS Route
|
||||||
private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares"
|
private const val OCS_ROUTE = "ocs/v2.php/apps/files_sharing/api/v1/shares"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,6 +103,13 @@ class UpdateRemoteShareOperation
|
|||||||
*/
|
*/
|
||||||
var permissions: Int = DEFAULT_PERMISSION
|
var permissions: Int = DEFAULT_PERMISSION
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable upload permissions to update in Share resource.
|
||||||
|
*
|
||||||
|
* Null results in no update applied to the upload permission.
|
||||||
|
*/
|
||||||
|
var publicUpload: Boolean? = null
|
||||||
|
|
||||||
var retrieveShareDetails = false // To retrieve more info about the just updated share
|
var retrieveShareDetails = false // To retrieve more info about the just updated share
|
||||||
|
|
||||||
private fun buildRequestUri(baseUri: Uri) =
|
private fun buildRequestUri(baseUri: Uri) =
|
||||||
@ -112,7 +119,7 @@ class UpdateRemoteShareOperation
|
|||||||
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
.appendQueryParameter(PARAM_FORMAT, VALUE_FORMAT)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private fun parseResponse(response: String): ShareResponse {
|
private fun parseResponse(response: String): ShareResponse? {
|
||||||
val moshi = Moshi.Builder().build()
|
val moshi = Moshi.Builder().build()
|
||||||
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java)
|
val commonOcsType: Type = Types.newParameterizedType(CommonOcsResponse::class.java, ShareItem::class.java)
|
||||||
val adapter: JsonAdapter<CommonOcsResponse<ShareItem>> = moshi.adapter(commonOcsType)
|
val adapter: JsonAdapter<CommonOcsResponse<ShareItem>> = moshi.adapter(commonOcsType)
|
||||||
@ -174,6 +181,10 @@ class UpdateRemoteShareOperation
|
|||||||
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
|
formBodyBuilder.add(PARAM_EXPIRATION_DATE, formattedExpirationDate)
|
||||||
} // else, ignore - no update
|
} // else, ignore - no update
|
||||||
|
|
||||||
|
if (publicUpload != null) {
|
||||||
|
formBodyBuilder.add(PARAM_PUBLIC_UPLOAD, publicUpload.toString())
|
||||||
|
}
|
||||||
|
|
||||||
// IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter,
|
// IMPORTANT: permissions parameter needs to be updated after mPublicUpload parameter,
|
||||||
// otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated
|
// otherwise they would be set always as 1 (READ) in the server when mPublicUpload was updated
|
||||||
if (permissions > DEFAULT_PERMISSION) {
|
if (permissions > DEFAULT_PERMISSION) {
|
||||||
@ -222,6 +233,7 @@ class UpdateRemoteShareOperation
|
|||||||
private const val PARAM_PASSWORD = "password"
|
private const val PARAM_PASSWORD = "password"
|
||||||
private const val PARAM_EXPIRATION_DATE = "expireDate"
|
private const val PARAM_EXPIRATION_DATE = "expireDate"
|
||||||
private const val PARAM_PERMISSIONS = "permissions"
|
private const val PARAM_PERMISSIONS = "permissions"
|
||||||
|
private const val PARAM_PUBLIC_UPLOAD = "publicUpload"
|
||||||
|
|
||||||
//Arguments - constant values
|
//Arguments - constant values
|
||||||
private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"
|
private const val FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"
|
||||||
|
@ -46,6 +46,7 @@ interface ShareService : Service {
|
|||||||
name: String,
|
name: String,
|
||||||
password: String,
|
password: String,
|
||||||
expirationDate: Long,
|
expirationDate: Long,
|
||||||
|
publicUpload: Boolean
|
||||||
): RemoteOperationResult<ShareResponse>
|
): RemoteOperationResult<ShareResponse>
|
||||||
|
|
||||||
fun updateShare(
|
fun updateShare(
|
||||||
@ -54,7 +55,8 @@ interface ShareService : Service {
|
|||||||
password: String?,
|
password: String?,
|
||||||
expirationDate: Long,
|
expirationDate: Long,
|
||||||
permissions: Int,
|
permissions: Int,
|
||||||
|
publicUpload: Boolean
|
||||||
): RemoteOperationResult<ShareResponse>
|
): RemoteOperationResult<ShareResponse>
|
||||||
|
|
||||||
fun deleteShare(remoteId: String): RemoteOperationResult<Unit>
|
fun deleteShare(remoteId: String): RemoteOperationResult<ShareResponse>
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,8 @@ import com.owncloud.android.lib.resources.shares.ShareType
|
|||||||
import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation
|
import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation
|
||||||
import com.owncloud.android.lib.resources.shares.services.ShareService
|
import com.owncloud.android.lib.resources.shares.services.ShareService
|
||||||
|
|
||||||
class OCShareService(override val client: OwnCloudClient) : ShareService {
|
class OCShareService(override val client: OwnCloudClient) :
|
||||||
|
ShareService {
|
||||||
override fun getShares(
|
override fun getShares(
|
||||||
remoteFilePath: String,
|
remoteFilePath: String,
|
||||||
reshares: Boolean,
|
reshares: Boolean,
|
||||||
@ -55,6 +56,7 @@ class OCShareService(override val client: OwnCloudClient) : ShareService {
|
|||||||
name: String,
|
name: String,
|
||||||
password: String,
|
password: String,
|
||||||
expirationDate: Long,
|
expirationDate: Long,
|
||||||
|
publicUpload: Boolean
|
||||||
): RemoteOperationResult<ShareResponse> =
|
): RemoteOperationResult<ShareResponse> =
|
||||||
CreateRemoteShareOperation(
|
CreateRemoteShareOperation(
|
||||||
remoteFilePath,
|
remoteFilePath,
|
||||||
@ -65,6 +67,7 @@ class OCShareService(override val client: OwnCloudClient) : ShareService {
|
|||||||
this.name = name
|
this.name = name
|
||||||
this.password = password
|
this.password = password
|
||||||
this.expirationDateInMillis = expirationDate
|
this.expirationDateInMillis = expirationDate
|
||||||
|
this.publicUpload = publicUpload
|
||||||
this.retrieveShareDetails = true
|
this.retrieveShareDetails = true
|
||||||
}.execute(client)
|
}.execute(client)
|
||||||
|
|
||||||
@ -74,6 +77,7 @@ class OCShareService(override val client: OwnCloudClient) : ShareService {
|
|||||||
password: String?,
|
password: String?,
|
||||||
expirationDate: Long,
|
expirationDate: Long,
|
||||||
permissions: Int,
|
permissions: Int,
|
||||||
|
publicUpload: Boolean
|
||||||
): RemoteOperationResult<ShareResponse> =
|
): RemoteOperationResult<ShareResponse> =
|
||||||
UpdateRemoteShareOperation(
|
UpdateRemoteShareOperation(
|
||||||
remoteId
|
remoteId
|
||||||
@ -82,10 +86,11 @@ class OCShareService(override val client: OwnCloudClient) : ShareService {
|
|||||||
this.password = password
|
this.password = password
|
||||||
this.expirationDateInMillis = expirationDate
|
this.expirationDateInMillis = expirationDate
|
||||||
this.permissions = permissions
|
this.permissions = permissions
|
||||||
|
this.publicUpload = publicUpload
|
||||||
this.retrieveShareDetails = true
|
this.retrieveShareDetails = true
|
||||||
}.execute(client)
|
}.execute(client)
|
||||||
|
|
||||||
override fun deleteShare(remoteId: String): RemoteOperationResult<Unit> =
|
override fun deleteShare(remoteId: String): RemoteOperationResult<ShareResponse> =
|
||||||
RemoveRemoteShareOperation(
|
RemoveRemoteShareOperation(
|
||||||
remoteId
|
remoteId
|
||||||
).execute(client)
|
).execute(client)
|
||||||
|
@ -1,99 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.resources.spaces
|
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
|
|
||||||
import com.owncloud.android.lib.resources.spaces.responses.SpacesResponseWrapper
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class GetRemoteSpacesOperation : RemoteOperation<List<SpaceResponse>>() {
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<List<SpaceResponse>> {
|
|
||||||
val requestUri = buildRequestUri(client.baseUri)
|
|
||||||
|
|
||||||
val getMethod = GetMethod(URL(requestUri.toString()))
|
|
||||||
|
|
||||||
return try {
|
|
||||||
val status = client.executeHttpMethod(getMethod)
|
|
||||||
val response = getMethod.getResponseBodyAsString()
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
onRequestSuccessful(response)
|
|
||||||
} else {
|
|
||||||
onResultUnsuccessful(getMethod, response, status)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e, "Exception while getting remote shares")
|
|
||||||
RemoteOperationResult(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun buildRequestUri(baseUri: Uri) =
|
|
||||||
baseUri.buildUpon()
|
|
||||||
.appendEncodedPath(GRAPH_API_PATH)
|
|
||||||
.appendEncodedPath(ENDPOINT_SPACES_LIST)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
private fun parseResponse(response: String): List<SpaceResponse> {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
val adapter: JsonAdapter<SpacesResponseWrapper> = moshi.adapter(SpacesResponseWrapper::class.java)
|
|
||||||
return adapter.fromJson(response)?.value ?: listOf()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onResultUnsuccessful(
|
|
||||||
method: GetMethod,
|
|
||||||
response: String?,
|
|
||||||
status: Int
|
|
||||||
): RemoteOperationResult<List<SpaceResponse>> {
|
|
||||||
Timber.e("Failed response while getting spaces for user")
|
|
||||||
if (response != null) {
|
|
||||||
Timber.e("*** status code: $status; response message: $response")
|
|
||||||
} else {
|
|
||||||
Timber.e("*** status code: $status")
|
|
||||||
}
|
|
||||||
return RemoteOperationResult(method)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onRequestSuccessful(response: String?): RemoteOperationResult<List<SpaceResponse>> {
|
|
||||||
val result = RemoteOperationResult<List<SpaceResponse>>(RemoteOperationResult.ResultCode.OK)
|
|
||||||
Timber.d("Successful response: $response")
|
|
||||||
result.data = response?.let { parseResponse(it) } ?: listOf()
|
|
||||||
Timber.d("*** Fetch of spaces completed and parsed to ${result.data}")
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int) = status == HttpConstants.HTTP_OK
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val GRAPH_API_PATH = "graph/v1.0"
|
|
||||||
private const val ENDPOINT_SPACES_LIST = "me/drives"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.resources.spaces.responses
|
|
||||||
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class SpacesResponseWrapper(
|
|
||||||
val value: List<SpaceResponse>
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class SpaceResponse(
|
|
||||||
val description: String?,
|
|
||||||
val driveAlias: String,
|
|
||||||
val driveType: String,
|
|
||||||
val id: String,
|
|
||||||
val lastModifiedDateTime: String?,
|
|
||||||
val name: String,
|
|
||||||
val owner: OwnerResponse?,
|
|
||||||
val quota: QuotaResponse?,
|
|
||||||
val root: RootResponse,
|
|
||||||
val special: List<SpecialResponse>?,
|
|
||||||
val webUrl: String,
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class OwnerResponse(
|
|
||||||
val user: UserResponse
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class QuotaResponse(
|
|
||||||
val remaining: Long?,
|
|
||||||
val state: String?,
|
|
||||||
val total: Long,
|
|
||||||
val used: Long?,
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class RootResponse(
|
|
||||||
val eTag: String?,
|
|
||||||
val id: String,
|
|
||||||
val webDavUrl: String,
|
|
||||||
val deleted: DeleteResponse?,
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class SpecialResponse(
|
|
||||||
val eTag: String,
|
|
||||||
val file: FileResponse,
|
|
||||||
val id: String,
|
|
||||||
val lastModifiedDateTime: String,
|
|
||||||
val name: String,
|
|
||||||
val size: Int,
|
|
||||||
val specialFolder: SpecialFolderResponse,
|
|
||||||
val webDavUrl: String
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class UserResponse(
|
|
||||||
val id: String
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class FileResponse(
|
|
||||||
val mimeType: String
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class DeleteResponse(
|
|
||||||
val state: String,
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class SpecialFolderResponse(
|
|
||||||
val name: String
|
|
||||||
)
|
|
@ -1,31 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
* Copyright (C) 2022 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.resources.spaces.services
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.Service
|
|
||||||
import com.owncloud.android.lib.resources.spaces.responses.SpaceResponse
|
|
||||||
|
|
||||||
interface SpacesService : Service {
|
|
||||||
fun getSpaces(): RemoteOperationResult<List<SpaceResponse>>
|
|
||||||
}
|
|
@ -45,7 +45,7 @@ import java.net.URL
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Capabilities from the server
|
* Get the Capabilities from the server
|
||||||
* Save Result.getData in a RemoteCapability object
|
* Save in Result.getData in a RemoteCapability object
|
||||||
*
|
*
|
||||||
* @author masensio
|
* @author masensio
|
||||||
* @author David González Verdugo
|
* @author David González Verdugo
|
||||||
|
@ -1,33 +1,29 @@
|
|||||||
/**
|
/* ownCloud Android Library is available under MIT license
|
||||||
* ownCloud Android Library is available under MIT license
|
* @author masensio
|
||||||
|
* @author David González Verdugo
|
||||||
|
* @author Abel García de Prada
|
||||||
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* @author masensio
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* @author David González Verdugo
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* @author Abel García de Prada
|
* in the Software without restriction, including without limitation the rights
|
||||||
* @author Juan Carlos Garrote Gascón
|
* 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:
|
||||||
*
|
*
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
* in the Software without restriction, including without limitation the rights
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
* furnished to do so, subject to the following conditions:
|
* 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.
|
||||||
*
|
*
|
||||||
* 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.resources.status
|
package com.owncloud.android.lib.resources.status
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,7 +33,7 @@ data class RemoteCapability(
|
|||||||
var accountName: String = "",
|
var accountName: String = "",
|
||||||
|
|
||||||
// Server version
|
// Server version
|
||||||
var versionMajor: Int = 0,
|
var versionMayor: Int = 0,
|
||||||
var versionMinor: Int = 0,
|
var versionMinor: Int = 0,
|
||||||
var versionMicro: Int = 0,
|
var versionMicro: Int = 0,
|
||||||
var versionString: String = "",
|
var versionString: String = "",
|
||||||
@ -70,12 +66,7 @@ data class RemoteCapability(
|
|||||||
// Files
|
// Files
|
||||||
var filesBigFileChunking: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
var filesBigFileChunking: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
||||||
var filesUndelete: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
var filesUndelete: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
||||||
var filesVersioning: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
var filesVersioning: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN
|
||||||
val filesPrivateLinks: CapabilityBooleanType = CapabilityBooleanType.UNKNOWN,
|
|
||||||
val filesAppProviders: List<RemoteAppProviders>?,
|
|
||||||
|
|
||||||
// Spaces
|
|
||||||
val spaces: RemoteSpaces?,
|
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* Enum for Boolean Type in capabilities, with values:
|
* Enum for Boolean Type in capabilities, with values:
|
||||||
@ -107,19 +98,4 @@ data class RemoteCapability(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data class RemoteAppProviders(
|
|
||||||
val enabled: Boolean,
|
|
||||||
val version: String,
|
|
||||||
val appsUrl: String?,
|
|
||||||
val openUrl: String?,
|
|
||||||
val openWebUrl: String?,
|
|
||||||
val newUrl: String?,
|
|
||||||
)
|
|
||||||
|
|
||||||
data class RemoteSpaces(
|
|
||||||
val enabled: Boolean,
|
|
||||||
val projects: Boolean,
|
|
||||||
val shareJail: Boolean,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,31 @@
|
|||||||
/**
|
/* ownCloud Android Library is available under MIT license
|
||||||
* ownCloud Android Library is available under MIT license
|
* @author Abel García de Prada
|
||||||
|
* Copyright (C) 2020 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
* @author Abel García de Prada
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* @author Juan Carlos Garrote Gascón
|
* 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:
|
||||||
*
|
*
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
* in the Software without restriction, including without limitation the rights
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
* furnished to do so, subject to the following conditions:
|
* 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.
|
||||||
*
|
*
|
||||||
* 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.resources.status.responses
|
package com.owncloud.android.lib.resources.status.responses
|
||||||
|
|
||||||
import com.owncloud.android.lib.resources.status.RemoteCapability
|
import com.owncloud.android.lib.resources.status.RemoteCapability
|
||||||
import com.owncloud.android.lib.resources.status.RemoteCapability.*
|
import com.owncloud.android.lib.resources.status.RemoteCapability.CapabilityBooleanType
|
||||||
import com.squareup.moshi.Json
|
import com.squareup.moshi.Json
|
||||||
import com.squareup.moshi.JsonClass
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
@ -40,7 +36,7 @@ data class CapabilityResponse(
|
|||||||
val capabilities: Capabilities?
|
val capabilities: Capabilities?
|
||||||
) {
|
) {
|
||||||
fun toRemoteCapability(): RemoteCapability = RemoteCapability(
|
fun toRemoteCapability(): RemoteCapability = RemoteCapability(
|
||||||
versionMajor = serverVersion?.major ?: 0,
|
versionMayor = serverVersion?.major ?: 0,
|
||||||
versionMinor = serverVersion?.minor ?: 0,
|
versionMinor = serverVersion?.minor ?: 0,
|
||||||
versionMicro = serverVersion?.micro ?: 0,
|
versionMicro = serverVersion?.micro ?: 0,
|
||||||
versionString = serverVersion?.string ?: "",
|
versionString = serverVersion?.string ?: "",
|
||||||
@ -72,12 +68,9 @@ data class CapabilityResponse(
|
|||||||
filesBigFileChunking = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.bigfilechunking),
|
filesBigFileChunking = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.bigfilechunking),
|
||||||
filesUndelete = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.undelete),
|
filesUndelete = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.undelete),
|
||||||
filesVersioning = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.versioning),
|
filesVersioning = CapabilityBooleanType.fromBooleanValue(capabilities?.fileCapabilities?.versioning),
|
||||||
filesPrivateLinks = capabilities?.fileCapabilities?.privateLinks?.let { CapabilityBooleanType.fromBooleanValue(it) } ?: CapabilityBooleanType.UNKNOWN,
|
|
||||||
filesAppProviders = capabilities?.fileCapabilities?.appProviders?.map { it.toAppProviders() },
|
|
||||||
filesSharingFederationIncoming = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.incoming),
|
filesSharingFederationIncoming = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.incoming),
|
||||||
filesSharingFederationOutgoing = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.outgoing),
|
filesSharingFederationOutgoing = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingFederation?.outgoing),
|
||||||
filesSharingUserProfilePicture = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingUser?.profilePicture),
|
filesSharingUserProfilePicture = CapabilityBooleanType.fromBooleanValue(capabilities?.fileSharingCapabilities?.fileSharingUser?.profilePicture),
|
||||||
spaces = capabilities?.spacesCapabilities?.toSpaces(),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,9 +83,7 @@ data class Capabilities(
|
|||||||
@Json(name = "files")
|
@Json(name = "files")
|
||||||
val fileCapabilities: FileCapabilities?,
|
val fileCapabilities: FileCapabilities?,
|
||||||
@Json(name = "dav")
|
@Json(name = "dav")
|
||||||
val davCapabilities: DavCapabilities?,
|
val davCapabilities: DavCapabilities?
|
||||||
@Json(name = "spaces")
|
|
||||||
val spacesCapabilities: SpacesCapabilities?,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
@ -169,43 +160,14 @@ data class FileSharingUser(
|
|||||||
data class FileCapabilities(
|
data class FileCapabilities(
|
||||||
val bigfilechunking: Boolean?,
|
val bigfilechunking: Boolean?,
|
||||||
val undelete: Boolean?,
|
val undelete: Boolean?,
|
||||||
val versioning: Boolean?,
|
val versioning: Boolean?
|
||||||
val privateLinks: Boolean?,
|
|
||||||
@Json(name = "app_providers")
|
|
||||||
val appProviders: List<AppProvider>?
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class AppProvider(
|
|
||||||
val enabled: Boolean,
|
|
||||||
val version: String,
|
|
||||||
@Json(name = "apps_url")
|
|
||||||
val appsUrl: String?,
|
|
||||||
@Json(name = "open_url")
|
|
||||||
val openUrl: String?,
|
|
||||||
@Json(name = "open_web_url")
|
|
||||||
val openWebUrl: String?,
|
|
||||||
@Json(name = "new_url")
|
|
||||||
val newUrl: String?,
|
|
||||||
) {
|
|
||||||
fun toAppProviders() = RemoteAppProviders(enabled, version, appsUrl, openUrl, openWebUrl, newUrl)
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class DavCapabilities(
|
data class DavCapabilities(
|
||||||
val chunking: String?
|
val chunking: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class SpacesCapabilities(
|
|
||||||
val enabled: Boolean,
|
|
||||||
val projects: Boolean,
|
|
||||||
@Json(name = "share_jail")
|
|
||||||
val shareJail: Boolean,
|
|
||||||
) {
|
|
||||||
fun toSpaces() = RemoteSpaces(enabled, projects, shareJail)
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
@JsonClass(generateAdapter = true)
|
||||||
data class ServerVersion(
|
data class ServerVersion(
|
||||||
var major: Int?,
|
var major: Int?,
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* Copyright (C) 2022 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
|
* @author David González Verdugo
|
||||||
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
@ -30,7 +32,8 @@ import com.owncloud.android.lib.resources.status.GetRemoteCapabilitiesOperation
|
|||||||
import com.owncloud.android.lib.resources.status.RemoteCapability
|
import com.owncloud.android.lib.resources.status.RemoteCapability
|
||||||
import com.owncloud.android.lib.resources.status.services.CapabilityService
|
import com.owncloud.android.lib.resources.status.services.CapabilityService
|
||||||
|
|
||||||
class OCCapabilityService(override val client: OwnCloudClient) : CapabilityService {
|
class OCCapabilityService(override val client: OwnCloudClient) :
|
||||||
|
CapabilityService {
|
||||||
override fun getCapabilities(): RemoteOperationResult<RemoteCapability> =
|
override fun getCapabilities(): RemoteOperationResult<RemoteCapability> =
|
||||||
GetRemoteCapabilitiesOperation().execute(client)
|
GetRemoteCapabilitiesOperation().execute(client)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
/* ownCloud Android Library is available under MIT license
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
* Copyright (C) 2022 ownCloud GmbH.
|
||||||
*
|
*
|
||||||
|
* @author Abel García de Prada
|
||||||
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
* in the Software without restriction, including without limitation the rights
|
* in the Software without restriction, including without limitation the rights
|
||||||
@ -24,6 +26,7 @@
|
|||||||
|
|
||||||
package com.owncloud.android.lib.resources.status.services.implementation
|
package com.owncloud.android.lib.resources.status.services.implementation
|
||||||
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
import com.owncloud.android.lib.common.OwnCloudClient
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
||||||
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
|
import com.owncloud.android.lib.resources.files.CheckPathExistenceRemoteOperation
|
||||||
@ -36,16 +39,16 @@ class OCServerInfoService : ServerInfoService {
|
|||||||
override fun checkPathExistence(
|
override fun checkPathExistence(
|
||||||
path: String,
|
path: String,
|
||||||
isUserLoggedIn: Boolean,
|
isUserLoggedIn: Boolean,
|
||||||
client: OwnCloudClient,
|
client: OwnCloudClient
|
||||||
): RemoteOperationResult<Boolean> =
|
): RemoteOperationResult<Boolean> =
|
||||||
CheckPathExistenceRemoteOperation(
|
CheckPathExistenceRemoteOperation(
|
||||||
remotePath = path,
|
remotePath = path,
|
||||||
isUserLoggedIn = isUserLoggedIn,
|
isUserLoggedIn = true
|
||||||
).execute(client)
|
).execute(client)
|
||||||
|
|
||||||
override fun getRemoteStatus(
|
override fun getRemoteStatus(
|
||||||
path: String,
|
path: String,
|
||||||
client: OwnCloudClient,
|
client: OwnCloudClient
|
||||||
): RemoteOperationResult<RemoteServerInfo> =
|
): RemoteOperationResult<RemoteServerInfo> =
|
||||||
GetRemoteStatusOperation().execute(client)
|
GetRemoteStatusOperation().execute(client)
|
||||||
}
|
}
|
||||||
|
@ -1,107 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
*
|
|
||||||
* Copyright (C) 2022 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.resources.webfinger
|
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.http.HttpConstants
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.GetMethod
|
|
||||||
import com.owncloud.android.lib.common.http.methods.nonwebdav.HttpMethod
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperation
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.webfinger.responses.WebFingerResponse
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import timber.log.Timber
|
|
||||||
import java.net.URL
|
|
||||||
|
|
||||||
class GetInstancesViaWebFingerOperation(
|
|
||||||
private val lockupServerDomain: String,
|
|
||||||
private val rel: String,
|
|
||||||
private val resource: String,
|
|
||||||
) : RemoteOperation<List<String>>() {
|
|
||||||
|
|
||||||
private fun buildRequestUri() =
|
|
||||||
Uri.parse(lockupServerDomain).buildUpon()
|
|
||||||
.path(ENDPOINT_WEBFINGER_PATH)
|
|
||||||
.appendQueryParameter("rel", rel)
|
|
||||||
.appendQueryParameter("resource", resource)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
private fun isSuccess(status: Int): Boolean = status == HttpConstants.HTTP_OK
|
|
||||||
|
|
||||||
private fun parseResponse(response: String): WebFingerResponse {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
val adapter = moshi.adapter(WebFingerResponse::class.java)
|
|
||||||
return adapter.fromJson(response)!!
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onResultUnsuccessful(
|
|
||||||
method: HttpMethod,
|
|
||||||
response: String?,
|
|
||||||
status: Int
|
|
||||||
): RemoteOperationResult<List<String>> {
|
|
||||||
Timber.e("Failed requesting WebFinger info")
|
|
||||||
if (response != null) {
|
|
||||||
Timber.e("*** status code: $status; response message: $response")
|
|
||||||
} else {
|
|
||||||
Timber.e("*** status code: $status")
|
|
||||||
}
|
|
||||||
return RemoteOperationResult<List<String>>(method)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onRequestSuccessful(rawResponse: String): RemoteOperationResult<List<String>> {
|
|
||||||
val response = parseResponse(rawResponse)
|
|
||||||
Timber.d("Successful WebFinger request: $response")
|
|
||||||
val operationResult = RemoteOperationResult<List<String>>(RemoteOperationResult.ResultCode.OK)
|
|
||||||
operationResult.data = response.links?.map { it.href } ?: listOf()
|
|
||||||
return operationResult
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<List<String>> {
|
|
||||||
val requestUri = buildRequestUri()
|
|
||||||
val getMethod = GetMethod(URL(requestUri.toString()))
|
|
||||||
|
|
||||||
// First iteration won't follow redirections.
|
|
||||||
getMethod.followRedirects = false
|
|
||||||
|
|
||||||
return try {
|
|
||||||
val status = client.executeHttpMethod(getMethod)
|
|
||||||
val response = getMethod.getResponseBodyAsString()!!
|
|
||||||
|
|
||||||
if (isSuccess(status)) {
|
|
||||||
onRequestSuccessful(response)
|
|
||||||
} else {
|
|
||||||
onResultUnsuccessful(getMethod, response, status)
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e, "Requesting WebFinger info failed")
|
|
||||||
RemoteOperationResult<List<String>>(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val ENDPOINT_WEBFINGER_PATH = "/.well-known/webfinger"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/* ownCloud Android Library is available under MIT license
|
|
||||||
*
|
|
||||||
* Copyright (C) 2022 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.resources.webfinger.responses
|
|
||||||
|
|
||||||
import com.squareup.moshi.JsonClass
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class WebFingerResponse(
|
|
||||||
val subject: String,
|
|
||||||
val links: List<LinkItem>?
|
|
||||||
)
|
|
||||||
|
|
||||||
@JsonClass(generateAdapter = true)
|
|
||||||
data class LinkItem(
|
|
||||||
val rel: String,
|
|
||||||
val href: String,
|
|
||||||
)
|
|
@ -1,30 +0,0 @@
|
|||||||
/**
|
|
||||||
* ownCloud Android client application
|
|
||||||
*
|
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License version 2,
|
|
||||||
* as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package com.owncloud.android.lib.resources.webfinger.services
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
|
|
||||||
interface WebFingerService {
|
|
||||||
fun getInstancesFromWebFinger(
|
|
||||||
lookupServer: String,
|
|
||||||
resource: String,
|
|
||||||
rel: String,
|
|
||||||
client: OwnCloudClient,
|
|
||||||
): RemoteOperationResult<List<String>>
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/**
|
|
||||||
* ownCloud Android client application
|
|
||||||
*
|
|
||||||
* Copyright (C) 2022 ownCloud GmbH.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License version 2,
|
|
||||||
* as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package com.owncloud.android.lib.resources.webfinger.services.implementation
|
|
||||||
|
|
||||||
import com.owncloud.android.lib.common.OwnCloudClient
|
|
||||||
import com.owncloud.android.lib.common.operations.RemoteOperationResult
|
|
||||||
import com.owncloud.android.lib.resources.webfinger.GetInstancesViaWebFingerOperation
|
|
||||||
import com.owncloud.android.lib.resources.webfinger.services.WebFingerService
|
|
||||||
|
|
||||||
class OCWebFingerService : WebFingerService {
|
|
||||||
|
|
||||||
override fun getInstancesFromWebFinger(
|
|
||||||
lookupServer: String,
|
|
||||||
resource: String,
|
|
||||||
rel: String,
|
|
||||||
client: OwnCloudClient,
|
|
||||||
): RemoteOperationResult<List<String>> =
|
|
||||||
GetInstancesViaWebFingerOperation(lookupServer, rel, resource).execute(client)
|
|
||||||
}
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -63,31 +63,31 @@ class ShareeResponseTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `example response - ok - correct sturcture`() {
|
fun `example response - ok - correct sturcture`() {
|
||||||
val response = loadResponses(EXAMPLE_RESPONSE_JSON)!!
|
val response = loadResponses(EXAMPLE_RESPONSE_JSON)!!
|
||||||
assertEquals(2, response.ocs.data?.groups?.size)
|
assertEquals(2, response.ocs.data.groups.size)
|
||||||
assertEquals(0, response.ocs.data?.remotes?.size)
|
assertEquals(0, response.ocs.data.remotes.size)
|
||||||
assertEquals(2, response.ocs.data?.users?.size)
|
assertEquals(2, response.ocs.data.users.size)
|
||||||
assertEquals(0, response.ocs.data?.exact?.groups?.size)
|
assertEquals(0, response.ocs.data.exact?.groups?.size)
|
||||||
assertEquals(0, response.ocs.data?.exact?.remotes?.size)
|
assertEquals(0, response.ocs.data.exact?.remotes?.size)
|
||||||
assertEquals(1, response.ocs.data?.exact?.users?.size)
|
assertEquals(1, response.ocs.data.exact?.users?.size)
|
||||||
assertEquals("user1@user1.com", response.ocs.data?.users?.get(0)?.value?.additionalInfo)
|
assertEquals("user1@user1.com", response.ocs.data.users.get(0).value.additionalInfo)
|
||||||
assertNull(response.ocs.data?.users?.get(1)?.value?.additionalInfo)
|
assertNull(response.ocs.data.users[1].value.additionalInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `check empty response - ok - parsing ok`() {
|
fun `check empty response - ok - parsing ok`() {
|
||||||
val response = loadResponses(EMPTY_RESPONSE_JSON)!!
|
val response = loadResponses(EMPTY_RESPONSE_JSON)!!
|
||||||
assertTrue(response.ocs.data?.exact?.groups?.isEmpty()!!)
|
assertTrue(response.ocs.data.exact?.groups?.isEmpty()!!)
|
||||||
assertTrue(response.ocs.data?.exact?.remotes?.isEmpty()!!)
|
assertTrue(response.ocs.data.exact?.remotes?.isEmpty()!!)
|
||||||
assertTrue(response.ocs.data?.exact?.users?.isEmpty()!!)
|
assertTrue(response.ocs.data.exact?.users?.isEmpty()!!)
|
||||||
assertTrue(response.ocs.data?.groups?.isEmpty()!!)
|
assertTrue(response.ocs.data.groups.isEmpty())
|
||||||
assertTrue(response.ocs.data?.remotes?.isEmpty()!!)
|
assertTrue(response.ocs.data.remotes.isEmpty())
|
||||||
assertTrue(response.ocs.data?.users?.isEmpty()!!)
|
assertTrue(response.ocs.data.users.isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val RESOURCES_PATH =
|
val RESOURCES_PATH =
|
||||||
"src/test/responses/com.owncloud.android.lib.resources.sharees.responses"
|
"src/test/responses/com.owncloud.android.lib.resources.sharees.responses"
|
||||||
const val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/example_sharee_response.json"
|
val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/example_sharee_response.json"
|
||||||
const val EMPTY_RESPONSE_JSON = "$RESOURCES_PATH/empty_sharee_response.json"
|
val EMPTY_RESPONSE_JSON = "$RESOURCES_PATH/empty_sharee_response.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,49 +0,0 @@
|
|||||||
package com.owncloud.android.lib.resources.webfinger.responses
|
|
||||||
|
|
||||||
import com.squareup.moshi.JsonAdapter
|
|
||||||
import com.squareup.moshi.JsonDataException
|
|
||||||
import com.squareup.moshi.Moshi
|
|
||||||
import org.junit.Assert
|
|
||||||
import org.junit.Before
|
|
||||||
import org.junit.Test
|
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class WebFingerResponseTest {
|
|
||||||
lateinit var adapter: JsonAdapter<WebFingerResponse>
|
|
||||||
|
|
||||||
private fun loadResponses(fileName: String) = adapter.fromJson(File(fileName).readText())
|
|
||||||
|
|
||||||
@Before
|
|
||||||
fun prepare() {
|
|
||||||
val moshi = Moshi.Builder().build()
|
|
||||||
adapter = moshi.adapter(WebFingerResponse::class.java)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `check rel in too much information - ok`() {
|
|
||||||
val response = loadResponses(TOO_MUCH_INFORMATION_JSON)!!
|
|
||||||
Assert.assertEquals("https://gast.somedomain.de", response.links!![0].href)
|
|
||||||
Assert.assertEquals("http://webfinger.owncloud/rel/server-instance", response.links!![0].rel)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = JsonDataException::class)
|
|
||||||
fun `check key value pairs - ko - no href key`() {
|
|
||||||
val response = loadResponses(BROKEN_JSON)!!
|
|
||||||
Assert.assertEquals("https://gast.somedomain.de", response.links!![0].href)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = JsonDataException::class)
|
|
||||||
fun `check key value pairs - ko - no rel key`() {
|
|
||||||
val response = loadResponses(BROKEN_JSON)!!
|
|
||||||
Assert.assertEquals("https://gast.somedomain.de", response.links!![0].href)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val RESOURCES_PATH =
|
|
||||||
"src/test/responses/com.owncloud.android.lib.resources.webfinger.responses"
|
|
||||||
private const val EXAMPLE_RESPONSE_JSON = "$RESOURCES_PATH/simple_response.json"
|
|
||||||
private const val TOO_MUCH_INFORMATION_JSON = "$RESOURCES_PATH/to_much_information_response.json"
|
|
||||||
private const val BROKEN_JSON = "$RESOURCES_PATH/broken_response.json"
|
|
||||||
private const val NOT_CONTAINING_RELEVANT_INFORMATION_JSON = "$RESOURCES_PATH/not_containing_relevant_info_response.json"
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"subject": "acct:bob@example.com",
|
|
||||||
"aliases": [
|
|
||||||
"https://www.example.com/~bob/"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"http://example.com/ns/role": "employee"
|
|
||||||
},
|
|
||||||
"links": [
|
|
||||||
{
|
|
||||||
"lel": "http://webfinger.example/rel/profile-page",
|
|
||||||
"foo": "https://www.example.com/~bob/"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"subject": "acct:peter.sine@gurken.xxx",
|
|
||||||
"links": [
|
|
||||||
{
|
|
||||||
"rel": "http://webfinger.example/rel/businesscard",
|
|
||||||
"href": "https://www.example.com/~bob/bob.vcf"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
{
|
|
||||||
"links": [
|
|
||||||
{
|
|
||||||
"href": "https://gast.somedomain.de",
|
|
||||||
"rel": "http://webfinger.owncloud/rel/server-instance"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"subject": "acct:peter.sine@gurken.xxx"
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
{
|
|
||||||
"subject": "acct:peter.sine@gurken.xxx",
|
|
||||||
"aliases": [
|
|
||||||
"https://www.example.com/~bob/"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"http://example.com/ns/role": "employee"
|
|
||||||
},
|
|
||||||
"gurken": {
|
|
||||||
"whatever": 42
|
|
||||||
},
|
|
||||||
"links": [
|
|
||||||
{
|
|
||||||
"gurken": "sallat",
|
|
||||||
"href": "https://gast.somedomain.de",
|
|
||||||
"rel": "http://webfinger.owncloud/rel/server-instance"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"gurken": "sallat",
|
|
||||||
"rel": "http://webfinger.example/rel/businesscard",
|
|
||||||
"href": "https://www.example.com/~bob/bob.vcf"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user