FileProviderExtension+Actions.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // FileProviderExtension+Actions.swift
  3. // PickerFileProvider
  4. //
  5. // Created by Marino Faggiana on 28/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 FileProvider
  25. import NextcloudKit
  26. extension FileProviderExtension {
  27. override func createDirectory(withName directoryName: String, inParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  28. guard let tableDirectory = providerUtility.getTableDirectoryFromParentItemIdentifier(parentItemIdentifier, account: fileProviderData.shared.account, homeServerUrl: fileProviderData.shared.homeServerUrl) else {
  29. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  30. }
  31. let directoryName = utilityFileSystem.createFileName(directoryName, serverUrl: tableDirectory.serverUrl, account: fileProviderData.shared.account)
  32. let serverUrlFileName = tableDirectory.serverUrl + "/" + directoryName
  33. NextcloudKit.shared.createFolder(serverUrlFileName: serverUrlFileName, account: fileProviderData.shared.account) { _, ocId, _, error in
  34. if error == .success {
  35. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: fileProviderData.shared.account) { _, files, _, error in
  36. if error == .success, let file = files.first {
  37. let isDirectoryEncrypted = self.utilityFileSystem.isDirectoryE2EE(file: file)
  38. let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryEncrypted)
  39. NCManageDatabase.shared.addDirectory(e2eEncrypted: false, favorite: false, ocId: ocId!, fileId: metadata.fileId, etag: metadata.etag, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  40. NCManageDatabase.shared.addMetadata(metadata)
  41. guard let metadataInsert = NCManageDatabase.shared.getMetadataFromOcId(ocId!),
  42. let parentItemIdentifier = self.providerUtility.getParentItemIdentifier(metadata: metadataInsert) else {
  43. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  44. }
  45. let item = FileProviderItem(metadata: metadataInsert, parentItemIdentifier: parentItemIdentifier)
  46. completionHandler(item, nil)
  47. } else {
  48. completionHandler(nil, NSFileProviderError(.serverUnreachable))
  49. }
  50. }
  51. } else {
  52. completionHandler(nil, NSFileProviderError(.serverUnreachable))
  53. }
  54. }
  55. }
  56. override func deleteItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (Error?) -> Void) {
  57. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  58. return completionHandler(NSFileProviderError(.noSuchItem))
  59. }
  60. let ocId = metadata.ocId
  61. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  62. let isDirectory = metadata.directory
  63. let serverUrl = metadata.serverUrl
  64. let fileName = metadata.fileName
  65. NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: metadata.account) { account, error in
  66. if error == .success { // || error == kOCErrorServerPathNotFound {
  67. let fileNamePath = self.utilityFileSystem.getDirectoryProviderStorageOcId(itemIdentifier.rawValue)
  68. do {
  69. try self.providerUtility.fileManager.removeItem(atPath: fileNamePath)
  70. } catch let error {
  71. print("error: \(error)")
  72. }
  73. if isDirectory {
  74. let dirForDelete = self.utilityFileSystem.stringAppendServerUrl(serverUrl, addFileName: fileName)
  75. NCManageDatabase.shared.deleteDirectoryAndSubDirectory(serverUrl: dirForDelete, account: account)
  76. }
  77. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", ocId))
  78. NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))
  79. completionHandler(nil)
  80. } else {
  81. completionHandler(NSFileProviderError(.serverUnreachable))
  82. }
  83. }
  84. }
  85. override func reparentItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toParentItemWithIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, newName: String?, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  86. guard let itemFrom = try? item(for: itemIdentifier),
  87. let metadataFrom = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  88. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  89. }
  90. let ocIdFrom = metadataFrom.ocId
  91. let serverUrlFrom = metadataFrom.serverUrl
  92. let fileNameFrom = serverUrlFrom + "/" + itemFrom.filename
  93. guard let tableDirectoryTo = providerUtility.getTableDirectoryFromParentItemIdentifier(parentItemIdentifier, account: fileProviderData.shared.account, homeServerUrl: fileProviderData.shared.homeServerUrl) else {
  94. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  95. }
  96. let serverUrlTo = tableDirectoryTo.serverUrl
  97. let fileNameTo = serverUrlTo + "/" + itemFrom.filename
  98. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: false, account: metadataFrom.account) { account, error in
  99. if error == .success {
  100. if metadataFrom.directory {
  101. NCManageDatabase.shared.deleteDirectoryAndSubDirectory(serverUrl: serverUrlFrom, account: account)
  102. NCManageDatabase.shared.renameDirectory(ocId: ocIdFrom, serverUrl: serverUrlTo)
  103. }
  104. NCManageDatabase.shared.moveMetadata(ocId: ocIdFrom, serverUrlTo: serverUrlTo)
  105. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocIdFrom) else {
  106. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  107. }
  108. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  109. completionHandler(item, nil)
  110. } else {
  111. completionHandler(nil, NSFileProviderError(.serverUnreachable))
  112. }
  113. }
  114. }
  115. override func renameItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toName itemName: String, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  116. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier),
  117. let directoryTable = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) else {
  118. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  119. }
  120. let fileNameFrom = metadata.fileNameView
  121. let fileNamePathFrom = metadata.serverUrl + "/" + fileNameFrom
  122. let fileNamePathTo = metadata.serverUrl + "/" + itemName
  123. let ocId = metadata.ocId
  124. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNamePathFrom, serverUrlFileNameDestination: fileNamePathTo, overwrite: false, account: metadata.account) { account, error in
  125. if error == .success {
  126. // Rename metadata
  127. NCManageDatabase.shared.renameMetadata(fileNameTo: itemName, ocId: ocId)
  128. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else {
  129. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  130. }
  131. if metadata.directory {
  132. NCManageDatabase.shared.setDirectory(serverUrl: fileNamePathFrom, serverUrlTo: fileNamePathTo, encrypted: directoryTable.e2eEncrypted, account: account)
  133. } else {
  134. let itemIdentifier = self.providerUtility.getItemIdentifier(metadata: metadata)
  135. self.providerUtility.moveFile(self.utilityFileSystem.getDirectoryProviderStorageOcId(itemIdentifier.rawValue, fileNameView: fileNameFrom), toPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(itemIdentifier.rawValue, fileNameView: itemName))
  136. self.providerUtility.moveFile(self.utilityFileSystem.getDirectoryProviderStoragePreviewOcId(itemIdentifier.rawValue, etag: metadata.etag), toPath: self.utilityFileSystem.getDirectoryProviderStoragePreviewOcId(itemIdentifier.rawValue, etag: metadata.etag))
  137. self.providerUtility.moveFile(self.utilityFileSystem.getDirectoryProviderStorageIconOcId(itemIdentifier.rawValue, etag: metadata.etag), toPath: self.utilityFileSystem.getDirectoryProviderStorageIconOcId(itemIdentifier.rawValue, etag: metadata.etag))
  138. NCManageDatabase.shared.setLocalFile(ocId: ocId, fileName: itemName)
  139. }
  140. guard let parentItemIdentifier = self.providerUtility.getParentItemIdentifier(metadata: metadata) else {
  141. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  142. }
  143. let item = FileProviderItem(metadata: tableMetadata.init(value: metadata), parentItemIdentifier: parentItemIdentifier)
  144. completionHandler(item, nil)
  145. } else {
  146. completionHandler(nil, NSFileProviderError(.serverUnreachable))
  147. }
  148. }
  149. }
  150. override func setFavoriteRank(_ favoriteRank: NSNumber?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  151. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  152. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  153. }
  154. var favorite = false
  155. let ocId = metadata.ocId
  156. if favoriteRank == nil {
  157. fileProviderData.shared.listFavoriteIdentifierRank.removeValue(forKey: itemIdentifier.rawValue)
  158. } else {
  159. if fileProviderData.shared.listFavoriteIdentifierRank[itemIdentifier.rawValue] == nil {
  160. fileProviderData.shared.listFavoriteIdentifierRank[itemIdentifier.rawValue] = favoriteRank
  161. }
  162. favorite = true
  163. }
  164. if (favorite == true && metadata.favorite == false) || (favorite == false && metadata.favorite == true) {
  165. let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  166. NextcloudKit.shared.setFavorite(fileName: fileNamePath, favorite: favorite, account: metadata.account) { _, error in
  167. if error == .success {
  168. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else {
  169. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  170. }
  171. // Change DB
  172. metadata.favorite = favorite
  173. NCManageDatabase.shared.addMetadata(metadata)
  174. /// SIGNAL
  175. let item = fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .workingSet)
  176. completionHandler(item, nil)
  177. } else {
  178. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else {
  179. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  180. }
  181. // Errore, remove from listFavoriteIdentifierRank
  182. fileProviderData.shared.listFavoriteIdentifierRank.removeValue(forKey: itemIdentifier.rawValue)
  183. /// SIGNAL
  184. let item = fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .workingSet)
  185. completionHandler(item, NSFileProviderError(.serverUnreachable))
  186. }
  187. }
  188. }
  189. }
  190. override func setTagData(_ tagData: Data?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  191. guard let metadataForTag = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  192. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  193. }
  194. let ocId = metadataForTag.ocId
  195. let account = metadataForTag.account
  196. NCManageDatabase.shared.addTag(ocId, tagIOS: tagData, account: account)
  197. /// SIGNAL WORKINGSET
  198. let item = fileProviderData.shared.signalEnumerator(ocId: ocId, type: .workingSet)
  199. completionHandler(item, nil)
  200. }
  201. override func setLastUsedDate(_ lastUsedDate: Date?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  202. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier),
  203. let parentItemIdentifier = providerUtility.getParentItemIdentifier(metadata: metadata) else {
  204. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  205. }
  206. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  207. completionHandler(item, nil)
  208. }
  209. }