UpdateShareViaLinkOperation.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.operations;
  21. import com.owncloud.android.datamodel.OCFile;
  22. import com.owncloud.android.lib.common.OwnCloudClient;
  23. import com.owncloud.android.lib.common.operations.RemoteOperation;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.lib.resources.files.FileUtils;
  26. import com.owncloud.android.lib.resources.shares.GetRemoteShareOperation;
  27. import com.owncloud.android.lib.resources.shares.OCShare;
  28. import com.owncloud.android.lib.resources.shares.ShareType;
  29. import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation;
  30. import com.owncloud.android.operations.common.SyncOperation;
  31. /**
  32. * Updates an existing public share for a given file
  33. */
  34. public class UpdateShareViaLinkOperation extends SyncOperation {
  35. private String mPath;
  36. private String mPassword;
  37. private Boolean mPublicUpload;
  38. private long mExpirationDateInMillis;
  39. /**
  40. * Constructor
  41. *
  42. * @param path Full path of the file/folder being shared. Mandatory argument
  43. */
  44. public UpdateShareViaLinkOperation(String path) {
  45. mPath = path;
  46. mPassword = null;
  47. mExpirationDateInMillis = 0;
  48. mPublicUpload = null;
  49. }
  50. /**
  51. * Set password to update in public link.
  52. *
  53. * @param password Password to set to the public link.
  54. * Empty string clears the current password.
  55. * Null results in no update applied to the password.
  56. */
  57. public void setPassword(String password) {
  58. mPassword = password;
  59. }
  60. /**
  61. * Set expiration date to update in Share resource.
  62. *
  63. * @param expirationDateInMillis Expiration date to set to the public link.
  64. * A negative value clears the current expiration date.
  65. * Zero value (start-of-epoch) results in no update done on
  66. * the expiration date.
  67. */
  68. public void setExpirationDate(long expirationDateInMillis) {
  69. mExpirationDateInMillis = expirationDateInMillis;
  70. }
  71. /**
  72. * Enable upload permissions to update in Share resource.
  73. *
  74. * @param publicUpload Upload Permission to set to the public link.
  75. * Null results in no update applied to the upload permission.
  76. */
  77. public void setPublicUpload(Boolean publicUpload) {
  78. mPublicUpload = publicUpload;
  79. }
  80. @Override
  81. protected RemoteOperationResult run(OwnCloudClient client) {
  82. OCShare publicShare = getStorageManager().getFirstShareByPathAndType(
  83. mPath,
  84. ShareType.PUBLIC_LINK,
  85. ""
  86. );
  87. if (publicShare == null) {
  88. // TODO try to get remote share before failing?
  89. return new RemoteOperationResult(
  90. RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
  91. );
  92. }
  93. // Update remote share with password
  94. UpdateRemoteShareOperation updateOp = new UpdateRemoteShareOperation(
  95. publicShare.getRemoteId()
  96. );
  97. updateOp.setPassword(mPassword);
  98. updateOp.setExpirationDate(mExpirationDateInMillis);
  99. updateOp.setPublicUpload(mPublicUpload);
  100. RemoteOperationResult result = updateOp.execute(client);
  101. if (result.isSuccess()) {
  102. // Retrieve updated share / save directly with password? -> no; the password is not to be saved
  103. RemoteOperation getShareOp = new GetRemoteShareOperation(publicShare.getRemoteId());
  104. result = getShareOp.execute(client);
  105. if (result.isSuccess()) {
  106. OCShare share = (OCShare) result.getData().get(0);
  107. updateData(share);
  108. }
  109. }
  110. return result;
  111. }
  112. public String getPath() {
  113. return mPath;
  114. }
  115. public String getPassword() {
  116. return mPassword;
  117. }
  118. private void updateData(OCShare share) {
  119. // Update DB with the response
  120. share.setPath(mPath);
  121. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  122. share.setIsFolder(true);
  123. } else {
  124. share.setIsFolder(false);
  125. }
  126. getStorageManager().saveShare(share); // TODO info about having a password? ask to Gonzalo
  127. // Update OCFile with data from share: ShareByLink and publicLink
  128. // TODO check & remove if not needed
  129. OCFile file = getStorageManager().getFileByPath(mPath);
  130. if (file != null) {
  131. file.setPublicLink(share.getShareLink());
  132. file.setShareViaLink(true);
  133. getStorageManager().saveFile(file);
  134. }
  135. }
  136. }