Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DownloadFileRemoteOperationIT : AbstractIT() {

assertTrue(
DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath)
.execute(client)
.execute(nextcloudClient)
.isSuccess
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
*/
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;
import com.owncloud.android.lib.common.operations.RemoteOperation;
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;
Expand Down Expand Up @@ -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
Expand All @@ -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<OnDatatransferProgressListener> it;

FileOutputStream fos = null;
try {
status = client.executeMethod(getMethod);
status = client.execute(getMethod);
if (isSuccess(status)) {
try {
targetFile.createNewFile();
Expand All @@ -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();
}
}
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down
Loading