FileProviderData.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // FileProviderData.swift
  3. // Files
  4. //
  5. // Created by Marino Faggiana on 27/05/18.
  6. // Copyright © 2018 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. //
  23. import UIKit
  24. import NextcloudKit
  25. class fileProviderData: NSObject {
  26. static let shared: fileProviderData = {
  27. let instance = fileProviderData()
  28. return instance
  29. }()
  30. var domain: NSFileProviderDomain?
  31. var fileProviderManager: NSFileProviderManager = NSFileProviderManager.default
  32. let utilityFileSystem = NCUtilityFileSystem()
  33. let database = NCManageDatabase.shared
  34. var listFavoriteIdentifierRank: [String: NSNumber] = [:]
  35. var fileProviderSignalDeleteContainerItemIdentifier: [NSFileProviderItemIdentifier: NSFileProviderItemIdentifier] = [:]
  36. var fileProviderSignalUpdateContainerItem: [NSFileProviderItemIdentifier: FileProviderItem] = [:]
  37. var fileProviderSignalDeleteWorkingSetItemIdentifier: [NSFileProviderItemIdentifier: NSFileProviderItemIdentifier] = [:]
  38. var fileProviderSignalUpdateWorkingSetItem: [NSFileProviderItemIdentifier: FileProviderItem] = [:]
  39. private var account: String = ""
  40. var session: NCSession.Session {
  41. if !account.isEmpty,
  42. let tableAccount = self.database.getTableAccount(account: account) {
  43. return NCSession.Session(account: tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userId: tableAccount.userId)
  44. } else if let activeTableAccount = self.database.getActiveTableAccount() {
  45. self.account = activeTableAccount.account
  46. return NCSession.Session(account: activeTableAccount.account, urlBase: activeTableAccount.urlBase, user: activeTableAccount.user, userId: activeTableAccount.userId)
  47. } else {
  48. return NCSession.Session(account: "", urlBase: "", user: "", userId: "")
  49. }
  50. }
  51. enum FileProviderError: Error {
  52. case downloadError
  53. case uploadError
  54. }
  55. enum TypeSignal: String {
  56. case delete
  57. case update
  58. case workingSet
  59. }
  60. struct UploadMetadata {
  61. var id: String
  62. var metadata: tableMetadata
  63. var task: URLSessionUploadTask?
  64. }
  65. var uploadMetadata: [UploadMetadata] = []
  66. // MARK: -
  67. func setupAccount(domain: NSFileProviderDomain?, providerExtension: NSFileProviderExtension) -> tableAccount? {
  68. self.domain = domain
  69. if let domain, let fileProviderManager = NSFileProviderManager(for: domain) {
  70. self.fileProviderManager = fileProviderManager
  71. }
  72. // LOG
  73. NextcloudKit.shared.nkCommonInstance.pathLog = utilityFileSystem.directoryGroup
  74. let levelLog = NCKeychain().logLevel
  75. NextcloudKit.shared.nkCommonInstance.levelLog = levelLog
  76. let version = NSString(format: NCBrandOptions.shared.textCopyrightNextcloudiOS as NSString, NCUtility().getVersionApp()) as String
  77. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Start File Provider session with level \(levelLog) " + version + " (File Provider Extension)")
  78. var tblAccount = self.database.getActiveTableAccount()
  79. if let domain {
  80. for tableAccount in self.database.getAllTableAccount() {
  81. guard let urlBase = NSURL(string: tableAccount.urlBase) else { continue }
  82. guard let host = urlBase.host else { continue }
  83. let accountDomain = tableAccount.userId + " (" + host + ")"
  84. if accountDomain == domain.identifier.rawValue {
  85. let account = "\(tableAccount.user) \(tableAccount.urlBase)"
  86. tblAccount = self.database.getTableAccount(account: account)
  87. break
  88. }
  89. }
  90. }
  91. guard let tblAccount else { return nil }
  92. self.account = tblAccount.account
  93. /// NextcloudKit Session
  94. NextcloudKit.shared.setup(delegate: NCNetworking.shared)
  95. NextcloudKit.shared.appendSession(account: tblAccount.account,
  96. urlBase: tblAccount.urlBase,
  97. user: tblAccount.user,
  98. userId: tblAccount.userId,
  99. password: NCKeychain().getPassword(account: tblAccount.account),
  100. userAgent: userAgent,
  101. nextcloudVersion: NCCapabilities.shared.getCapabilities(account: tblAccount.account).capabilityServerVersionMajor,
  102. groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
  103. NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
  104. return tableAccount(value: tblAccount)
  105. }
  106. // MARK: -
  107. @discardableResult
  108. func signalEnumerator(ocId: String, type: TypeSignal) -> FileProviderItem? {
  109. guard let metadata = self.database.getMetadataFromOcId(ocId),
  110. let parentItemIdentifier = fileProviderUtility().getParentItemIdentifier(metadata: metadata) else {
  111. return nil
  112. }
  113. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  114. if type == .delete {
  115. fileProviderData.shared.fileProviderSignalDeleteContainerItemIdentifier[item.itemIdentifier] = item.itemIdentifier
  116. fileProviderData.shared.fileProviderSignalDeleteWorkingSetItemIdentifier[item.itemIdentifier] = item.itemIdentifier
  117. }
  118. if type == .update {
  119. fileProviderData.shared.fileProviderSignalUpdateContainerItem[item.itemIdentifier] = item
  120. fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  121. }
  122. if type == .workingSet {
  123. fileProviderData.shared.fileProviderSignalUpdateWorkingSetItem[item.itemIdentifier] = item
  124. }
  125. if type == .delete || type == .update {
  126. fileProviderManager.signalEnumerator(for: parentItemIdentifier) { _ in }
  127. }
  128. fileProviderManager.signalEnumerator(for: .workingSet) { _ in }
  129. return item
  130. }
  131. // MARK: -
  132. func appendUploadMetadata(id: String, metadata: tableMetadata, task: URLSessionUploadTask?) {
  133. if let index = uploadMetadata.firstIndex(where: { $0.id == id }) {
  134. uploadMetadata.remove(at: index)
  135. }
  136. uploadMetadata.append(UploadMetadata(id: id, metadata: metadata, task: task))
  137. }
  138. func getUploadMetadata(id: String) -> UploadMetadata? {
  139. return uploadMetadata.filter({ $0.id == id }).first
  140. }
  141. }