NCShareNetworking.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // swiftlint:disable force_cast
  26. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. // swiftlint:enable force_cast
  28. weak var delegate: NCShareNetworkingDelegate?
  29. var view: UIView
  30. var metadata: tableMetadata
  31. init(metadata: tableMetadata, view: UIView, delegate: NCShareNetworkingDelegate?) {
  32. self.metadata = metadata
  33. self.view = view
  34. self.delegate = delegate
  35. super.init()
  36. }
  37. func readShare(showLoadingIndicator: Bool) {
  38. if showLoadingIndicator {
  39. NCActivityIndicator.shared.start(backgroundView: view)
  40. }
  41. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, account: metadata.account)!
  42. let parameter = NKShareParameter(path: filenamePath)
  43. NextcloudKit.shared.readShares(parameters: parameter) { account, shares, _, error in
  44. if error == .success, let shares = shares {
  45. NCManageDatabase.shared.deleteTableShare(account: account, path: "/" + filenamePath)
  46. let home = NCUtilityFileSystem.shared.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  47. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: shares)
  48. NextcloudKit.shared.getGroupfolders { account, results, _, error in
  49. if showLoadingIndicator {
  50. NCActivityIndicator.shared.stop()
  51. }
  52. if error == .success, let groupfolders = results {
  53. NCManageDatabase.shared.addGroupfolders(account: account, groupfolders: groupfolders)
  54. }
  55. self.delegate?.readShareCompleted()
  56. }
  57. } else {
  58. if showLoadingIndicator {
  59. NCActivityIndicator.shared.stop()
  60. }
  61. NCContentPresenter.shared.showError(error: error)
  62. self.delegate?.readShareCompleted()
  63. }
  64. }
  65. }
  66. func createShare(option: NCTableShareable) {
  67. // NOTE: Permissions don't work for creating with file drop!
  68. // https://github.com/nextcloud/server/issues/17504
  69. // NOTE: Can't save label and expirationDate in the same request.
  70. // Library update needed:
  71. // https://github.com/nextcloud/ios-communication-library/pull/104
  72. NCActivityIndicator.shared.start(backgroundView: view)
  73. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, account: metadata.account)!
  74. 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
  75. NCActivityIndicator.shared.stop()
  76. if error == .success, let share = share {
  77. option.idShare = share.idShare
  78. let home = NCUtilityFileSystem.shared.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  79. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: [share])
  80. if option.hasChanges(comparedTo: share) {
  81. self.updateShare(option: option)
  82. }
  83. } else {
  84. NCContentPresenter.shared.showError(error: error)
  85. }
  86. self.delegate?.shareCompleted()
  87. }
  88. }
  89. func unShare(idShare: Int) {
  90. NCActivityIndicator.shared.start(backgroundView: view)
  91. NextcloudKit.shared.deleteShare(idShare: idShare) { account, error in
  92. NCActivityIndicator.shared.stop()
  93. if error == .success {
  94. NCManageDatabase.shared.deleteTableShare(account: account, idShare: idShare)
  95. self.delegate?.unShareCompleted()
  96. } else {
  97. NCContentPresenter.shared.showError(error: error)
  98. }
  99. }
  100. }
  101. func updateShare(option: NCTableShareable) {
  102. NCActivityIndicator.shared.start(backgroundView: view)
  103. 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
  104. NCActivityIndicator.shared.stop()
  105. if error == .success, let share = share {
  106. let home = NCUtilityFileSystem.shared.getHomeServer(urlBase: self.metadata.urlBase, userId: self.metadata.userId)
  107. NCManageDatabase.shared.addShare(account: self.metadata.account, home: home, shares: [share])
  108. self.delegate?.readShareCompleted()
  109. } else {
  110. NCContentPresenter.shared.showError(error: error)
  111. self.delegate?.updateShareWithError(idShare: option.idShare)
  112. }
  113. }
  114. }
  115. func getSharees(searchString: String) {
  116. NCActivityIndicator.shared.start(backgroundView: view)
  117. NextcloudKit.shared.searchSharees(search: searchString) { _, sharees, _, error in
  118. NCActivityIndicator.shared.stop()
  119. if error == .success {
  120. self.delegate?.getSharees(sharees: sharees)
  121. } else {
  122. NCContentPresenter.shared.showError(error: error)
  123. self.delegate?.getSharees(sharees: nil)
  124. }
  125. }
  126. }
  127. }
  128. protocol NCShareNetworkingDelegate: AnyObject {
  129. func readShareCompleted()
  130. func shareCompleted()
  131. func unShareCompleted()
  132. func updateShareWithError(idShare: Int)
  133. func getSharees(sharees: [NKSharee]?)
  134. }