NCShareNetworking.swift 8.0 KB

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