NCShareNetworking.swift 7.9 KB

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