UpdateShareViaLinkOperation.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import lombok.Getter;
  32. import lombok.Setter;
  33. /**
  34. * Updates an existing public share for a given file
  35. */
  36. public class UpdateShareViaLinkOperation extends SyncOperation {
  37. @Getter private String path;
  38. @Getter @Setter private String password;
  39. /** Enable upload permissions to update in Share resource. */
  40. @Setter private Boolean publicUploadOnFolder;
  41. @Setter private Boolean publicUploadOnFile;
  42. @Setter private Boolean hideFileDownload;
  43. @Setter private long expirationDateInMillis;
  44. /**
  45. * Constructor
  46. *
  47. * @param path Full path of the file/folder being shared. Mandatory argument
  48. */
  49. public UpdateShareViaLinkOperation(String path) {
  50. this.path = path;
  51. expirationDateInMillis = 0;
  52. }
  53. @Override
  54. protected RemoteOperationResult run(OwnCloudClient client) {
  55. OCShare publicShare = getStorageManager().getFirstShareByPathAndType(path, ShareType.PUBLIC_LINK, "");
  56. if (publicShare == null) {
  57. // TODO try to get remote share before failing?
  58. return new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
  59. }
  60. UpdateRemoteShareOperation updateOp = new UpdateRemoteShareOperation(publicShare.getRemoteId());
  61. updateOp.setPassword(password);
  62. updateOp.setExpirationDate(expirationDateInMillis);
  63. // updateOp.setPublicUploadOnFolder(publicUploadOnFolder);
  64. // updateOp.setPublicUploadOnFile(publicUploadOnFile);
  65. updateOp.setHideFileDownload(hideFileDownload);
  66. RemoteOperationResult result = updateOp.execute(client);
  67. if (result.isSuccess()) {
  68. // Retrieve updated share / save directly with password? -> no; the password is not to be saved
  69. RemoteOperation getShareOp = new GetRemoteShareOperation(publicShare.getRemoteId());
  70. result = getShareOp.execute(client);
  71. if (result.isSuccess()) {
  72. OCShare share = (OCShare) result.getData().get(0);
  73. updateData(share);
  74. }
  75. }
  76. return result;
  77. }
  78. private void updateData(OCShare share) {
  79. // Update DB with the response
  80. share.setPath(path);
  81. if (path.endsWith(FileUtils.PATH_SEPARATOR)) {
  82. share.setFolder(true);
  83. } else {
  84. share.setFolder(false);
  85. }
  86. getStorageManager().saveShare(share); // TODO info about having a password? ask to Gonzalo
  87. // Update OCFile with data from share: ShareByLink and publicLink
  88. // TODO check & remove if not needed
  89. OCFile file = getStorageManager().getFileByPath(path);
  90. if (file != null) {
  91. file.setPublicLink(share.getShareLink());
  92. file.setSharedViaLink(true);
  93. getStorageManager().saveFile(file);
  94. }
  95. }
  96. }