CopyFileOperation.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2021 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-FileCopyrightText: 2019 Andy Scherzinger <info@andy-scherzinger.de>
  6. * SPDX-FileCopyrightText: 2012-2014 ownCloud Inc.
  7. * SPDX-FileCopyrightText: 2014 Jorge Antonio Diaz-Benito Soriano <jorge.diazbenitosoriano@gmail.com>
  8. * SPDX-License-Identifier: GPL-2.0-only AND (AGPL-3.0-or-later OR GPL-2.0-only)
  9. */
  10. package com.owncloud.android.operations;
  11. import com.owncloud.android.datamodel.FileDataStorageManager;
  12. import com.owncloud.android.datamodel.OCFile;
  13. import com.owncloud.android.lib.common.OwnCloudClient;
  14. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  15. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  16. import com.owncloud.android.lib.resources.files.CopyFileRemoteOperation;
  17. import com.owncloud.android.operations.common.SyncOperation;
  18. /**
  19. * Operation copying an {@link OCFile} to a different folder.
  20. *
  21. * @author David A. Velasco
  22. */
  23. public class CopyFileOperation extends SyncOperation {
  24. private final String srcPath;
  25. private String targetParentPath;
  26. /**
  27. * Constructor
  28. *
  29. * @param srcPath Remote path of the {@link OCFile} to move.
  30. * @param targetParentPath Path to the folder where the file will be copied into.
  31. */
  32. public CopyFileOperation(String srcPath, String targetParentPath, FileDataStorageManager storageManager) {
  33. super(storageManager);
  34. this.srcPath = srcPath;
  35. this.targetParentPath = targetParentPath;
  36. if (!this.targetParentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  37. this.targetParentPath += OCFile.PATH_SEPARATOR;
  38. }
  39. }
  40. /**
  41. * Performs the operation.
  42. *
  43. * @param client Client object to communicate with the remote ownCloud server.
  44. */
  45. @Override
  46. protected RemoteOperationResult run(OwnCloudClient client) {
  47. /// 1. check copy validity
  48. if (targetParentPath.startsWith(srcPath)) {
  49. return new RemoteOperationResult(ResultCode.INVALID_COPY_INTO_DESCENDANT);
  50. }
  51. OCFile file = getStorageManager().getFileByPath(srcPath);
  52. if (file == null) {
  53. return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND);
  54. }
  55. /// 2. remote copy
  56. String targetPath = targetParentPath + file.getFileName();
  57. if (file.isFolder()) {
  58. targetPath += OCFile.PATH_SEPARATOR;
  59. }
  60. // auto rename, to allow copy
  61. if (targetPath.equals(srcPath)) {
  62. if (file.isFolder()) {
  63. targetPath = targetParentPath + file.getFileName();
  64. }
  65. targetPath = UploadFileOperation.getNewAvailableRemotePath(client, targetPath, null, false);
  66. if (file.isFolder()) {
  67. targetPath += OCFile.PATH_SEPARATOR;
  68. }
  69. }
  70. RemoteOperationResult result = new CopyFileRemoteOperation(srcPath, targetPath, false).execute(client);
  71. /// 3. local copy
  72. if (result.isSuccess()) {
  73. getStorageManager().copyLocalFile(file, targetPath);
  74. }
  75. // TODO handle ResultCode.PARTIAL_COPY_DONE in client Activity, for the moment
  76. return result;
  77. }
  78. }