X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/cb175f29949dbac17a3870a71ecff60df5c9bdc4..c54d7e35d0efb3d1352ff6d1330cac185bef0a21:/src/com/owncloud/android/operations/UploadFileOperation.java diff --git a/src/com/owncloud/android/operations/UploadFileOperation.java b/src/com/owncloud/android/operations/UploadFileOperation.java index 42ca738f..ee9f7c88 100644 --- a/src/com/owncloud/android/operations/UploadFileOperation.java +++ b/src/com/owncloud/android/operations/UploadFileOperation.java @@ -1,5 +1,8 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2015 ownCloud Inc. +/** + * ownCloud Android client application + * + * @author David A. Velasco + * Copyright (C) 2015 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, @@ -26,10 +29,9 @@ import java.io.OutputStream; import java.util.HashSet; import java.util.Iterator; import java.util.Set; -import java.util.concurrent.CancellationException; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.RequestEntity; import android.accounts.Account; @@ -56,8 +58,6 @@ import com.owncloud.android.utils.UriUtils; /** * Remote operation performing the upload of a file to an ownCloud server - * - * @author David A. Velasco */ public class UploadFileOperation extends RemoteOperation { @@ -75,7 +75,6 @@ public class UploadFileOperation extends RemoteOperation { private boolean mWasRenamed = false; private String mOriginalFileName = null; private String mOriginalStoragePath = null; - PutMethod mPutMethod = null; private Set mDataTransferListeners = new HashSet(); private AtomicBoolean mCancellationRequested = new AtomicBoolean(false); private Context mContext; @@ -93,7 +92,8 @@ public class UploadFileOperation extends RemoteOperation { int localBehaviour, Context context) { if (account == null) - throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation creation"); + throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation " + + "creation"); if (file == null) throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation"); if (file.getStoragePath() == null || file.getStoragePath().length() <= 0) { @@ -258,7 +258,6 @@ public class UploadFileOperation extends RemoteOperation { (nRead = in.read(data, 0, data.length)) != -1) { out.write(data, 0, nRead); } - out.flush(); } else { @@ -278,6 +277,11 @@ public class UploadFileOperation extends RemoteOperation { } } + if (mCancellationRequested.get()) { + result = new RemoteOperationResult(new OperationCancelledException()); + } + + } catch (Exception e) { result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED); return result; @@ -300,25 +304,28 @@ public class UploadFileOperation extends RemoteOperation { } } } - localCopyPassed = true; + localCopyPassed = (result == null); /// perform the upload if ( mChunked && (new File(mFile.getStoragePath())).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) { mUploadOperation = new ChunkedUploadRemoteFileOperation(mFile.getStoragePath(), - mFile.getRemotePath(), mFile.getMimetype()); + mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict()); } else { mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), - mFile.getRemotePath(), mFile.getMimetype()); + mFile.getRemotePath(), mFile.getMimetype(), mFile.getEtagInConflict()); } Iterator listener = mDataTransferListeners.iterator(); while (listener.hasNext()) { mUploadOperation.addDatatransferProgressListener(listener.next()); } - if (!mCancellationRequested.get()) { - result = mUploadOperation.execute(client); + if (mCancellationRequested.get()) { + throw new OperationCancelledException(); } + + result = mUploadOperation.execute(client); + /// move local temporal file or original file to its corresponding // location in the ownCloud local folder if (result.isSuccess()) { @@ -329,8 +336,8 @@ public class UploadFileOperation extends RemoteOperation { mFile.setStoragePath(expectedPath); File fileToMove = null; if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY - // ; see where temporalFile was - // set + // ; see where temporalFile was + // set fileToMove = temporalFile; } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE fileToMove = originalFile; @@ -352,16 +359,13 @@ public class UploadFileOperation extends RemoteOperation { } } } + + } else if (result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED ) { + result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); } } catch (Exception e) { - // TODO something cleaner with cancellations - if (mCancellationRequested.get()) { - mUploadOperation.cancel(); - result = new RemoteOperationResult(new OperationCancelledException()); - } else { - result = new RemoteOperationResult(e); - } + result = new RemoteOperationResult(e); } finally { if (temporalFile != null && !originalFile.equals(temporalFile)) { @@ -401,8 +405,8 @@ public class UploadFileOperation extends RemoteOperation { newFile.setModificationTimestamp(mFile.getModificationTimestamp()); newFile.setModificationTimestampAtLastSyncForData( mFile.getModificationTimestampAtLastSyncForData()); - // newFile.setEtag(mFile.getEtag()) - newFile.setKeepInSync(mFile.keepInSync()); + newFile.setEtag(mFile.getEtag()); + newFile.setFavorite(mFile.isFavorite()); newFile.setLastSyncDateForProperties(mFile.getLastSyncDateForProperties()); newFile.setLastSyncDateForData(mFile.getLastSyncDateForData()); newFile.setStoragePath(mFile.getStoragePath());