From eeb1ebc878d4e9a44304343761abd860323d541c Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 17 Nov 2022 17:05:41 +0100 Subject: [PATCH 1/2] Change way to move file at the end of a download --- .../operations/DownloadFileOperation.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 86664526..741faf98 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -14,6 +14,8 @@ import static com.owncloud.android.lib.common.operations.RemoteOperationResult.R import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.INVALID_OVERWRITE; import static com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode.OK; +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + import android.content.Context; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -21,6 +23,9 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.resources.files.DownloadFileRemoteOperation; import com.owncloud.android.lib.resources.files.model.RemoteFile; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.utils.CommonUtils; @@ -142,18 +147,13 @@ public class DownloadFileOperation extends RemoteOperation { * @return true if success, false otherwise */ private boolean moveFileToRealLocation(File tmpFile, File targetFile) { - final File targetFolder = targetFile.getParentFile(); - try { - if (!targetFolder.exists()) { - targetFolder.mkdirs(); - } else if (targetFile.exists()) { - targetFile.delete(); - } - return tmpFile.renameTo(targetFile); - } catch (SecurityException | NullPointerException exception) { + Files.copy(tmpFile.toPath(), targetFile.toPath(), REPLACE_EXISTING); + tmpFile.delete(); + } catch (IOException | SecurityException | NullPointerException exception) { Timber.e(exception); + return false; } - return false; + return true; } } -- GitLab From 267d1ab36827bad5e2d62ffcad1385336ce7340e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 17 Nov 2022 17:22:36 +0100 Subject: [PATCH 2/2] Compare file size after the copy operation to check success --- .../e/drive/operations/DownloadFileOperation.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 741faf98..6c9d6675 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -25,6 +25,7 @@ import com.owncloud.android.lib.resources.files.model.RemoteFile; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFileState; @@ -148,12 +149,14 @@ public class DownloadFileOperation extends RemoteOperation { */ private boolean moveFileToRealLocation(File tmpFile, File targetFile) { try { - Files.copy(tmpFile.toPath(), targetFile.toPath(), REPLACE_EXISTING); - tmpFile.delete(); + final Path copyResult = Files.copy(tmpFile.toPath(), targetFile.toPath(), REPLACE_EXISTING); + if (copyResult.toFile().length() == tmpFile.length()) { + tmpFile.delete(); + return true; + } } catch (IOException | SecurityException | NullPointerException exception) { Timber.e(exception); - return false; } - return true; + return false; } } -- GitLab