CreateShareWithShareeOperation.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * @author TSI-mc
  7. * Copyright (C) 2015 ownCloud Inc.
  8. * Copyright (C) 2023 TSI-mc
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. package com.owncloud.android.operations;
  24. import android.text.TextUtils;
  25. import com.owncloud.android.datamodel.FileDataStorageManager;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.lib.common.OwnCloudClient;
  28. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  29. import com.owncloud.android.lib.resources.files.FileUtils;
  30. import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation;
  31. import com.owncloud.android.lib.resources.shares.OCShare;
  32. import com.owncloud.android.lib.resources.shares.ShareType;
  33. import com.owncloud.android.operations.common.SyncOperation;
  34. import java.util.Arrays;
  35. import java.util.HashSet;
  36. import java.util.Set;
  37. /**
  38. * Creates a new private share for a given file.
  39. */
  40. public class CreateShareWithShareeOperation extends SyncOperation {
  41. private String path;
  42. private String shareeName;
  43. private ShareType shareType;
  44. private int permissions;
  45. private String noteMessage;
  46. private String sharePassword;
  47. private boolean hideFileDownload;
  48. private long expirationDateInMillis;
  49. private String label;
  50. private static final Set<ShareType> supportedShareTypes = new HashSet<>(Arrays.asList(ShareType.USER,
  51. ShareType.GROUP,
  52. ShareType.FEDERATED,
  53. ShareType.EMAIL,
  54. ShareType.ROOM,
  55. ShareType.CIRCLE));
  56. /**
  57. * Constructor.
  58. *
  59. * @param path Full path of the file/folder being shared.
  60. * @param shareeName User or group name of the target sharee.
  61. * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
  62. * are the only valid values for the moment.
  63. * @param permissions Share permissions key as detailed in
  64. * https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-share-api.html#create-a-new-share
  65. * .
  66. */
  67. public CreateShareWithShareeOperation(String path,
  68. String shareeName,
  69. ShareType shareType,
  70. int permissions,
  71. FileDataStorageManager storageManager) {
  72. super(storageManager);
  73. if (!supportedShareTypes.contains(shareType)) {
  74. throw new IllegalArgumentException("Illegal share type " + shareType);
  75. }
  76. this.path = path;
  77. this.shareeName = shareeName;
  78. this.shareType = shareType;
  79. this.permissions = permissions;
  80. }
  81. /**
  82. * Constructor.
  83. *
  84. * @param path Full path of the file/folder being shared.
  85. * @param shareeName User or group name of the target sharee.
  86. * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
  87. * are the only valid values for the moment.
  88. * @param permissions Share permissions key as detailed in https://doc.owncloud.org/server/8.2/developer_manual/core/ocs-share-api.html
  89. * .
  90. */
  91. public CreateShareWithShareeOperation(String path,
  92. String shareeName,
  93. ShareType shareType,
  94. int permissions,
  95. String noteMessage,
  96. String sharePassword,
  97. long expirationDateInMillis,
  98. boolean hideFileDownload,
  99. FileDataStorageManager storageManager) {
  100. super(storageManager);
  101. if (!supportedShareTypes.contains(shareType)) {
  102. throw new IllegalArgumentException("Illegal share type " + shareType);
  103. }
  104. this.path = path;
  105. this.shareeName = shareeName;
  106. this.shareType = shareType;
  107. this.permissions = permissions;
  108. this.expirationDateInMillis = expirationDateInMillis;
  109. this.hideFileDownload = hideFileDownload;
  110. this.noteMessage = noteMessage;
  111. this.sharePassword = sharePassword;
  112. }
  113. @Override
  114. protected RemoteOperationResult run(OwnCloudClient client) {
  115. CreateShareRemoteOperation operation = new CreateShareRemoteOperation(
  116. path,
  117. shareType,
  118. shareeName,
  119. false,
  120. sharePassword,
  121. permissions,
  122. noteMessage
  123. );
  124. operation.setGetShareDetails(true);
  125. RemoteOperationResult result = operation.execute(client);
  126. if (result.isSuccess() && result.getData().size() > 0) {
  127. OCShare share = (OCShare) result.getData().get(0);
  128. //once creating share link update other information
  129. UpdateShareInfoOperation updateShareInfoOperation = new UpdateShareInfoOperation(share, getStorageManager());
  130. updateShareInfoOperation.setExpirationDateInMillis(expirationDateInMillis);
  131. updateShareInfoOperation.setHideFileDownload(hideFileDownload);
  132. updateShareInfoOperation.setLabel(label);
  133. //execute and save the result in database
  134. RemoteOperationResult updateShareInfoResult = updateShareInfoOperation.execute(client);
  135. if (updateShareInfoResult.isSuccess() && updateShareInfoResult.getData().size() > 0) {
  136. OCShare shareUpdated = (OCShare) updateShareInfoResult.getData().get(0);
  137. updateData(shareUpdated);
  138. }
  139. }
  140. return result;
  141. }
  142. private void updateData(OCShare share) {
  143. // Update DB with the response
  144. share.setPath(path);
  145. share.setFolder(path.endsWith(FileUtils.PATH_SEPARATOR));
  146. share.setPasswordProtected(!TextUtils.isEmpty(sharePassword));
  147. getStorageManager().saveShare(share);
  148. // Update OCFile with data from share: ShareByLink and publicLink
  149. OCFile file = getStorageManager().getFileByPath(path);
  150. if (file != null) {
  151. file.setSharedWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
  152. getStorageManager().saveFile(file);
  153. }
  154. }
  155. public String getPath() {
  156. return this.path;
  157. }
  158. public void setLabel(String label) {
  159. this.label = label;
  160. }
  161. }