1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

add DownloadRemoteFileOperation

This commit is contained in:
theScrabi 2018-06-15 12:29:59 +02:00 committed by davigonz
parent 82fce2944b
commit 60c1be2c0c
3 changed files with 38 additions and 22 deletions

View File

@ -46,4 +46,5 @@ public class GetMethod extends HttpMethod {
return super.execute(); return super.execute();
} }
} }

View File

@ -28,6 +28,7 @@ import com.owncloud.android.lib.common.http.methods.HttpBaseMethod;
import java.io.IOException; import java.io.IOException;
import okhttp3.Call;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
/** /**
@ -37,13 +38,20 @@ import okhttp3.HttpUrl;
*/ */
public abstract class HttpMethod extends HttpBaseMethod { public abstract class HttpMethod extends HttpBaseMethod {
private Call mCall;
public HttpMethod(HttpUrl httpUrl) { public HttpMethod(HttpUrl httpUrl) {
super(httpUrl); super(httpUrl);
} }
@Override @Override
public int execute() throws IOException { public int execute() throws IOException {
mResponse = mOkHttpClient.newCall(mRequest).execute(); mCall = mOkHttpClient.newCall(mRequest);
mResponse = mCall.execute();
return super.getStatusCode(); return super.getStatusCode();
} }
public void abort() {
mCall.cancel();
}
} }

View File

@ -34,11 +34,10 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClient;
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.OnDatatransferProgressListener;
import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.network.WebdavUtils;
import com.owncloud.android.lib.common.operations.OperationCancelledException; import com.owncloud.android.lib.common.operations.OperationCancelledException;
@ -46,6 +45,8 @@ 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.Log_OC; import com.owncloud.android.lib.common.utils.Log_OC;
import okhttp3.HttpUrl;
/** /**
* Remote operation performing the download of a remote file in the ownCloud server. * Remote operation performing the download of a remote file in the ownCloud server.
* *
@ -103,30 +104,32 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
RemoteOperationResult result; RemoteOperationResult result;
int status; int status;
boolean savedFile = false; boolean savedFile = false;
mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); mGet = new GetMethod(HttpUrl.parse(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)));
Iterator<OnDatatransferProgressListener> it; Iterator<OnDatatransferProgressListener> it;
FileOutputStream fos = null; FileOutputStream fos = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
try { try {
status = client.executeMethod(mGet); status = client.executeHttpMethod(mGet);
if (isSuccess(status)) { if (isSuccess(status)) {
targetFile.createNewFile(); targetFile.createNewFile();
bis = new BufferedInputStream(mGet.getResponseBodyAsStream()); bis = new BufferedInputStream(mGet.getResponseAsStream());
fos = new FileOutputStream(targetFile); fos = new FileOutputStream(targetFile);
long transferred = 0; long transferred = 0;
Header contentLength = mGet.getResponseHeader("Content-Length"); String contentLength = mGet.getResponseHeader("Content-Length");
long totalToTransfer = (contentLength != null && long totalToTransfer =
contentLength.getValue().length() > 0) ? (contentLength != null
Long.parseLong(contentLength.getValue()) : 0; && contentLength.length() > 0)
? Long.parseLong(contentLength)
: 0;
byte[] bytes = new byte[4096]; byte[] bytes = new byte[4096];
int readResult; int readResult;
while ((readResult = bis.read(bytes)) != -1) { while ((readResult = bis.read(bytes)) != -1) {
synchronized (mCancellationRequested) { synchronized (mCancellationRequested) {
if (mCancellationRequested.get()) { if (mCancellationRequested.get()) {
mGet.abort(); mGet.abort();
throw new OperationCancelledException(); throw new OperationCancelledException();
} }
} }
@ -142,41 +145,45 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
} }
if (transferred == totalToTransfer) { // Check if the file is completed if (transferred == totalToTransfer) { // Check if the file is completed
savedFile = true; savedFile = true;
Header modificationTime = mGet.getResponseHeader("Last-Modified"); final String modificationTime =
if (modificationTime == null) { mGet.getResponseHeaders().get("Last-Modified") != null
modificationTime = mGet.getResponseHeader("last-modified"); ? mGet.getResponseHeaders().get("Last-Modified")
} : mGet.getResponseHeader("last-modified");
if (modificationTime != null) { if (modificationTime != null) {
Date d = WebdavUtils.parseResponseDate(modificationTime.getValue()); final Date d = WebdavUtils.parseResponseDate(modificationTime);
mModificationTimestamp = (d != null) ? d.getTime() : 0; mModificationTimestamp = (d != null) ? d.getTime() : 0;
} else { } else {
Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath); Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath);
} }
// TODO
// mEtag = WebdavUtils.getEtagFromResponse(mGet); // TODO mEtag = WebdavUtils.getEtagFromResponse(mGet);
if (mEtag.length() == 0) { if (mEtag.length() == 0) {
Log_OC.e(TAG, "Could not read eTag from response downloading " + mRemotePath); Log_OC.e(TAG, "Could not read eTag from response downloading " + mRemotePath);
} }
} else { } else {
client.exhaustResponse(mGet.getResponseBodyAsStream()); client.exhaustResponse(mGet.getResponseAsStream());
// TODO some kind of error control! // TODO some kind of error control!
} }
} else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) { } else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
client.exhaustResponse(mGet.getResponseBodyAsStream()); client.exhaustResponse(mGet.getResponseAsStream());
} // else, body read by RemoteOperationResult constructor } // else, body read by RemoteOperationResult constructor
result = new RemoteOperationResult(isSuccess(status), mGet); result = isSuccess(status)
? new RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
: new RemoteOperationResult(mGet);
} catch (Exception e) {
return new RemoteOperationResult(e);
} finally { } finally {
if (fos != null) fos.close(); if (fos != null) fos.close();
if (bis != null) bis.close(); if (bis != null) bis.close();
if (!savedFile && targetFile.exists()) { if (!savedFile && targetFile.exists()) {
targetFile.delete(); targetFile.delete();
} }
mGet.releaseConnection(); // let the connection available for other methods
} }
return result; return result;
} }