NCShareNetworking.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // NCShareNetworking.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/07/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. import UIKit
  23. import NextcloudKit
  24. class NCShareNetworking: NSObject {
  25. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  26. let utilityFileSystem = NCUtilityFileSystem()
  27. weak var delegate: NCShareNetworkingDelegate?
  28. var view: UIView
  29. var metadata: tableMetadata
  30. init(metadata: tableMetadata, view: UIView, delegate: NCShareNetworkingDelegate?) {
  31. self.metadata = metadata
  32. self.view = view
  33. self.delegate = delegate
  34. super.init()
  35. }
  36. func readShare(showLoadingIndicator: Bool) {
  37. if showLoadingIndicator {
  38. NCActivityIndicator.shared.start(backgroundView: view)
  39. }
  40. let filenamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  41. let parameter = NKShareParameter(path: filenamePath)
  42. NextcloudKit.shared.readShares(parameters: parameter) { account, shares, _, error in
  43. if error == .success, let shares = shares {
  44. NCManageDatabase.shared.deleteTableShare(account: account, path: "/" + filenamePath)
  45. let home = self.utilityFileSystem.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  46. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: shares)
  47. NextcloudKit.shared.getGroupfolders { account, results, _, error in
  48. if showLoadingIndicator {
  49. NCActivityIndicator.shared.stop()
  50. }
  51. if error == .success, let groupfolders = results {
  52. NCManageDatabase.shared.addGroupfolders(account: account, groupfolders: groupfolders)
  53. }
  54. self.delegate?.readShareCompleted()
  55. }
  56. } else {
  57. if showLoadingIndicator {
  58. NCActivityIndicator.shared.stop()
  59. }
  60. NCContentPresenter().showError(error: error)
  61. self.delegate?.readShareCompleted()
  62. }
  63. }
  64. }
  65. func createShare(option: NCTableShareable) {
  66. // NOTE: Permissions don't work for creating with file drop!
  67. // https://github.com/nextcloud/server/issues/17504
  68. // NOTE: Can't save label and expirationDate in the same request.
  69. // Library update needed:
  70. // https://github.com/nextcloud/ios-communication-library/pull/104
  71. NCActivityIndicator.shared.start(backgroundView: view)
  72. let filenamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  73. NextcloudKit.shared.createShare(path: filenamePath, shareType: option.shareType, shareWith: option.shareWith, password: option.password, note: option.note, permissions: option.permissions, attributes: option.attributes) { _, share, _, error in
  74. NCActivityIndicator.shared.stop()
  75. if error == .success, let share = share {
  76. option.idShare = share.idShare
  77. let home = self.utilityFileSystem.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  78. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: [share])
  79. if option.hasChanges(comparedTo: share) {
  80. self.updateShare(option: option)
  81. }
  82. } else {
  83. NCContentPresenter().showError(error: error)
  84. }
  85. self.delegate?.shareCompleted()
  86. }
  87. }
  88. func unShare(idShare: Int) {
  89. NCActivityIndicator.shared.start(backgroundView: view)
  90. NextcloudKit.shared.deleteShare(idShare: idShare) { account, error in
  91. NCActivityIndicator.shared.stop()
  92. if error == .success {
  93. NCManageDatabase.shared.deleteTableShare(account: account, idShare: idShare)
  94. self.delegate?.unShareCompleted()
  95. } else {
  96. NCContentPresenter().showError(error: error)
  97. }
  98. }
  99. }
  100. func updateShare(option: NCTableShareable) {
  101. NCActivityIndicator.shared.start(backgroundView: view)
  102. NextcloudKit.shared.updateShare(idShare: option.idShare, password: option.password, expireDate: option.expDateString, permissions: option.permissions, note: option.note, label: option.label, hideDownload: option.hideDownload, attributes: option.attributes) { _, share, _, error in
  103. NCActivityIndicator.shared.stop()
  104. if error == .success, let share = share {
  105. let home = self.utilityFileSystem.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  106. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: [share])
  107. self.delegate?.readShareCompleted()
  108. } else {
  109. NCContentPresenter().showError(error: error)
  110. self.delegate?.updateShareWithError(idShare: option.idShare)
  111. }
  112. }
  113. }
  114. func getSharees(searchString: String) {
  115. NCActivityIndicator.shared.start(backgroundView: view)
  116. NextcloudKit.shared.searchSharees(search: searchString) { _, sharees, _, error in
  117. NCActivityIndicator.shared.stop()
  118. if error == .success {
  119. self.delegate?.getSharees(sharees: sharees)
  120. } else {
  121. NCContentPresenter().showError(error: error)
  122. self.delegate?.getSharees(sharees: nil)
  123. }
  124. }
  125. }
  126. }
  127. protocol NCShareNetworkingDelegate: AnyObject {
  128. func readShareCompleted()
  129. func shareCompleted()
  130. func unShareCompleted()
  131. func updateShareWithError(idShare: Int)
  132. func getSharees(sharees: [NKSharee]?)
  133. }