Merge branch 'master' of github.com:owncloud/android into setAsWallpaper
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UploadFileOperation.java
index 12cf1ac..ee9f7c8 100644 (file)
@@ -31,7 +31,7 @@ import java.util.Iterator;
 import java.util.Set;
 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;
@@ -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<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
     private AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
     private Context mContext;
@@ -312,61 +311,61 @@ public class UploadFileOperation extends RemoteOperation {
                     (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 <OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
             while (listener.hasNext()) {
                 mUploadOperation.addDatatransferProgressListener(listener.next());
             }
-            if (!mCancellationRequested.get()) {
-                result = mUploadOperation.execute(client);
-
-                /// move local temporal file or original file to its corresponding
-                // location in the ownCloud local folder
-                if (result.isSuccess()) {
-                    if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
-                        mFile.setStoragePath(null);
-
-                    } else {
-                        mFile.setStoragePath(expectedPath);
-                        File fileToMove = null;
-                        if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY
-                            // ; see where temporalFile was
-                            // set
-                            fileToMove = temporalFile;
-                        } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
-                            fileToMove = originalFile;
-                        }
-                        if (!expectedFile.equals(fileToMove)) {
-                            File expectedFolder = expectedFile.getParentFile();
-                            expectedFolder.mkdirs();
-                            if (!expectedFolder.isDirectory() || !fileToMove.renameTo(expectedFile)) {
-                                mFile.setStoragePath(null); // forget the local file
-                                // by now, treat this as a success; the file was
-                                // uploaded; the user won't like that the local file
-                                // is not linked, but this should be a very rare
-                                // fail;
-                                // the best option could be show a warning message
-                                // (but not a fail)
-                                // result = new
-                                // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
-                                // return result;
-                            }
+            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()) {
+                if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
+                    mFile.setStoragePath(null);
+
+                } else {
+                    mFile.setStoragePath(expectedPath);
+                    File fileToMove = null;
+                    if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY
+                        // ; see where temporalFile was
+                        // set
+                        fileToMove = temporalFile;
+                    } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
+                        fileToMove = originalFile;
+                    }
+                    if (!expectedFile.equals(fileToMove)) {
+                        File expectedFolder = expectedFile.getParentFile();
+                        expectedFolder.mkdirs();
+                        if (!expectedFolder.isDirectory() || !fileToMove.renameTo(expectedFile)) {
+                            mFile.setStoragePath(null); // forget the local file
+                            // by now, treat this as a success; the file was
+                            // uploaded; the user won't like that the local file
+                            // is not linked, but this should be a very rare
+                            // fail;
+                            // the best option could be show a warning message
+                            // (but not a fail)
+                            // result = new
+                            // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
+                            // return result;
                         }
                     }
                 }
+
+            } 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()) {
-                result = new RemoteOperationResult(new OperationCancelledException());
-            } else {
-                result = new RemoteOperationResult(e);
-            }
+            result = new RemoteOperationResult(e);
 
         } finally {
             if (temporalFile != null && !originalFile.equals(temporalFile)) {
@@ -406,7 +405,7 @@ public class UploadFileOperation extends RemoteOperation {
         newFile.setModificationTimestamp(mFile.getModificationTimestamp());
         newFile.setModificationTimestampAtLastSyncForData(
                 mFile.getModificationTimestampAtLastSyncForData());
-        // newFile.setEtag(mFile.getEtag())
+        newFile.setEtag(mFile.getEtag());
         newFile.setFavorite(mFile.isFavorite());
         newFile.setLastSyncDateForProperties(mFile.getLastSyncDateForProperties());
         newFile.setLastSyncDateForData(mFile.getLastSyncDateForData());