X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/c0c52fa16a79f6be71ffbe7c7cb587af67335793..32dadbcc6ca0de9924f216f81c6a124862ab3136:/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 77de090d..512056ba 100644 --- a/src/com/owncloud/android/operations/UploadFileOperation.java +++ b/src/com/owncloud/android/operations/UploadFileOperation.java @@ -26,12 +26,13 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.channels.FileChannel; import java.util.HashSet; 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; @@ -39,6 +40,7 @@ import android.content.Context; import android.net.Uri; import com.owncloud.android.MainApp; +import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.lib.common.OwnCloudClient; @@ -75,7 +77,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; @@ -312,10 +313,10 @@ 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 listener = mDataTransferListeners.iterator(); while (listener.hasNext()) { @@ -332,7 +333,9 @@ public class UploadFileOperation extends RemoteOperation { if (result.isSuccess()) { if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) { mFile.setStoragePath(null); - + } else if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_REMOVE){ + mFile.setStoragePath(null); + originalFile.delete(); } else { mFile.setStoragePath(expectedPath); File fileToMove = null; @@ -346,20 +349,44 @@ public class UploadFileOperation extends RemoteOperation { 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 (expectedFolder.isDirectory()){ + if (!fileToMove.renameTo(expectedFile)){ + // try to copy and then delete + expectedFile.createNewFile(); + FileChannel inChannel = new FileInputStream(fileToMove).getChannel(); + FileChannel outChannel = new FileOutputStream(expectedFile).getChannel(); + + try { + inChannel.transferTo(0, inChannel.size(), outChannel); + fileToMove.delete(); + } catch (Exception e){ + 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; + } + finally { + if (inChannel != null) inChannel.close(); + if (outChannel != null) outChannel.close(); + } + } + + } else { + mFile.setStoragePath(null); } } } + FileDataStorageManager.triggerMediaScan(originalFile.getAbsolutePath()); + FileDataStorageManager.triggerMediaScan(expectedFile.getAbsolutePath()); + } else if (result.getHttpCode() == HttpStatus.SC_PRECONDITION_FAILED ) { + result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); } } catch (Exception e) { @@ -369,6 +396,9 @@ public class UploadFileOperation extends RemoteOperation { if (temporalFile != null && !originalFile.equals(temporalFile)) { temporalFile.delete(); } + if (result == null){ + return new RemoteOperationResult(false, 404, null); + } if (result.isSuccess()) { Log_OC.i(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage()); @@ -403,7 +433,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());