Skip to content

Commit 83b68e9

Browse files
Merge pull request #1924 from nextcloud/depocc/DownloadFileRemoteOperation
DownloadFileRemoteOperation uses now NextcloudClient
2 parents 61893bd + e5ca70d commit 83b68e9

4 files changed

Lines changed: 35 additions & 31 deletions

File tree

library/src/androidTest/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperationIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DownloadFileRemoteOperationIT : AbstractIT() {
2727

2828
assertTrue(
2929
DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath)
30-
.execute(client)
30+
.execute(nextcloudClient)
3131
.isSuccess
3232
)
3333

library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package com.nextcloud.common
1111

12+
import com.nextcloud.common.OkHttpMethodBase.Companion.UNKNOWN_STATUS_CODE
1213
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
1314
import com.owncloud.android.lib.common.operations.RemoteOperation
1415
import com.owncloud.android.lib.common.utils.Log_OC
@@ -84,6 +85,8 @@ abstract class OkHttpMethodBase(
8485

8586
fun getResponseBodyAsString(): String = response?.body?.string() ?: ""
8687

88+
fun getResponseBodyAsStream() = response?.body?.byteStream()
89+
8790
fun getResponseContentLength(): Long = response?.body?.contentLength() ?: -1
8891

8992
fun releaseConnection() {

library/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import android.net.Uri;
1717

18-
import androidx.annotation.Nullable;
18+
import com.nextcloud.common.OkHttpMethodBase;
1919

2020
import org.apache.commons.httpclient.Header;
2121
import org.apache.commons.httpclient.HttpMethod;
@@ -28,6 +28,7 @@
2828
import java.util.Date;
2929
import java.util.Locale;
3030

31+
import androidx.annotation.Nullable;
3132
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3233

3334
@SuppressFBWarnings("FS")
@@ -239,12 +240,6 @@ public static String parseEtag(String rawEtag) {
239240
return rawEtag;
240241
}
241242

242-
243-
/**
244-
*
245-
* @param method
246-
* @return
247-
*/
248243
public static String getEtagFromResponse(HttpMethod method) {
249244
Header eTag = method.getResponseHeader("OC-ETag");
250245
if (eTag == null) {
@@ -263,4 +258,21 @@ public static String getEtagFromResponse(HttpMethod method) {
263258
return result;
264259
}
265260

261+
public static String getEtagFromResponse(OkHttpMethodBase method) {
262+
String eTag = method.getResponseHeader("OC-ETag");
263+
if (eTag == null) {
264+
eTag = method.getResponseHeader("oc-etag");
265+
}
266+
if (eTag == null) {
267+
eTag = method.getResponseHeader("ETag");
268+
}
269+
if (eTag == null) {
270+
eTag = method.getResponseHeader("etag");
271+
}
272+
String result = "";
273+
if (eTag != null) {
274+
result = parseEtag(eTag);
275+
}
276+
return result;
277+
}
266278
}

library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
*/
77
package com.owncloud.android.lib.resources.files;
88

9-
import com.owncloud.android.lib.common.OwnCloudClient;
9+
import com.nextcloud.common.NextcloudClient;
10+
import com.nextcloud.operations.GetMethod;
1011
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
1112
import com.owncloud.android.lib.common.network.WebdavUtils;
1213
import com.owncloud.android.lib.common.operations.OperationCancelledException;
1314
import com.owncloud.android.lib.common.operations.RemoteOperation;
1415
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
1516
import com.owncloud.android.lib.common.utils.Log_OC;
1617

17-
import org.apache.commons.httpclient.Header;
1818
import org.apache.commons.httpclient.HttpStatus;
19-
import org.apache.commons.httpclient.methods.GetMethod;
2019

2120
import java.io.BufferedInputStream;
2221
import java.io.File;
@@ -58,7 +57,7 @@ public DownloadFileRemoteOperation(String remotePath, String temporalFolderPath)
5857
}
5958

6059
@Override
61-
protected RemoteOperationResult run(OwnCloudClient client) {
60+
public RemoteOperationResult run(NextcloudClient client) {
6261
RemoteOperationResult result;
6362

6463
/// download will be performed to a temporal file, then moved to the final location
@@ -82,15 +81,15 @@ protected RemoteOperationResult run(OwnCloudClient client) {
8281
}
8382

8483

85-
private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException {
84+
private int downloadFile(NextcloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException {
8685
int status;
8786
boolean savedFile = false;
88-
getMethod = new GetMethod(client.getFilesDavUri(remotePath));
87+
getMethod = new GetMethod(client.getFilesDavUri(remotePath), false);
8988
Iterator<OnDatatransferProgressListener> it;
9089

9190
FileOutputStream fos = null;
9291
try {
93-
status = client.executeMethod(getMethod);
92+
status = client.execute(getMethod);
9493
if (isSuccess(status)) {
9594
try {
9695
targetFile.createNewFile();
@@ -102,17 +101,14 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept
102101
fos = new FileOutputStream(targetFile);
103102
long transferred = 0;
104103

105-
Header contentLength = getMethod.getResponseHeader("Content-Length");
106-
long totalToTransfer = (contentLength != null &&
107-
contentLength.getValue().length() > 0) ?
108-
Long.parseLong(contentLength.getValue()) : 0;
104+
String contentLength = getMethod.getResponseHeader("Content-Length");
105+
long totalToTransfer = (contentLength != null) ? Long.parseLong(contentLength) : 0;
109106

110107
byte[] bytes = new byte[4096];
111108
int readResult;
112109
while ((readResult = bis.read(bytes)) != -1) {
113110
synchronized (mCancellationRequested) {
114111
if (mCancellationRequested.get()) {
115-
getMethod.abort();
116112
throw new OperationCancelledException();
117113
}
118114
}
@@ -128,21 +124,21 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept
128124
}
129125
// Check if the file is completed
130126
// if transfer-encoding: chunked we cannot check if the file is complete
131-
Header transferEncodingHeader = getMethod.getResponseHeader("Transfer-Encoding");
127+
String transferEncodingHeader = getMethod.getResponseHeader("Transfer-Encoding");
132128
boolean transferEncoding = false;
133129

134130
if (transferEncodingHeader != null) {
135-
transferEncoding = "chunked".equals(transferEncodingHeader.getValue());
131+
transferEncoding = "chunked".equals(transferEncodingHeader);
136132
}
137133

138134
if (transferred == totalToTransfer || transferEncoding) {
139135
savedFile = true;
140-
Header modificationTime = getMethod.getResponseHeader("Last-Modified");
136+
String modificationTime = getMethod.getResponseHeader("Last-Modified");
141137
if (modificationTime == null) {
142138
modificationTime = getMethod.getResponseHeader("last-modified");
143139
}
144140
if (modificationTime != null) {
145-
Date d = WebdavUtils.parseResponseDate(modificationTime.getValue());
141+
Date d = WebdavUtils.parseResponseDate(modificationTime);
146142
modificationTimestamp = (d != null) ? d.getTime() : 0;
147143
} else {
148144
Log_OC.e(TAG, "Could not read modification time from response downloading " + remotePath);
@@ -153,15 +149,8 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept
153149
Log_OC.e(TAG, "Could not read eTag from response downloading " + remotePath);
154150
}
155151

156-
} else {
157-
client.exhaustResponse(getMethod.getResponseBodyAsStream());
158-
// TODO some kind of error control!
159152
}
160-
161-
} else {
162-
client.exhaustResponse(getMethod.getResponseBodyAsStream());
163153
}
164-
165154
} finally {
166155
if (fos != null) fos.close();
167156
if (!savedFile && targetFile.exists()) {

0 commit comments

Comments
 (0)