OC-2388: Move ChunkedUploadFileOperation to the framework. UploadFileOperation select operation with or without chunks

This commit is contained in:
masensio 2013-12-16 10:46:29 +01:00
parent dc1ea137a2
commit e76d4b00ef
5 changed files with 50 additions and 46 deletions

View file

@ -16,7 +16,7 @@
*
*/
package com.owncloud.android.operations;
package com.owncloud.android.oc_framework.operations.remote;
import java.io.File;
import java.io.IOException;
@ -27,35 +27,25 @@ import java.util.Random;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PutMethod;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.oc_framework.network.ProgressiveDataTransferer;
import com.owncloud.android.oc_framework.network.webdav.ChunkFromFileChannelRequestEntity;
import com.owncloud.android.oc_framework.network.webdav.OnDatatransferProgressListener;
import com.owncloud.android.oc_framework.network.webdav.WebdavClient;
import com.owncloud.android.oc_framework.network.webdav.WebdavUtils;
import com.owncloud.android.utils.Log_OC;
import android.accounts.Account;
import android.content.Context;
import android.util.Log;
public class ChunkedUploadFileOperation extends UploadFileOperation {
public class ChunkedUploadRemoteFileOperation extends UploadRemoteFileOperation {
private static final long CHUNK_SIZE = 1024000;
private static final String OC_CHUNKED_HEADER = "OC-Chunked";
private static final String TAG = ChunkedUploadFileOperation.class.getSimpleName();
public ChunkedUploadFileOperation( Account account,
OCFile file,
boolean isInstant,
boolean forceOverwrite,
int localBehaviour, Context context,
OnDatatransferProgressListener listener) {
super(account, file, isInstant, forceOverwrite, localBehaviour, context, listener);
}
private static final String TAG = ChunkedUploadRemoteFileOperation.class.getSimpleName();
public ChunkedUploadRemoteFileOperation(String storagePath, String remotePath, String mimeType) {
super(storagePath, remotePath, mimeType);
}
@Override
protected int uploadFile(WebdavClient client) throws HttpException, IOException {
int status = -1;
@ -63,13 +53,17 @@ public class ChunkedUploadFileOperation extends UploadFileOperation {
FileChannel channel = null;
RandomAccessFile raf = null;
try {
File file = new File(getStoragePath());
File file = new File(mStoragePath);
raf = new RandomAccessFile(file, "r");
channel = raf.getChannel();
mEntity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE, file);
((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners());
mEntity = new ChunkFromFileChannelRequestEntity(channel, mMimeType, CHUNK_SIZE, file);
//((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners());
synchronized (mDataTransferListeners) {
((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(mDataTransferListeners);
}
long offset = 0;
String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(mRemotePath) + "-chunking-" + Math.abs((new Random()).nextInt(9000)+1000) + "-" ;
long chunkCount = (long) Math.ceil((double)file.length() / CHUNK_SIZE);
for (int chunkIndex = 0; chunkIndex < chunkCount ; chunkIndex++, offset += CHUNK_SIZE) {
if (mPutMethod != null) {
@ -81,7 +75,7 @@ public class ChunkedUploadFileOperation extends UploadFileOperation {
mPutMethod.setRequestEntity(mEntity);
status = client.executeMethod(mPutMethod);
client.exhaustResponse(mPutMethod.getResponseBodyAsStream());
Log_OC.d(TAG, "Upload of " + getStoragePath() + " to " + getRemotePath() + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
Log.d(TAG, "Upload of " + mStoragePath + " to " + mRemotePath + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status);
if (!isSuccess(status))
break;
}

View file

@ -47,12 +47,13 @@ import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
public class UploadRemoteFileOperation extends RemoteOperation {
private String mStoragePath;
private String mRemotePath;
private String mMimeType;
PutMethod mPutMethod = null;
protected String mStoragePath;
protected String mRemotePath;
protected String mMimeType;
protected PutMethod mPutMethod = null;
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
protected Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
protected RequestEntity mEntity = null;

View file

@ -38,7 +38,6 @@ import com.owncloud.android.authentication.AuthenticatorActivity;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.db.DbHandler;
import com.owncloud.android.operations.ChunkedUploadFileOperation;
import com.owncloud.android.operations.CreateFolderOperation;
import com.owncloud.android.oc_framework.operations.RemoteOperation;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
@ -251,7 +250,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
files[i] = obtainNewOCFileToUpload(remotePaths[i], localPaths[i], ((mimeTypes != null) ? mimeTypes[i]
: (String) null), storageManager);
if (files[i] == null) {
// TODO @andomaex add failure Notiification
// TODO @andomaex add failure Notification
return Service.START_NOT_STICKY;
}
}
@ -265,13 +264,15 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
try {
for (int i = 0; i < files.length; i++) {
uploadKey = buildRemoteName(account, files[i].getRemotePath());
if (chunked) {
newUpload = new ChunkedUploadFileOperation(account, files[i], isInstant, forceOverwrite,
localAction, getApplicationContext(), this);
} else {
newUpload = new UploadFileOperation(account, files[i], isInstant, forceOverwrite, localAction,
getApplicationContext(), this);
}
newUpload = new UploadFileOperation(account, files[i], chunked, isInstant, forceOverwrite, localAction,
getApplicationContext());
// if (chunked) {
// newUpload = new ChunkedUploadFileOperation(account, files[i], isInstant, forceOverwrite,
// localAction, getApplicationContext());
// } else {
// newUpload = new UploadFileOperation(account, files[i], isInstant, forceOverwrite, localAction,
// getApplicationContext());
// }
if (isInstant) {
newUpload.setRemoteFolderToBeCreated();
}

View file

@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
@ -42,6 +43,7 @@ import com.owncloud.android.oc_framework.operations.OperationCancelledException;
import com.owncloud.android.oc_framework.operations.RemoteOperation;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.oc_framework.operations.remote.ChunkedUploadRemoteFileOperation;
import com.owncloud.android.oc_framework.operations.remote.ExistenceCheckRemoteOperation;
import com.owncloud.android.oc_framework.operations.remote.UploadRemoteFileOperation;
import com.owncloud.android.utils.FileStorageUtils;
@ -64,6 +66,7 @@ public class UploadFileOperation extends RemoteOperation {
private OCFile mFile;
private OCFile mOldFile;
private String mRemotePath = null;
private boolean mChunked = false;
private boolean mIsInstant = false;
private boolean mRemoteFolderToBeCreated = false;
private boolean mForceOverwrite = false;
@ -72,7 +75,6 @@ public class UploadFileOperation extends RemoteOperation {
private String mOriginalFileName = null;
private String mOriginalStoragePath = null;
PutMethod mPutMethod = null;
private OnDatatransferProgressListener mDataTransferListener;
private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
private Context mContext;
@ -84,11 +86,11 @@ public class UploadFileOperation extends RemoteOperation {
public UploadFileOperation( Account account,
OCFile file,
boolean chunked,
boolean isInstant,
boolean forceOverwrite,
int localBehaviour,
Context context,
OnDatatransferProgressListener listener) {
Context context) {
if (account == null)
throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation creation");
if (file == null)
@ -103,13 +105,13 @@ public class UploadFileOperation extends RemoteOperation {
mAccount = account;
mFile = file;
mRemotePath = file.getRemotePath();
mChunked = chunked;
mIsInstant = isInstant;
mForceOverwrite = forceOverwrite;
mLocalBehaviour = localBehaviour;
mOriginalStoragePath = mFile.getStoragePath();
mOriginalFileName = mFile.getFileName();
mContext = context;
mDataTransferListener = listener;
}
public Account getAccount() {
@ -271,8 +273,17 @@ public class UploadFileOperation extends RemoteOperation {
localCopyPassed = true;
/// perform the upload
mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
mFile.getMimetype());
if (mChunked) {
mUploadOperation = new ChunkedUploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
mFile.getMimetype());
} else {
mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
mFile.getMimetype());
}
Iterator <OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
while (listener.hasNext()) {
mUploadOperation.addDatatransferProgressListener(listener.next());
}
result = mUploadOperation.execute(client);
/// move local temporal file or original file to its corresponding

View file

@ -28,9 +28,6 @@ import java.util.Vector;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.R.id;
import com.owncloud.android.R.layout;
import com.owncloud.android.R.string;
import com.owncloud.android.authentication.AccountAuthenticator;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;