NCShareNetworking.swift 8.1 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 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(shareWith: String, shareType: Int, password: String?, 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. var permission: Int = NCManageDatabase.shared.getCapabilitiesServerInt(account: metadata.account, elements: ["ocs", "data", "capabilities", "files_sharing", "default_permissions"])
  74. if permission <= 0 {
  75. permission = metadata.directory ? NCGlobal.shared.permissionMaxFolderShare : NCGlobal.shared.permissionMaxFileShare
  76. }
  77. NCCommunication.shared.createShare(path: filenamePath, shareType: shareType, shareWith: shareWith, password: password, permissions: permission) { (account, share, errorCode, errorDescription) in
  78. NCUtility.shared.stopActivityIndicator()
  79. if errorCode == 0 && share != nil {
  80. NCManageDatabase.shared.addShare(urlBase: self.urlBase, account: self.metadata.account, shares: [share!])
  81. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: self.metadata.account)
  82. } else {
  83. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  84. }
  85. self.delegate?.shareCompleted()
  86. }
  87. }
  88. func unShare(idShare: Int) {
  89. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  90. NCCommunication.shared.deleteShare(idShare: idShare) { account, errorCode, errorDescription in
  91. NCUtility.shared.stopActivityIndicator()
  92. if errorCode == 0 {
  93. NCManageDatabase.shared.deleteTableShare(account: account, idShare: idShare)
  94. self.delegate?.unShareCompleted()
  95. } else {
  96. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  97. }
  98. }
  99. }
  100. func updateShare(idShare: Int, password: String?, permissions: Int, note: String?, label: String?, expirationDate: String?, hideDownload: Bool) {
  101. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  102. NCCommunication.shared.updateShare(idShare: idShare, password: password, expireDate: expirationDate, permissions: permissions, note: note, label: label, hideDownload: hideDownload) { account, share, errorCode, errorDescription in
  103. NCUtility.shared.stopActivityIndicator()
  104. if errorCode == 0 && share != nil {
  105. NCManageDatabase.shared.addShare(urlBase: self.urlBase, account: self.metadata.account, shares: [share!])
  106. self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: self.metadata.account)
  107. self.delegate?.readShareCompleted()
  108. } else {
  109. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  110. self.delegate?.updateShareWithError(idShare: idShare)
  111. }
  112. }
  113. }
  114. func getSharees(searchString: String) {
  115. NCUtility.shared.startActivityIndicator(backgroundView: view, blurEffect: false)
  116. NCCommunication.shared.searchSharees(search: searchString) { _, sharees, errorCode, errorDescription in
  117. NCUtility.shared.stopActivityIndicator()
  118. if errorCode == 0 {
  119. self.delegate?.getSharees(sharees: sharees)
  120. } else {
  121. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  122. self.delegate?.getSharees(sharees: nil)
  123. }
  124. }
  125. }
  126. }
  127. protocol NCShareNetworkingDelegate: AnyObject {
  128. func readShareCompleted()
  129. func shareCompleted()
  130. func unShareCompleted()
  131. func updateShareWithError(idShare: Int)
  132. func getSharees(sharees: [NCCommunicationSharee]?)
  133. }