CreateShareOperation.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  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. /**
  22. * Creates a new share from a given file
  23. */
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.datamodel.FileDataStorageManager;
  28. import com.owncloud.android.datamodel.OCFile;
  29. import com.owncloud.android.lib.common.OwnCloudClient;
  30. import com.owncloud.android.lib.resources.shares.OCShare;
  31. import com.owncloud.android.lib.common.operations.RemoteOperation;
  32. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
  35. import com.owncloud.android.lib.resources.shares.ShareType;
  36. import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
  37. import com.owncloud.android.lib.resources.files.FileUtils;
  38. import com.owncloud.android.operations.common.SyncOperation;
  39. public class CreateShareOperation extends SyncOperation {
  40. private static final String TAG = CreateShareOperation.class.getSimpleName();
  41. protected FileDataStorageManager mStorageManager;
  42. private Context mContext;
  43. private String mPath;
  44. private ShareType mShareType;
  45. private String mShareWith;
  46. private boolean mPublicUpload;
  47. private String mPassword;
  48. private int mPermissions;
  49. private Intent mSendIntent;
  50. /**
  51. * Constructor
  52. * @param context The context that the share is coming from.
  53. * @param path Full path of the file/folder being shared. Mandatory argument
  54. * @param shareType 0 = user, 1 = group, 3 = Public link. Mandatory argument
  55. * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
  56. * @param publicUpload If false (default) public cannot upload to a public shared folder.
  57. * If true public can upload to a shared folder. Only available for public link shares
  58. * @param password Password to protect a public link share. Only available for public link shares
  59. * @param permissions 1 - Read only - Default for public shares
  60. * 2 - Update
  61. * 4 - Create
  62. * 8 - Delete
  63. * 16- Re-share
  64. * 31- All above - Default for private shares
  65. * For user or group shares.
  66. * To obtain combinations, add the desired values together.
  67. * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
  68. */
  69. public CreateShareOperation(Context context, String path, ShareType shareType, String shareWith,
  70. boolean publicUpload, String password, int permissions,
  71. Intent sendIntent) {
  72. mContext = context;
  73. mPath = path;
  74. mShareType = shareType;
  75. mShareWith = shareWith;
  76. mPublicUpload = publicUpload;
  77. mPassword = password;
  78. mPermissions = permissions;
  79. mSendIntent = sendIntent;
  80. }
  81. @Override
  82. protected RemoteOperationResult run(OwnCloudClient client) {
  83. RemoteOperation operation = null;
  84. // Check if the share link already exists
  85. operation = new GetRemoteSharesForFileOperation(mPath, false, false);
  86. RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
  87. if (!result.isSuccess() || result.getData().size() <= 0) {
  88. operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload,
  89. mPassword, mPermissions);
  90. result = ((CreateRemoteShareOperation)operation).execute(client);
  91. }
  92. if (result.isSuccess()) {
  93. if (result.getData().size() > 0) {
  94. OCShare share = (OCShare) result.getData().get(0);
  95. updateData(share);
  96. }
  97. }
  98. return result;
  99. }
  100. public String getPath() {
  101. return mPath;
  102. }
  103. public ShareType getShareType() {
  104. return mShareType;
  105. }
  106. public String getShareWith() {
  107. return mShareWith;
  108. }
  109. public boolean getPublicUpload() {
  110. return mPublicUpload;
  111. }
  112. public String getPassword() {
  113. return mPassword;
  114. }
  115. public int getPermissions() {
  116. return mPermissions;
  117. }
  118. public Intent getSendIntent() {
  119. return mSendIntent;
  120. }
  121. private void updateData(OCShare share) {
  122. // Update DB with the response
  123. share.setPath(mPath);
  124. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  125. share.setIsFolder(true);
  126. } else {
  127. share.setIsFolder(false);
  128. }
  129. share.setPermissions(mPermissions);
  130. getStorageManager().saveShare(share);
  131. // Update OCFile with data from share: ShareByLink and publicLink
  132. OCFile file = getStorageManager().getFileByPath(mPath);
  133. if (file!=null) {
  134. mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
  135. mSendIntent.putExtra(Intent.EXTRA_SUBJECT, String.format(mContext.getString(R.string.subject_token),
  136. getClient().getCredentials().getUsername(), file.getFileName()));
  137. file.setPublicLink(share.getShareLink());
  138. file.setShareByLink(true);
  139. getStorageManager().saveFile(file);
  140. Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
  141. }
  142. }
  143. }