From: masensio Date: Mon, 16 Dec 2013 09:46:29 +0000 (+0100) Subject: OC-2388: Move ChunkedUploadFileOperation to the framework. UploadFileOperation select... X-Git-Tag: oc-android-1.5.5~101^2~3 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/e76d4b00ef2c73bbbf17e7f30a0ee93156ce7d1c?ds=inline;hp=-c OC-2388: Move ChunkedUploadFileOperation to the framework. UploadFileOperation select operation with or without chunks --- e76d4b00ef2c73bbbf17e7f30a0ee93156ce7d1c diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ChunkedUploadRemoteFileOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ChunkedUploadRemoteFileOperation.java new file mode 100644 index 00000000..bacbcc3e --- /dev/null +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/ChunkedUploadRemoteFileOperation.java @@ -0,0 +1,94 @@ +/* ownCloud Android client application + * Copyright (C) 2012 Bartek Przybylski + * Copyright (C) 2012-2013 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.oc_framework.operations.remote; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; +import java.util.Random; + +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.methods.PutMethod; + +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.WebdavClient; +import com.owncloud.android.oc_framework.network.webdav.WebdavUtils; + + +import android.util.Log; + + +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 = 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; + + FileChannel channel = null; + RandomAccessFile raf = null; + try { + File file = new File(mStoragePath); + raf = new RandomAccessFile(file, "r"); + channel = raf.getChannel(); + 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(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) { + mPutMethod.releaseConnection(); // let the connection available for other methods + } + mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex); + mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); + ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset); + mPutMethod.setRequestEntity(mEntity); + status = client.executeMethod(mPutMethod); + client.exhaustResponse(mPutMethod.getResponseBodyAsStream()); + Log.d(TAG, "Upload of " + mStoragePath + " to " + mRemotePath + ", chunk index " + chunkIndex + ", count " + chunkCount + ", HTTP result status " + status); + if (!isSuccess(status)) + break; + } + + } finally { + if (channel != null) + channel.close(); + if (raf != null) + raf.close(); + if (mPutMethod != null) + mPutMethod.releaseConnection(); // let the connection available for other methods + } + return status; + } + +} diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/UploadRemoteFileOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/UploadRemoteFileOperation.java index b3bb5fd6..91e21b49 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/UploadRemoteFileOperation.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/UploadRemoteFileOperation.java @@ -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 mDataTransferListeners = new HashSet(); + protected Set mDataTransferListeners = new HashSet(); protected RequestEntity mEntity = null; diff --git a/src/com/owncloud/android/files/services/FileUploader.java b/src/com/owncloud/android/files/services/FileUploader.java index 744a3dad..5f1bd3de 100644 --- a/src/com/owncloud/android/files/services/FileUploader.java +++ b/src/com/owncloud/android/files/services/FileUploader.java @@ -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(); } diff --git a/src/com/owncloud/android/operations/ChunkedUploadFileOperation.java b/src/com/owncloud/android/operations/ChunkedUploadFileOperation.java deleted file mode 100644 index 45edf005..00000000 --- a/src/com/owncloud/android/operations/ChunkedUploadFileOperation.java +++ /dev/null @@ -1,100 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2012 Bartek Przybylski - * Copyright (C) 2012-2013 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package com.owncloud.android.operations; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.FileChannel; -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; - - -public class ChunkedUploadFileOperation extends UploadFileOperation { - - 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); - } - - @Override - protected int uploadFile(WebdavClient client) throws HttpException, IOException { - int status = -1; - - FileChannel channel = null; - RandomAccessFile raf = null; - try { - File file = new File(getStoragePath()); - raf = new RandomAccessFile(file, "r"); - channel = raf.getChannel(); - mEntity = new ChunkFromFileChannelRequestEntity(channel, getMimeType(), CHUNK_SIZE, file); - ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListeners(getDataTransferListeners()); - long offset = 0; - String uriPrefix = client.getBaseUri() + WebdavUtils.encodePath(getRemotePath()) + "-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) { - mPutMethod.releaseConnection(); // let the connection available for other methods - } - mPutMethod = new PutMethod(uriPrefix + chunkCount + "-" + chunkIndex); - mPutMethod.addRequestHeader(OC_CHUNKED_HEADER, OC_CHUNKED_HEADER); - ((ChunkFromFileChannelRequestEntity)mEntity).setOffset(offset); - 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); - if (!isSuccess(status)) - break; - } - - } finally { - if (channel != null) - channel.close(); - if (raf != null) - raf.close(); - if (mPutMethod != null) - mPutMethod.releaseConnection(); // let the connection available for other methods - } - return status; - } - -} diff --git a/src/com/owncloud/android/operations/UploadFileOperation.java b/src/com/owncloud/android/operations/UploadFileOperation.java index 26b05159..7501fa12 100644 --- a/src/com/owncloud/android/operations/UploadFileOperation.java +++ b/src/com/owncloud/android/operations/UploadFileOperation.java @@ -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 mDataTransferListeners = new HashSet(); 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 listener = mDataTransferListeners.iterator(); + while (listener.hasNext()) { + mUploadOperation.addDatatransferProgressListener(listener.next()); + } result = mUploadOperation.execute(client); /// move local temporal file or original file to its corresponding diff --git a/src/com/owncloud/android/ui/activity/Uploader.java b/src/com/owncloud/android/ui/activity/Uploader.java index 08e2d215..85c9e40d 100644 --- a/src/com/owncloud/android/ui/activity/Uploader.java +++ b/src/com/owncloud/android/ui/activity/Uploader.java @@ -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;