--- /dev/null
+/* 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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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;
+ }
+
+}
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;
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;
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;
}
}
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();
}
+++ /dev/null
-/* 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 <http://www.gnu.org/licenses/>.
- *
- */
-
-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;
- }
-
-}
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;
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;
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;
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;
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)
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() {
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
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;