diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperationIT.kt index d6a5183090..94840010b8 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperationIT.kt @@ -27,7 +27,7 @@ class DownloadFileRemoteOperationIT : AbstractIT() { assertTrue( DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath) - .execute(client) + .execute(nextcloudClient) .isSuccess ) diff --git a/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt b/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt index aba9c23917..96b39ebf70 100644 --- a/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt +++ b/library/src/main/java/com/nextcloud/common/OkHttpMethodBase.kt @@ -9,6 +9,7 @@ */ package com.nextcloud.common +import com.nextcloud.common.OkHttpMethodBase.Companion.UNKNOWN_STATUS_CODE import com.owncloud.android.lib.common.OwnCloudClientManagerFactory import com.owncloud.android.lib.common.operations.RemoteOperation import com.owncloud.android.lib.common.utils.Log_OC @@ -84,6 +85,8 @@ abstract class OkHttpMethodBase( fun getResponseBodyAsString(): String = response?.body?.string() ?: "" + fun getResponseBodyAsStream() = response?.body?.byteStream() + fun getResponseContentLength(): Long = response?.body?.contentLength() ?: -1 fun releaseConnection() { diff --git a/library/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java b/library/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java index e83afbbcd9..a0ec3abc11 100644 --- a/library/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java +++ b/library/src/main/java/com/owncloud/android/lib/common/network/WebdavUtils.java @@ -15,7 +15,7 @@ import android.net.Uri; -import androidx.annotation.Nullable; +import com.nextcloud.common.OkHttpMethodBase; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpMethod; @@ -28,6 +28,7 @@ import java.util.Date; import java.util.Locale; +import androidx.annotation.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @SuppressFBWarnings("FS") @@ -239,12 +240,6 @@ public static String parseEtag(String rawEtag) { return rawEtag; } - - /** - * - * @param method - * @return - */ public static String getEtagFromResponse(HttpMethod method) { Header eTag = method.getResponseHeader("OC-ETag"); if (eTag == null) { @@ -263,4 +258,21 @@ public static String getEtagFromResponse(HttpMethod method) { return result; } + public static String getEtagFromResponse(OkHttpMethodBase method) { + String eTag = method.getResponseHeader("OC-ETag"); + if (eTag == null) { + eTag = method.getResponseHeader("oc-etag"); + } + if (eTag == null) { + eTag = method.getResponseHeader("ETag"); + } + if (eTag == null) { + eTag = method.getResponseHeader("etag"); + } + String result = ""; + if (eTag != null) { + result = parseEtag(eTag); + } + return result; + } } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java index b5de6ebf99..bc04510390 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/files/DownloadFileRemoteOperation.java @@ -6,7 +6,8 @@ */ package com.owncloud.android.lib.resources.files; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.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; @@ -14,9 +15,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; -import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.GetMethod; import java.io.BufferedInputStream; import java.io.File; @@ -58,7 +57,7 @@ public DownloadFileRemoteOperation(String remotePath, String temporalFolderPath) } @Override - protected RemoteOperationResult run(OwnCloudClient client) { + public RemoteOperationResult run(NextcloudClient client) { RemoteOperationResult result; /// download will be performed to a temporal file, then moved to the final location @@ -82,15 +81,15 @@ protected RemoteOperationResult run(OwnCloudClient client) { } - private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException { + private int downloadFile(NextcloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException { int status; boolean savedFile = false; - getMethod = new GetMethod(client.getFilesDavUri(remotePath)); + getMethod = new GetMethod(client.getFilesDavUri(remotePath), false); Iterator it; FileOutputStream fos = null; try { - status = client.executeMethod(getMethod); + status = client.execute(getMethod); if (isSuccess(status)) { try { targetFile.createNewFile(); @@ -102,17 +101,15 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept fos = new FileOutputStream(targetFile); long transferred = 0; - Header contentLength = getMethod.getResponseHeader("Content-Length"); - long totalToTransfer = (contentLength != null && - contentLength.getValue().length() > 0) ? - Long.parseLong(contentLength.getValue()) : 0; + String contentLength = getMethod.getResponseHeader("Content-Length"); + long totalToTransfer = (contentLength != null) ?Long.parseLong(contentLength) : 0; byte[] bytes = new byte[4096]; int readResult; while ((readResult = bis.read(bytes)) != -1) { synchronized (mCancellationRequested) { if (mCancellationRequested.get()) { - getMethod.abort(); + // getMethod.abort(); throw new OperationCancelledException(); } } @@ -128,21 +125,21 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept } // Check if the file is completed // if transfer-encoding: chunked we cannot check if the file is complete - Header transferEncodingHeader = getMethod.getResponseHeader("Transfer-Encoding"); + String transferEncodingHeader = getMethod.getResponseHeader("Transfer-Encoding"); boolean transferEncoding = false; if (transferEncodingHeader != null) { - transferEncoding = "chunked".equals(transferEncodingHeader.getValue()); + transferEncoding = "chunked".equals(transferEncodingHeader); } if (transferred == totalToTransfer || transferEncoding) { savedFile = true; - Header modificationTime = getMethod.getResponseHeader("Last-Modified"); + String modificationTime = getMethod.getResponseHeader("Last-Modified"); if (modificationTime == null) { modificationTime = getMethod.getResponseHeader("last-modified"); } if (modificationTime != null) { - Date d = WebdavUtils.parseResponseDate(modificationTime.getValue()); + Date d = WebdavUtils.parseResponseDate(modificationTime); modificationTimestamp = (d != null) ? d.getTime() : 0; } else { Log_OC.e(TAG, "Could not read modification time from response downloading " + remotePath); @@ -153,15 +150,8 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept Log_OC.e(TAG, "Could not read eTag from response downloading " + remotePath); } - } else { - client.exhaustResponse(getMethod.getResponseBodyAsStream()); - // TODO some kind of error control! } - - } else { - client.exhaustResponse(getMethod.getResponseBodyAsStream()); } - } finally { if (fos != null) fos.close(); if (!savedFile && targetFile.exists()) {