Skip to content

Commit

Permalink
Merge pull request #12452 from nextcloud/backport_sync_conflict_bugfix
Browse files Browse the repository at this point in the history
[stable-3.28] Remove user notification for autoupload edge cases
  • Loading branch information
AndyScherzinger authored Jan 29, 2024
2 parents 9fb5703 + ccaf8b1 commit 19ead91
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class FilesUploadWorker(
/**
* adapted from [com.owncloud.android.files.services.FileUploader.notifyUploadResult]
*/
@Suppress("ReturnCount")
private fun notifyUploadResult(
uploadFileOperation: UploadFileOperation,
uploadResult: RemoteOperationResult<Any?>
Expand All @@ -324,6 +325,14 @@ class FilesUploadWorker(
return
}

// Only notify if it is not same file on remote that causes conflict
if (uploadResult.code == ResultCode.SYNC_CONFLICT && FilesUploadHelper().isSameFileOnRemote(
uploadFileOperation.user, File(uploadFileOperation.storagePath), uploadFileOperation.remotePath, context
)
) {
return
}

val notDelayed = uploadResult.code != ResultCode.DELAYED_FOR_WIFI &&
uploadResult.code != ResultCode.DELAYED_FOR_CHARGING &&
uploadResult.code != ResultCode.DELAYED_IN_POWER_SAVE_MODE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.operations.UploadFileOperation;
import com.owncloud.android.utils.FilesUploadHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -737,6 +739,25 @@ public void updateDatabaseUploadResult(RemoteOperationResult uploadResult, Uploa
upload.getRemotePath(),
localPath
);
} else if (uploadResult.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT &&
new FilesUploadHelper().isSameFileOnRemote(
upload.getUser(), new File(upload.getStoragePath()), upload.getRemotePath(), upload.getContext())) {

updateUploadStatus(
upload.getOCUploadId(),
UploadStatus.UPLOAD_SUCCEEDED,
UploadResult.SAME_FILE_CONFLICT,
upload.getRemotePath(),
localPath
);
} else if (uploadResult.getCode() == RemoteOperationResult.ResultCode.LOCAL_FILE_NOT_FOUND) {
updateUploadStatus(
upload.getOCUploadId(),
UploadStatus.UPLOAD_SUCCEEDED,
UploadResult.FILE_NOT_FOUND,
upload.getRemotePath(),
localPath
);
} else {
updateUploadStatus(
upload.getOCUploadId(),
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/owncloud/android/db/UploadResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public enum UploadResult {
SYNC_CONFLICT(19),
CANNOT_CREATE_FILE(20),
LOCAL_STORAGE_NOT_COPIED(21),
QUOTA_EXCEEDED(22);
QUOTA_EXCEEDED(22),
SAME_FILE_CONFLICT(23);

private final int value;

Expand Down Expand Up @@ -107,6 +108,8 @@ public static UploadResult fromValue(int value) {
return LOCAL_STORAGE_NOT_COPIED;
case 22:
return QUOTA_EXCEEDED;
case 23:
return SAME_FILE_CONFLICT;
}
return UNKNOWN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.format.DateUtils;
Expand Down Expand Up @@ -261,6 +262,7 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
itemViewHolder.binding.uploadRemotePath.setVisibility(View.VISIBLE);
itemViewHolder.binding.uploadFileSize.setVisibility(View.VISIBLE);
itemViewHolder.binding.uploadStatus.setVisibility(View.VISIBLE);
itemViewHolder.binding.uploadStatus.setTypeface(null, Typeface.NORMAL);
itemViewHolder.binding.uploadProgressBar.setVisibility(View.GONE);

// Update information depending of upload details
Expand Down Expand Up @@ -304,6 +306,16 @@ public void onBindViewHolder(SectionedViewHolder holder, int section, int relati
case UPLOAD_FAILED -> itemViewHolder.binding.uploadDate.setVisibility(View.GONE);
case UPLOAD_SUCCEEDED -> itemViewHolder.binding.uploadStatus.setVisibility(View.GONE);
}

// show status if same file conflict or local file deleted
if (item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED &&
item.getLastResult() != UploadResult.UPLOADED){
itemViewHolder.binding.uploadStatus.setVisibility(View.VISIBLE);
itemViewHolder.binding.uploadStatus.setTypeface(null, Typeface.BOLD);
itemViewHolder.binding.uploadDate.setVisibility(View.GONE);
itemViewHolder.binding.uploadFileSize.setVisibility(View.GONE);
}

itemViewHolder.binding.uploadStatus.setText(status);

// bind listeners to perform actions
Expand Down Expand Up @@ -622,7 +634,13 @@ private String getStatusText(OCUpload upload) {
break;

case UPLOAD_SUCCEEDED:
status = parentActivity.getString(R.string.uploads_view_upload_status_succeeded);
if (upload.getLastResult() == UploadResult.SAME_FILE_CONFLICT){
status = parentActivity.getString(R.string.uploads_view_upload_status_succeeded_same_file);
}else if (upload.getLastResult() == UploadResult.FILE_NOT_FOUND) {
status = getUploadFailedStatusText(upload.getLastResult());
} else {
status = parentActivity.getString(R.string.uploads_view_upload_status_succeeded);
}
break;

case UPLOAD_FAILED:
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/owncloud/android/utils/FilesUploadHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.owncloud.android.utils

import android.content.Context
import com.nextcloud.client.account.User
import com.nextcloud.client.jobs.BackgroundJobManager
import com.owncloud.android.MainApp
Expand All @@ -31,7 +32,11 @@ import com.owncloud.android.db.OCUpload
import com.owncloud.android.files.services.FileUploader.FileUploaderBinder
import com.owncloud.android.files.services.NameCollisionPolicy
import com.owncloud.android.lib.common.network.OnDatatransferProgressListener
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import java.io.File
import javax.inject.Inject

class FilesUploadHelper {
Expand Down Expand Up @@ -133,6 +138,25 @@ class FilesUploadHelper {
}
}

@Suppress("MagicNumber")
fun isSameFileOnRemote(user: User, localFile: File, remotePath: String, context: Context): Boolean {
// Compare remote file to local file
val localLastModifiedTimestamp = localFile.lastModified() / 1000 // remote file timestamp in milli not micro sec
val localCreationTimestamp = FileUtil.getCreationTimestamp(localFile)
val localSize: Long = localFile.length()

val operation = ReadFileRemoteOperation(remotePath)
val result: RemoteOperationResult<*> = operation.execute(user, context)
if (result.isSuccess) {
val remoteFile = result.data[0] as RemoteFile
return remoteFile.size == localSize &&
localCreationTimestamp != null &&
localCreationTimestamp == remoteFile.creationTimestamp &&
remoteFile.modifiedTimestamp == localLastModifiedTimestamp * 1000
}
return false
}

companion object Progress {
val mBoundListeners = HashMap<String, OnDatatransferProgressListener>()

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<string name="uploads_view_group_failed_uploads">Failed/pending restart</string>
<string name="uploads_view_group_finished_uploads">Uploaded</string>
<string name="uploads_view_upload_status_succeeded">Completed</string>
<string name="uploads_view_upload_status_succeeded_same_file">Same file found on remote, skipping upload</string>
<string name="uploads_view_upload_status_cancelled">Cancelled</string>
<string name="uploads_view_upload_status_failed_connection_error">Connection error</string>
<string name="uploads_view_upload_status_failed_credentials_error">Credentials error</string>
Expand Down

0 comments on commit 19ead91

Please sign in to comment.