|
51 | 51 | import com.owncloud.android.MainApp; |
52 | 52 | import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; |
53 | 53 | import com.owncloud.android.lib.common.network.WebdavEntry; |
54 | | -import com.owncloud.android.lib.common.operations.RemoteOperationResult; |
55 | 54 | import com.owncloud.android.lib.common.utils.Log_OC; |
56 | 55 | import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation; |
57 | 56 | import com.owncloud.android.lib.resources.files.model.FileLockType; |
@@ -497,9 +496,13 @@ public List<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) { |
497 | 496 | } |
498 | 497 |
|
499 | 498 | public boolean saveFile(OCFile ocFile) { |
| 499 | + Log_OC.d(TAG, "saving file: " + ocFile.getRemotePath()); |
| 500 | + |
500 | 501 | boolean overridden = false; |
501 | 502 | final ContentValues cv = createContentValuesForFile(ocFile); |
502 | 503 | if (ocFile.isFolder()) { |
| 504 | + // only refresh folder operation must update eTag otherwise content of the folder may stay as outdated |
| 505 | + cv.remove(ProviderTableMeta.FILE_ETAG); |
503 | 506 | cv.remove(ProviderTableMeta.FILE_STORAGE_PATH); |
504 | 507 | } |
505 | 508 |
|
@@ -547,46 +550,65 @@ public boolean saveFile(OCFile ocFile) { |
547 | 550 | } |
548 | 551 |
|
549 | 552 | /** |
550 | | - * traverses a files parent tree to be able to store a file with its parents. Throws a |
551 | | - * RemoteOperationFailedException in case the parent can't be retrieved. |
| 553 | + * Ensures that an {@link OCFile} and all of its parent folders are stored locally. |
| 554 | + * <p> |
| 555 | + * If the file has no parent ID and is not the root folder, this method recursively: |
| 556 | + * <ul> |
| 557 | + * <li>Resolves the parent path</li> |
| 558 | + * <li>Loads the parent from local storage or fetches it from the server</li> |
| 559 | + * <li>Saves all missing parent folders</li> |
| 560 | + * <li>Assigns the resolved parent ID to the file</li> |
| 561 | + * </ul> |
| 562 | + * |
| 563 | + * @param ocFile the file to be saved together with its parent hierarchy |
| 564 | + * @param context Android context used for remote operations |
| 565 | + * |
| 566 | + * @return the same {@link OCFile} instance with a valid parent ID |
552 | 567 | * |
553 | | - * @param ocFile the file |
554 | | - * @param context the app context |
555 | | - * @return the parent file |
| 568 | + * @throws RemoteOperationFailedException if a parent folder cannot be retrieved |
| 569 | + * from the server |
556 | 570 | */ |
557 | 571 | public OCFile saveFileWithParent(OCFile ocFile, Context context) { |
558 | 572 | if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) { |
| 573 | + Log_OC.d(TAG, "saving file with parents: " + ocFile.getRemotePath()); |
| 574 | + |
559 | 575 | String remotePath = ocFile.getRemotePath(); |
560 | 576 | String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName())); |
561 | 577 |
|
562 | 578 | OCFile parentFile = getFileByPath(parentPath); |
563 | 579 | OCFile returnFile; |
564 | 580 |
|
565 | 581 | if (parentFile == null) { |
566 | | - // remote request |
567 | | - ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath); |
568 | | - // TODO Deprecated |
569 | | - RemoteOperationResult result = operation.execute(getUser(), context); |
570 | | - if (result.isSuccess()) { |
571 | | - OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); |
572 | | - |
573 | | - returnFile = saveFileWithParent(remoteFolder, context); |
| 582 | + Log_OC.d(TAG, "Parent not found locally, fetching: " + parentPath); |
| 583 | + |
| 584 | + final var operation = new ReadFileRemoteOperation(parentPath); |
| 585 | + final var result = operation.execute(getUser(), context); |
| 586 | + |
| 587 | + if (result.isSuccess() && result.getData().get(0) instanceof RemoteFile remoteFile) { |
| 588 | + OCFile folder = FileStorageUtils.fillOCFile(remoteFile); |
| 589 | + Log_OC.d(TAG, "Fetched parent folder: " + folder); |
| 590 | + returnFile = saveFileWithParent(folder, context); |
574 | 591 | } else { |
575 | 592 | Exception exception = result.getException(); |
576 | 593 | String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / " |
577 | 594 | + result.getLogMessage(context); |
578 | 595 |
|
| 596 | + Log_OC.e(TAG, message); |
| 597 | + |
579 | 598 | if (exception != null) { |
580 | 599 | throw new RemoteOperationFailedException(message, exception); |
581 | 600 | } else { |
582 | 601 | throw new RemoteOperationFailedException(message); |
583 | 602 | } |
584 | 603 | } |
585 | 604 | } else { |
| 605 | + Log_OC.d(TAG, "parent file exists, calling saveFileWithParent: " + ocFile.getRemotePath()); |
586 | 606 | returnFile = saveFileWithParent(parentFile, context); |
587 | 607 | } |
588 | 608 |
|
589 | | - ocFile.setParentId(returnFile.getFileId()); |
| 609 | + long parentId = returnFile.getFileId(); |
| 610 | + Log_OC.d(TAG, "saving parent id of: " + ocFile.getRemotePath() + " with: " + parentId); |
| 611 | + ocFile.setParentId(parentId); |
590 | 612 | saveFile(ocFile); |
591 | 613 | } |
592 | 614 |
|
|
0 commit comments