From f1c6726d301a43a52f603c9de8012aec5013ba9a Mon Sep 17 00:00:00 2001 From: davigonz Date: Wed, 4 Jul 2018 10:55:59 +0200 Subject: [PATCH] Delete remote chunks folder when canceling upload --- .../files/CreateRemoteFolderOperation.java | 6 +-- .../files/MoveRemoteChunksFileOperation.java | 19 ------- .../files/MoveRemoteFileOperation.java | 8 ++- .../files/RemoveRemoteFileOperation.java | 8 ++- .../ChunkedUploadRemoteFileOperation.java | 4 +- .../CreateRemoteChunkFolderOperation.java | 6 ++- .../chunks/MoveRemoteChunksFileOperation.java | 50 +++++++++++++++++++ .../RemoveRemoteChunksFolderOperation.java | 16 ++++++ 8 files changed, 86 insertions(+), 31 deletions(-) delete mode 100644 src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java rename src/com/owncloud/android/lib/resources/files/{ => chunks}/ChunkedUploadRemoteFileOperation.java (96%) rename src/com/owncloud/android/lib/resources/files/{ => chunks}/CreateRemoteChunkFolderOperation.java (91%) create mode 100644 src/com/owncloud/android/lib/resources/files/chunks/MoveRemoteChunksFileOperation.java create mode 100644 src/com/owncloud/android/lib/resources/files/chunks/RemoveRemoteChunksFolderOperation.java diff --git a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java index ae4a0034..b8727aab 100644 --- a/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/CreateRemoteFolderOperation.java @@ -57,7 +57,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { private String mRemotePath; private boolean mCreateFullPath; - protected boolean mFolderToSaveChunks; + protected boolean createChunksFolder; /** * Constructor @@ -67,7 +67,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { public CreateRemoteFolderOperation(String remotePath, boolean createFullPath) { mRemotePath = remotePath; mCreateFullPath = createFullPath; - mFolderToSaveChunks = false; + createChunksFolder = false; } /** @@ -103,7 +103,7 @@ public class CreateRemoteFolderOperation extends RemoteOperation { private RemoteOperationResult createFolder(OwnCloudClient client) { RemoteOperationResult result; try { - Uri webDavUri = mFolderToSaveChunks ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); + Uri webDavUri = createChunksFolder ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); final MkColMethod mkcol = new MkColMethod(HttpUrl.parse(webDavUri + WebdavUtils.encodePath(mRemotePath))); mkcol.setReadTimeout(READ_TIMEOUT, TimeUnit.SECONDS); mkcol.setConnectionTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS); diff --git a/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java deleted file mode 100644 index 7c8859de..00000000 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteChunksFileOperation.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.owncloud.android.lib.resources.files; - -public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation { - - /** - * Constructor. - * - * @param srcRemotePath Remote path of the file/folder to move. - * @param targetRemotePath Remove path desired for the file/folder after moving it. - * @param overwrite - */ - public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, - String fileLastModifTimestamp, long fileLength) { - super(srcRemotePath, targetRemotePath, overwrite); - isChunkedFile = true; - mFileLastModifTimestamp = fileLastModifTimestamp; - mFileLength = fileLength; - } -} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java index 2ec989ca..7b291b66 100644 --- a/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/MoveRemoteFileOperation.java @@ -60,7 +60,7 @@ public class MoveRemoteFileOperation extends RemoteOperation { private String mTargetRemotePath; private boolean mOverwrite; - protected boolean isChunkedFile; + protected boolean moveChunkedFile = false; protected String mFileLastModifTimestamp; protected long mFileLength; @@ -79,7 +79,6 @@ public class MoveRemoteFileOperation extends RemoteOperation { mSrcRemotePath = srcRemotePath; mTargetRemotePath = targetRemotePath; mOverwrite = overwrite; - isChunkedFile = false; } /** @@ -111,17 +110,16 @@ public class MoveRemoteFileOperation extends RemoteOperation { /// perform remote operation RemoteOperationResult result; try { - // After finishing a chunked upload, we have to move the resulting file from uploads folder to files one, // so this uri has to be customizable - Uri srcWebDavUri = isChunkedFile ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); + Uri srcWebDavUri = moveChunkedFile ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); final MoveMethod move = new MoveMethod( HttpUrl.parse(srcWebDavUri + WebdavUtils.encodePath(mSrcRemotePath)), client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mTargetRemotePath), mOverwrite); - if (isChunkedFile) { + if (moveChunkedFile) { move.addRequestHeader(HttpConstants.OC_X_OC_MTIME_HEADER, mFileLastModifTimestamp); move.addRequestHeader(HttpConstants.OC_TOTAL_LENGTH_HEADER, String.valueOf(mFileLength)); } diff --git a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java index 24a47688..7fed429c 100644 --- a/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/RemoveRemoteFileOperation.java @@ -24,6 +24,8 @@ package com.owncloud.android.lib.resources.files; +import android.net.Uri; + import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpConstants; import com.owncloud.android.lib.common.http.HttpUtils; @@ -46,6 +48,8 @@ public class RemoveRemoteFileOperation extends RemoteOperation { private static final String TAG = RemoveRemoteFileOperation.class.getSimpleName(); private String mRemotePath; + protected boolean removeChunksFolder = false; + /** * Constructor * @@ -65,8 +69,10 @@ public class RemoveRemoteFileOperation extends RemoteOperation { RemoteOperationResult result; try { + Uri srcWebDavUri = removeChunksFolder ? client.getNewUploadsWebDavUri() : client.getNewFilesWebDavUri(); + DeleteMethod deleteMethod = new DeleteMethod( - HttpUtils.stringUrlToHttpUrl(client.getNewFilesWebDavUri() + WebdavUtils.encodePath(mRemotePath)) + HttpUtils.stringUrlToHttpUrl(srcWebDavUri + WebdavUtils.encodePath(mRemotePath)) ); int status = client.executeHttpMethod(deleteMethod); diff --git a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java b/src/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java similarity index 96% rename from src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java rename to src/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java index fb6be04e..905944ad 100644 --- a/src/com/owncloud/android/lib/resources/files/ChunkedUploadRemoteFileOperation.java +++ b/src/com/owncloud/android/lib/resources/files/chunks/ChunkedUploadRemoteFileOperation.java @@ -22,7 +22,7 @@ * */ -package com.owncloud.android.lib.resources.files; +package com.owncloud.android.lib.resources.files.chunks; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.http.HttpUtils; @@ -31,6 +31,8 @@ import com.owncloud.android.lib.common.network.ChunkFromFileRequestBody; import com.owncloud.android.lib.common.operations.OperationCancelledException; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.FileUtils; +import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; import java.io.File; import java.io.RandomAccessFile; diff --git a/src/com/owncloud/android/lib/resources/files/CreateRemoteChunkFolderOperation.java b/src/com/owncloud/android/lib/resources/files/chunks/CreateRemoteChunkFolderOperation.java similarity index 91% rename from src/com/owncloud/android/lib/resources/files/CreateRemoteChunkFolderOperation.java rename to src/com/owncloud/android/lib/resources/files/chunks/CreateRemoteChunkFolderOperation.java index 8c5ecc17..2fc9d4bf 100644 --- a/src/com/owncloud/android/lib/resources/files/CreateRemoteChunkFolderOperation.java +++ b/src/com/owncloud/android/lib/resources/files/chunks/CreateRemoteChunkFolderOperation.java @@ -22,7 +22,9 @@ * */ -package com.owncloud.android.lib.resources.files; +package com.owncloud.android.lib.resources.files.chunks; + +import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation; /** * Remote operation performing the creation of a new folder to save chunks during an upload to the ownCloud server. @@ -38,6 +40,6 @@ public class CreateRemoteChunkFolderOperation extends CreateRemoteFolderOperatio */ public CreateRemoteChunkFolderOperation(String remotePath, boolean createFullPath) { super(remotePath, createFullPath); - mFolderToSaveChunks = true; + createChunksFolder = true; } } \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/files/chunks/MoveRemoteChunksFileOperation.java b/src/com/owncloud/android/lib/resources/files/chunks/MoveRemoteChunksFileOperation.java new file mode 100644 index 00000000..5c84609a --- /dev/null +++ b/src/com/owncloud/android/lib/resources/files/chunks/MoveRemoteChunksFileOperation.java @@ -0,0 +1,50 @@ +/* ownCloud Android Library is available under MIT license + * Copyright (C) 2018 ownCloud GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +package com.owncloud.android.lib.resources.files.chunks; + +import com.owncloud.android.lib.resources.files.MoveRemoteFileOperation; + +/** + * Remote operation to move the file built from chunks after uploading it + * + * @author David González Verdugo + */ +public class MoveRemoteChunksFileOperation extends MoveRemoteFileOperation { + + /** + * Constructor. + * + * @param srcRemotePath Remote path of the file/folder to move. + * @param targetRemotePath Remove path desired for the file/folder after moving it. + * @param overwrite + */ + public MoveRemoteChunksFileOperation(String srcRemotePath, String targetRemotePath, boolean overwrite, + String fileLastModifTimestamp, long fileLength) { + super(srcRemotePath, targetRemotePath, overwrite); + moveChunkedFile = true; + mFileLastModifTimestamp = fileLastModifTimestamp; + mFileLength = fileLength; + } +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/resources/files/chunks/RemoveRemoteChunksFolderOperation.java b/src/com/owncloud/android/lib/resources/files/chunks/RemoveRemoteChunksFolderOperation.java new file mode 100644 index 00000000..831cc79c --- /dev/null +++ b/src/com/owncloud/android/lib/resources/files/chunks/RemoveRemoteChunksFolderOperation.java @@ -0,0 +1,16 @@ +package com.owncloud.android.lib.resources.files.chunks; + +import com.owncloud.android.lib.resources.files.RemoveRemoteFileOperation; + +public class RemoveRemoteChunksFolderOperation extends RemoveRemoteFileOperation { + + /** + * Constructor + * + * @param remotePath RemotePath of the remote file or folder to remove from the server + */ + public RemoveRemoteChunksFolderOperation(String remotePath) { + super(remotePath); + removeChunksFolder = true; + } +} \ No newline at end of file