NCShareNetworking.swift 6.6 KB

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