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 86664526bb9267a0a5d0b430783ec3a8c5e54cf7..6c9d6675e011080e294f5abc146803ae660155f5 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,10 @@ 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 java.nio.file.Path; + import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.utils.CommonUtils; @@ -142,16 +148,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(); + final Path copyResult = Files.copy(tmpFile.toPath(), targetFile.toPath(), REPLACE_EXISTING); + if (copyResult.toFile().length() == tmpFile.length()) { + tmpFile.delete(); + return true; } - return tmpFile.renameTo(targetFile); - } catch (SecurityException | NullPointerException exception) { + } catch (IOException | SecurityException | NullPointerException exception) { Timber.e(exception); } return false;