NCShareNetworking.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 NCCommunication
  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. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  40. }
  41. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase, account: metadata.account)!
  42. let parameter = NCCShareParameter(path: filenamePath)
  43. NCCommunication.shared.readShares(parameters: parameter) { account, shares, errorCode, errorDescription in
  44. if showLoadingIndicator {
  45. NCUtility.shared.stopActivityIndicator()
  46. }
  47. if errorCode == 0 && shares != nil {
  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.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  52. }
  53. self.delegate?.readShareCompleted()
  54. }
  55. }
  56. func createShareLink(password: String?) {
  57. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  58. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase, account: metadata.account)!
  59. NCCommunication.shared.createShareLink(path: filenamePath, password: password) { account, share, errorCode, errorDescription in
  60. NCUtility.shared.stopActivityIndicator()
  61. if errorCode == 0 && share != nil {
  62. NCManageDatabase.shared.addShare(urlBase: self.urlBase, account: self.metadata.account, shares: [share!])
  63. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: self.metadata.account)
  64. } else if errorCode != 0 {
  65. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  66. }
  67. self.delegate?.shareCompleted()
  68. }
  69. }
  70. func createShare(option: TableShareable, metadata: tableMetadata) {
  71. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  72. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase, account: metadata.account)!
  73. let permission = max(1, metadata.sharePermissionsCollaborationServices & option.permissions)
  74. NCCommunication.shared.createShare(path: filenamePath, shareType: option.shareType, shareWith: option.shareWith, password: option.password, permissions: permission) { (account, share, errorCode, errorDescription) in
  75. NCUtility.shared.stopActivityIndicator()
  76. if errorCode == 0 && share != nil {
  77. NCManageDatabase.shared.addShare(urlBase: self.urlBase, account: self.metadata.account, shares: [share!])
  78. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: self.metadata.account)
  79. } else {
  80. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  81. }
  82. self.delegate?.shareCompleted()
  83. }
  84. }
  85. func unShare(idShare: Int) {
  86. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  87. NCCommunication.shared.deleteShare(idShare: idShare) { account, errorCode, errorDescription in
  88. NCUtility.shared.stopActivityIndicator()
  89. if errorCode == 0 {
  90. NCManageDatabase.shared.deleteTableShare(account: account, idShare: idShare)
  91. self.delegate?.unShareCompleted()
  92. } else {
  93. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  94. }
  95. }
  96. }
  97. func updateShare(option: TableShareable) {
  98. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  99. NCCommunication.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, errorCode, errorDescription in
  100. NCUtility.shared.stopActivityIndicator()
  101. if errorCode == 0 && share != nil {
  102. NCManageDatabase.shared.addShare(urlBase: self.urlBase, account: self.metadata.account, shares: [share!])
  103. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: self.metadata.account)
  104. self.delegate?.readShareCompleted()
  105. } else {
  106. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  107. self.delegate?.updateShareWithError(idShare: option.idShare)
  108. }
  109. }
  110. }
  111. func getSharees(searchString: String) {
  112. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  113. NCCommunication.shared.searchSharees(search: searchString) { _, sharees, errorCode, errorDescription in
  114. NCUtility.shared.stopActivityIndicator()
  115. if errorCode == 0 {
  116. self.delegate?.getSharees(sharees: sharees)
  117. } else {
  118. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  119. self.delegate?.getSharees(sharees: nil)
  120. }
  121. }
  122. }
  123. }
  124. protocol NCShareNetworkingDelegate: AnyObject {
  125. func readShareCompleted()
  126. func shareCompleted()
  127. func unShareCompleted()
  128. func updateShareWithError(idShare: Int)
  129. func getSharees(sharees: [NCCommunicationSharee]?)
  130. }