Skip to content

Commit

Permalink
Merge pull request #7011 from dassio/skip_upload
Browse files Browse the repository at this point in the history
add skip option in case of name collision
  • Loading branch information
AndyScherzinger committed Oct 2, 2020
2 parents a617416 + 6a5f5f4 commit 61aaba5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 7 deletions.
Expand Up @@ -32,6 +32,7 @@ import com.owncloud.android.AbstractOnServerIT
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.datamodel.UploadsStorageManager
import com.owncloud.android.db.OCUpload
import com.owncloud.android.lib.common.operations.OperationCancelledException
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.operations.UploadFileOperation
Expand Down Expand Up @@ -397,4 +398,109 @@ class FileUploaderIT : AbstractOnServerIT() {

assertEquals(file.length(), (result2.data[0] as RemoteFile).length)
}

/**
* uploads a file with "skip if exists" option set, so do nothing if file exists
*/
@Test
fun testCancelServer() {
val file = getDummyFile("/chunkedFile.txt")
val ocUpload = OCUpload(file.absolutePath, "/testFile.txt", account.name)

assertTrue(
UploadFileOperation(
uploadsStorageManager,
connectivityServiceMock,
powerManagementServiceMock,
user,
null,
ocUpload,
FileUploader.NameCollisionPolicy.CANCEL,
FileUploader.LOCAL_BEHAVIOUR_COPY,
targetContext,
false,
false
)
.setRemoteFolderToBeCreated()
.execute(client, storageManager)
.isSuccess
)

val result = ReadFileRemoteOperation("/testFile.txt").execute(client)
assertTrue(result.isSuccess)

assertEquals(file.length(), (result.data[0] as RemoteFile).length)

val ocUpload2 = OCUpload(getDummyFile("/empty.txt").absolutePath, "/testFile.txt", account.name)

val uploadResult = UploadFileOperation(
uploadsStorageManager,
connectivityServiceMock,
powerManagementServiceMock,
user,
null,
ocUpload2,
FileUploader.NameCollisionPolicy.CANCEL,
FileUploader.LOCAL_BEHAVIOUR_COPY,
targetContext,
false,
false
)
.execute(client, storageManager)

assertFalse(uploadResult.isSuccess)
assertTrue(uploadResult.exception is OperationCancelledException)

val result2 = ReadFileRemoteOperation("/testFile.txt").execute(client)
assertTrue(result2.isSuccess)

assertEquals(file.length(), (result2.data[0] as RemoteFile).length)
}

/**
* uploads a file with "skip if exists" option set, so do nothing if file exists
*/
@Test
fun testKeepCancelStatic() {
val file = getDummyFile("/chunkedFile.txt")

FileUploader.uploadNewFile(
targetContext,
account,
file.absolutePath,
"/testFile.txt",
FileUploader.LOCAL_BEHAVIOUR_COPY,
null,
true,
UploadFileOperation.CREATED_BY_USER,
false,
false,
FileUploader.NameCollisionPolicy.DEFAULT
)

longSleep()

val result = ReadFileRemoteOperation("/testFile.txt").execute(client)
assertTrue(result.isSuccess)

assertEquals(file.length(), (result.data[0] as RemoteFile).length)

val ocFile2 = OCFile("/testFile.txt")
ocFile2.setStoragePath(getDummyFile("/empty.txt").absolutePath)

FileUploader.uploadUpdateFile(
targetContext,
account,
ocFile2,
FileUploader.LOCAL_BEHAVIOUR_COPY,
FileUploader.NameCollisionPolicy.CANCEL
)

shortSleep()

val result2 = ReadFileRemoteOperation("/testFile.txt").execute(client)
assertTrue(result2.isSuccess)

assertEquals(file.length(), (result2.data[0] as RemoteFile).length)
}
}
Expand Up @@ -26,7 +26,6 @@
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.style.StyleSpan;
Expand Down Expand Up @@ -113,11 +112,11 @@ public static SyncedFolderPreferencesDialogFragment newInstance(SyncedFolderDisp
}

@Override
public void onAttach(Activity activity) {
public void onAttach(@NonNull Activity activity) {
super.onAttach(activity);
if (!(activity instanceof OnSyncedFolderPreferenceListener)) {
throw new IllegalArgumentException("The host activity must implement "
+ OnSyncedFolderPreferenceListener.class.getCanonicalName());
+ OnSyncedFolderPreferenceListener.class.getCanonicalName());
}
}

Expand Down Expand Up @@ -584,31 +583,35 @@ public void onClick(DialogInterface dialog, int which) {

/**
* Get index for name collision selection dialog.
* @return 0 if ASK_USER, 1 if OVERWRITE, 2 if RENAME. Otherwise: 0
* @return 0 if ASK_USER, 1 if OVERWRITE, 2 if RENAME, 3 if SKIP, Otherwise: 0
*/
static private Integer getSelectionIndexForNameCollisionPolicy(FileUploader.NameCollisionPolicy nameCollisionPolicy) {
switch (nameCollisionPolicy) {
case OVERWRITE:
return 1;
case RENAME:
return 2;
case CANCEL:
return 3;
case ASK_USER:
default:
return 0;
}
}

/**
* Get index for name collision selection dialog.
* Inverse of getSelectionIndexForNameCollisionPolicy.
* @return ASK_USER if 0, OVERWRITE if 1, RENAME if 2. Otherwise: ASK_USEr
* Get index for name collision selection dialog. Inverse of getSelectionIndexForNameCollisionPolicy.
*
* @return ASK_USER if 0, OVERWRITE if 1, RENAME if 2, SKIP if 3. Otherwise: ASK_USER
*/
static private FileUploader.NameCollisionPolicy getNameCollisionPolicyForSelectionIndex(int index) {
switch (index) {
case 1:
return FileUploader.NameCollisionPolicy.OVERWRITE;
case 2:
return FileUploader.NameCollisionPolicy.RENAME;
case 3:
return FileUploader.NameCollisionPolicy.CANCEL;
case 0:
default:
return FileUploader.NameCollisionPolicy.ASK_USER;
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/attrs.xml
Expand Up @@ -16,5 +16,6 @@
<item>@string/pref_instant_name_collision_policy_entries_always_ask</item>
<item>@string/pref_instant_name_collision_policy_entries_overwrite</item>
<item>@string/pref_instant_name_collision_policy_entries_rename</item>
<item>@string/pref_instant_name_collision_policy_entries_cancel</item>
</string-array>
</resources>
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Expand Up @@ -459,6 +459,7 @@
<string name="pref_instant_name_collision_policy_entries_always_ask">Ask me every time</string>
<string name="pref_instant_name_collision_policy_entries_rename">Rename new version</string>
<string name="pref_instant_name_collision_policy_entries_overwrite">Overwrite remote version</string>
<string name="pref_instant_name_collision_policy_entries_cancel">Skip uploading</string>

<string name="share_dialog_title">Sharing</string>
<string name="share_file">Share %1$s</string>
Expand Down

0 comments on commit 61aaba5

Please sign in to comment.