From fd55d73fa2424d001a8f7a8a553ab1b0a864f030 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 1 Sep 2022 15:54:29 +0200 Subject: [PATCH 1/7] Extract code about fileDiff beetween RemoteFile & Database or local file into a dedicated class. Clean ObserverService.handleRemoteFile part about fileDiff checking --- .../e/drive/services/ObserverService.java | 35 +++----- .../e/drive/utils/FileDiffUtils.java | 81 +++++++++++++++++++ 2 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index e57f135e..f2bc993e 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -47,11 +47,13 @@ import foundation.e.drive.receivers.DebugCmdReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; +import foundation.e.drive.utils.FileDiffUtils; import foundation.e.drive.utils.ServiceExceptionHandler; import foundation.e.drive.utils.SynchronizationServiceConnection; import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE; +import static foundation.e.drive.utils.FileDiffUtils.getActionForFileDiff; import androidx.annotation.Nullable; @@ -380,41 +382,30 @@ public class ObserverService extends Service implements OnRemoteOperationListene continue; } - Log.v(TAG, "syncedFileState.getRemotePath() :"+syncedFileState.getRemotePath() ); - - //If syncedFileState match the remote file if (remoteFilePath.equals(syncedFileState.getRemotePath())) { Log.d(TAG, "correspondant found for "+remoteFilePath ); correspondant_found = true; - if (syncedFileState.isLastEtagStored() //there is an etag stored - && (!remoteFile.getEtag().equals(syncedFileState.getLastETAG()) //If etag has changed - || syncedFileState.getLocalLastModified() == 0L)) { //File hasn't been downloaded - - Log.v(TAG, "etag and localLastModified are valids for "+remoteFilePath ); + final FileDiffUtils.Action action = getActionForFileDiff(remoteFile, syncedFileState); + if (action == FileDiffUtils.Action.Download) { - //compare size with local file - if (remoteFile.getLength() == new File(syncedFileState.getLocalPath()).length()) { //length is 0 is file doesn't exist - Log.v(TAG, "file size are the same for local and remote, just update syncedFileState etag"); + Log.i(TAG, "Add download operation for file "+syncedFileState.getId()); + this.syncRequests.put(syncedFileState.getId(), new DownloadRequest(remoteFile, syncedFileState)); - syncedFileState.setLastETAG(remoteFile.getEtag()); - int affectedRows = DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", this); - Log.v(TAG, affectedRows + " syncedFileState.s row in DB has been updated."); - } else { - Log.i(TAG, "Add download operation for file "+syncedFileState.getId()); + } else if (action == FileDiffUtils.Action.updateDB) { - this.syncRequests.put(syncedFileState.getId(), new DownloadRequest(remoteFile, syncedFileState)); - } + syncedFileState.setLastETAG(remoteFile.getEtag()); + final int affectedRows = DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", this); + if (affectedRows == 0) Log.e(TAG, "Error while updating eTag in DB for: "+remoteFilePath); } - syncedFileListIterator.remove(); //we can delete syncedFile from list because its correspondant has already been found and handled + syncedFileListIterator.remove(); break; } } - if ( correspondant_found )continue; + if (correspondant_found) continue; - //If we get here, RemoteFile is a new file to download - Log.v(TAG, "SyncedFileState corresponding to remoteFile not found."); + Log.v(TAG, remoteFilePath + "is a new file"); //Extract parent folder's path of remote file final String parentOfKnownPath = remoteFilePath.substring(0, remoteFilePath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1); diff --git a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java new file mode 100644 index 00000000..55510464 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java @@ -0,0 +1,81 @@ +/* + * Copyright © ECORP SAS 2022. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.utils; + +import com.owncloud.android.lib.resources.files.model.RemoteFile; + +import java.io.File; + +import foundation.e.drive.models.SyncedFileState; + +/** + * This class encapsulate code to compare syncedFile & Remote file + * but also RemoteFolder and SyncedFolder + * @author vincent Bourgmayer + */ +public class FileDiffUtils { + + public enum Action { + Upload, + Download, + Remove, + skip, + updateDB + } + + + /** + * Define what to do of RemoteFile for which we know the Database equivalent + * @param remoteFile RemoteFile + * @param fileState SyncedFileState instance + * @return Action from Enum + */ + public static Action getActionForFileDiff(RemoteFile remoteFile, SyncedFileState fileState) { + if (hasAlreadyBeenDownloaded(fileState) && !hasEtagChanged(remoteFile, fileState)) { + return Action.skip; + } + final File localFile = new File(fileState.getLocalPath()); + if (isRemoteSizeSameAsLocalSize(remoteFile, localFile)) { + return Action.updateDB; + } else { + return Action.Download; + } + } + + /** + * Compare RemoteFile's eTag with the one stored in Database + * @param file RemoteFile + * @param fileState last store file's state + * @return true if ETag + */ + private static boolean hasEtagChanged(RemoteFile file, SyncedFileState fileState) { + //if SyncedFileState has no Etag then it hasn't been uploaded and so must not exist on server + return fileState.isLastEtagStored() && !file.getEtag().equals(fileState.getLastETAG()); + } + + /** + * Indicate if the file has already been downloaded + * or detected on the device + * @param fileState SyncedFileState containing data from Database + * @return true if localLastModified store in Database == 0 + */ + private static boolean hasAlreadyBeenDownloaded(SyncedFileState fileState) { + return fileState.getLocalLastModified() == 0l; + } + + /** + * + * @param remoteFile RemoteFile instance + * @param localFile File instance + * @return true if remote file size is same as local file size + */ + private static boolean isRemoteSizeSameAsLocalSize(RemoteFile remoteFile, File localFile) { + // if local file doesn't exist its size will be 0 + return remoteFile.getLength() == localFile.length(); + } +} -- GitLab From 10da3b6eda6dafc1cfe101b1935afd872de196ef Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 2 Sep 2022 10:03:44 +0200 Subject: [PATCH 2/7] clean codes --- .../java/foundation/e/drive/services/ObserverService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index f2bc993e..2fa93e89 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -47,7 +47,7 @@ import foundation.e.drive.receivers.DebugCmdReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; -import foundation.e.drive.utils.FileDiffUtils; +import foundation.e.drive.utils.FileDiffUtils.Action; import foundation.e.drive.utils.ServiceExceptionHandler; import foundation.e.drive.utils.SynchronizationServiceConnection; @@ -386,13 +386,13 @@ public class ObserverService extends Service implements OnRemoteOperationListene Log.d(TAG, "correspondant found for "+remoteFilePath ); correspondant_found = true; - final FileDiffUtils.Action action = getActionForFileDiff(remoteFile, syncedFileState); - if (action == FileDiffUtils.Action.Download) { + final Action action = getActionForFileDiff(remoteFile, syncedFileState); + if (action == Action.Download) { Log.i(TAG, "Add download operation for file "+syncedFileState.getId()); this.syncRequests.put(syncedFileState.getId(), new DownloadRequest(remoteFile, syncedFileState)); - } else if (action == FileDiffUtils.Action.updateDB) { + } else if (action == Action.updateDB) { syncedFileState.setLastETAG(remoteFile.getEtag()); final int affectedRows = DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", this); -- GitLab From b021e807e995f6f94f71496f515f8102ba0587ae Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Sep 2022 09:33:47 +0000 Subject: [PATCH 3/7] Apply 1 suggestion(s) to 1 file(s) --- .../main/java/foundation/e/drive/services/ObserverService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index 2fa93e89..d5e41fd8 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -389,7 +389,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene final Action action = getActionForFileDiff(remoteFile, syncedFileState); if (action == Action.Download) { - Log.i(TAG, "Add download operation for file "+syncedFileState.getId()); + Log.i(TAG, "Add download operation for file " + syncedFileState.getId()); this.syncRequests.put(syncedFileState.getId(), new DownloadRequest(remoteFile, syncedFileState)); } else if (action == Action.updateDB) { -- GitLab From 68cee7f736fc80851067185312679dacc8dbd020 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Sep 2022 09:33:56 +0000 Subject: [PATCH 4/7] Apply 1 suggestion(s) to 1 file(s) --- .../main/java/foundation/e/drive/services/ObserverService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index d5e41fd8..b80f4066 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -396,7 +396,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene syncedFileState.setLastETAG(remoteFile.getEtag()); final int affectedRows = DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", this); - if (affectedRows == 0) Log.e(TAG, "Error while updating eTag in DB for: "+remoteFilePath); + if (affectedRows == 0) Log.e(TAG, "Error while updating eTag in DB for: " + remoteFilePath); } syncedFileListIterator.remove(); break; -- GitLab From 81629ef3df2ef6963087d5acc6c85feb98a0c716 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Sep 2022 09:34:05 +0000 Subject: [PATCH 5/7] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java index 55510464..3b3ae9a6 100644 --- a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java @@ -5,6 +5,7 @@ * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl.html */ + package foundation.e.drive.utils; import com.owncloud.android.lib.resources.files.model.RemoteFile; -- GitLab From 5fbab34d462f882d733e27db1f52005274dddfea Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Sep 2022 09:34:15 +0000 Subject: [PATCH 6/7] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java index 3b3ae9a6..87b62cea 100644 --- a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java @@ -43,9 +43,9 @@ public class FileDiffUtils { final File localFile = new File(fileState.getLocalPath()); if (isRemoteSizeSameAsLocalSize(remoteFile, localFile)) { return Action.updateDB; - } else { - return Action.Download; } + + return Action.Download; } /** -- GitLab From e15865c6121a62279ae1fe75a31faac95985ffb5 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Sep 2022 09:34:28 +0000 Subject: [PATCH 7/7] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java index 87b62cea..1edaa713 100644 --- a/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/FileDiffUtils.java @@ -40,7 +40,9 @@ public class FileDiffUtils { if (hasAlreadyBeenDownloaded(fileState) && !hasEtagChanged(remoteFile, fileState)) { return Action.skip; } + final File localFile = new File(fileState.getLocalPath()); + if (isRemoteSizeSameAsLocalSize(remoteFile, localFile)) { return Action.updateDB; } -- GitLab