NCShareNetworking.swift 7.8 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. //NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["serverUrl":self.metadata.serverUrl])
  23. import Foundation
  24. import NCCommunication
  25. class NCShareNetworking: NSObject {
  26. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var urlBase: String
  28. var delegate: NCShareNetworkingDelegate?
  29. var view: UIView
  30. var metadata: tableMetadata
  31. init(metadata: tableMetadata, urlBase: String, view: UIView, delegate: NCShareNetworkingDelegate?) {
  32. self.metadata = metadata
  33. self.urlBase = urlBase
  34. self.view = view
  35. self.delegate = delegate
  36. super.init()
  37. }
  38. func readShare() {
  39. NCUtility.sharedInstance.startActivityIndicator(view: view)
  40. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase)!
  41. NCCommunication.shared.readShares(path: filenamePath) { (account, shares, errorCode, errorDescription) in
  42. NCUtility.sharedInstance.stopActivityIndicator()
  43. if errorCode == 0 && shares != nil {
  44. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, urlBase: self.urlBase, shares: shares!)
  45. self.appDelegate.shares = NCManageDatabase.sharedInstance.getTableShares(account: self.metadata.account)
  46. } else {
  47. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  48. }
  49. self.delegate?.readShareCompleted()
  50. }
  51. }
  52. func createShareLink(password: String?) {
  53. NCUtility.sharedInstance.startActivityIndicator(view: view)
  54. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase)!
  55. NCCommunication.shared.createShareLink(path: filenamePath, password: password) { (account, share, errorCode, errorDescription) in
  56. NCUtility.sharedInstance.stopActivityIndicator()
  57. if errorCode == 0 && share != nil {
  58. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, urlBase: self.urlBase, shares: [share!])
  59. self.appDelegate.shares = NCManageDatabase.sharedInstance.getTableShares(account: self.metadata.account)
  60. } else if errorCode != 0 {
  61. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  62. }
  63. self.delegate?.shareCompleted()
  64. }
  65. }
  66. func createShare(shareWith: String, shareType: Int, metadata: tableMetadata) {
  67. NCUtility.sharedInstance.startActivityIndicator(view: view)
  68. let filenamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase)!
  69. var permission: Int = 1
  70. if metadata.directory { permission = Int(k_max_folder_share_permission) } else { permission = Int(k_max_file_share_permission) }
  71. NCCommunication.shared.createShare(path: filenamePath, shareType: shareType, shareWith: shareWith, permissions: permission) { (account, share, errorCode, errorDescription) in
  72. NCUtility.sharedInstance.stopActivityIndicator()
  73. if errorCode == 0 && share != nil {
  74. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, urlBase: self.urlBase, shares: [share!])
  75. self.appDelegate.shares = NCManageDatabase.sharedInstance.getTableShares(account: self.metadata.account)
  76. } else {
  77. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  78. }
  79. self.delegate?.shareCompleted()
  80. }
  81. }
  82. func unShare(idShare: Int) {
  83. NCUtility.sharedInstance.startActivityIndicator(view: view)
  84. NCCommunication.shared.deleteShare(idShare: idShare) { (account, errorCode, errorDescription) in
  85. NCUtility.sharedInstance.stopActivityIndicator()
  86. if errorCode == 0 {
  87. NCManageDatabase.sharedInstance.deleteTableShare(account: account, idShare: idShare)
  88. self.delegate?.unShareCompleted()
  89. } else {
  90. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  91. }
  92. }
  93. }
  94. func updateShare(idShare: Int, password: String?, permission: Int, note: String?, expirationDate: String?, hideDownload: Bool) {
  95. NCUtility.sharedInstance.startActivityIndicator(view: view)
  96. NCCommunication.shared.updateShare(idShare: idShare, password: password, expireDate: expirationDate, permissions: permission, note: note, hideDownload: hideDownload) { (account, share, errorCode, errorDescription) in
  97. NCUtility.sharedInstance.stopActivityIndicator()
  98. if errorCode == 0 && share != nil {
  99. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, urlBase: self.urlBase, shares: [share!])
  100. self.appDelegate.shares = NCManageDatabase.sharedInstance.getTableShares(account: self.metadata.account)
  101. self.delegate?.readShareCompleted()
  102. } else {
  103. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  104. self.delegate?.updateShareWithError(idShare: idShare)
  105. }
  106. }
  107. }
  108. func getSharees(searchString: String) {
  109. NCUtility.sharedInstance.startActivityIndicator(view: view)
  110. NCCommunication.shared.searchSharees(search: searchString) { (account, sharees, errorCode, errorDescription) in
  111. NCUtility.sharedInstance.stopActivityIndicator()
  112. if errorCode == 0 {
  113. self.delegate?.getSharees(sharees: sharees)
  114. } else {
  115. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError), forced: true)
  116. self.delegate?.getSharees(sharees: nil)
  117. }
  118. }
  119. }
  120. }
  121. protocol NCShareNetworkingDelegate {
  122. func readShareCompleted()
  123. func shareCompleted()
  124. func unShareCompleted()
  125. func updateShareWithError(idShare: Int)
  126. func getSharees(sharees: [NCCommunicationSharee]?)
  127. }