NCShareNetworking.swift 6.0 KB

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