FileProviderExtension+Actions.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.session.account, homeServerUrl: utilityFileSystem.getHomeServer(session: fileProviderData.shared.session)) else {
  29. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  30. }
  31. let directoryName = utilityFileSystem.createFileName(directoryName, serverUrl: tableDirectory.serverUrl, account: fileProviderData.shared.session.account)
  32. let serverUrlFileName = tableDirectory.serverUrl + "/" + directoryName
  33. NextcloudKit.shared.createFolder(serverUrlFileName: serverUrlFileName, account: fileProviderData.shared.session.account) { _, ocId, _, _, error in
  34. if error == .success {
  35. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: fileProviderData.shared.session.account) { _, files, _, error in
  36. if error == .success, let file = files?.first {
  37. let isDirectoryEncrypted = self.utilityFileSystem.isDirectoryE2EE(file: file)
  38. let metadata = self.database.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryEncrypted)
  39. self.database.addDirectory(e2eEncrypted: false, favorite: false, ocId: ocId!, fileId: metadata.fileId, etag: metadata.etag, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  40. self.database.addMetadata(metadata)
  41. guard let metadataInsert = self.database.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(.filenameCollision))
  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 {
  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. self.database.deleteDirectoryAndSubDirectory(serverUrl: dirForDelete, account: account)
  76. }
  77. self.database.deleteMetadataOcId(ocId)
  78. self.database.deleteLocalFileOcId(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.session.account, homeServerUrl: utilityFileSystem.getHomeServer(session: fileProviderData.shared.session)) else {
  94. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  95. }
  96. let serverUrlTo = tableDirectoryTo.serverUrl
  97. var fileNameTo = serverUrlTo + "/" + itemFrom.filename
  98. if let newName {
  99. fileNameTo = serverUrlTo + "/" + newName
  100. }
  101. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  102. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true, account: metadataFrom.account) { account, _, error in
  103. if error == .success {
  104. if metadataFrom.directory {
  105. self.database.deleteDirectoryAndSubDirectory(serverUrl: serverUrlFrom, account: account)
  106. self.database.renameDirectory(ocId: ocIdFrom, serverUrl: serverUrlTo)
  107. }
  108. self.database.moveMetadata(ocId: ocIdFrom, serverUrlTo: serverUrlTo)
  109. guard let metadata = self.database.getMetadataFromOcId(ocIdFrom) else {
  110. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  111. }
  112. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  113. completionHandler(item, nil)
  114. } else {
  115. completionHandler(nil, NSFileProviderError(.noSuchItem, userInfo: [NSLocalizedDescriptionKey: error.errorDescription, NSLocalizedFailureReasonErrorKey: ""]))
  116. }
  117. }
  118. }
  119. }
  120. override func renameItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toName itemName: String, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  121. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  122. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  123. }
  124. let fileNameFrom = metadata.fileNameView
  125. let fileNamePathFrom = metadata.serverUrl + "/" + fileNameFrom
  126. let fileNamePathTo = metadata.serverUrl + "/" + itemName
  127. let ocId = metadata.ocId
  128. NextcloudKit.shared.moveFileOrFolder(serverUrlFileNameSource: fileNamePathFrom, serverUrlFileNameDestination: fileNamePathTo, overwrite: false, account: metadata.account) { _, _, error in
  129. if error == .success {
  130. self.database.renameMetadata(fileNameNew: itemName, ocId: ocId)
  131. self.database.setMetadataServeUrlFileNameStatusNormal(ocId: ocId)
  132. guard let metadata = self.database.getMetadataFromOcId(ocId) else {
  133. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  134. }
  135. guard let parentItemIdentifier = self.providerUtility.getParentItemIdentifier(metadata: metadata) else {
  136. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  137. }
  138. let item = FileProviderItem(metadata: tableMetadata.init(value: metadata), parentItemIdentifier: parentItemIdentifier)
  139. completionHandler(item, nil)
  140. } else if error.errorCode == NCGlobal.shared.errorBadRequest {
  141. completionHandler(nil, NSFileProviderError(.noSuchItem, userInfo: [NSLocalizedDescriptionKey: error.errorDescription, NSLocalizedFailureReasonErrorKey: ""]))
  142. } else {
  143. completionHandler(nil, NSFileProviderError(.serverUnreachable))
  144. }
  145. }
  146. }
  147. override func setFavoriteRank(_ favoriteRank: NSNumber?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  148. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  149. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  150. }
  151. var favorite = false
  152. let ocId = metadata.ocId
  153. if favoriteRank == nil {
  154. fileProviderData.shared.listFavoriteIdentifierRank.removeValue(forKey: itemIdentifier.rawValue)
  155. } else {
  156. if fileProviderData.shared.listFavoriteIdentifierRank[itemIdentifier.rawValue] == nil {
  157. fileProviderData.shared.listFavoriteIdentifierRank[itemIdentifier.rawValue] = favoriteRank
  158. }
  159. favorite = true
  160. }
  161. if (favorite == true && metadata.favorite == false) || (favorite == false && metadata.favorite == true) {
  162. let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, session: fileProviderData.shared.session)
  163. NextcloudKit.shared.setFavorite(fileName: fileNamePath, favorite: favorite, account: metadata.account) { _, _, error in
  164. if error == .success {
  165. guard let metadata = self.database.getMetadataFromOcId(ocId) else {
  166. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  167. }
  168. // Change DB
  169. metadata.favorite = favorite
  170. self.database.addMetadata(metadata)
  171. /// SIGNAL
  172. let item = fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .workingSet)
  173. completionHandler(item, nil)
  174. } else {
  175. guard let metadata = self.database.getMetadataFromOcId(ocId) else {
  176. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  177. }
  178. // Errore, remove from listFavoriteIdentifierRank
  179. fileProviderData.shared.listFavoriteIdentifierRank.removeValue(forKey: itemIdentifier.rawValue)
  180. /// SIGNAL
  181. let item = fileProviderData.shared.signalEnumerator(ocId: metadata.ocId, type: .workingSet)
  182. completionHandler(item, NSFileProviderError(.serverUnreachable))
  183. }
  184. }
  185. }
  186. }
  187. override func setTagData(_ tagData: Data?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  188. guard let metadataForTag = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier) else {
  189. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  190. }
  191. let ocId = metadataForTag.ocId
  192. let account = metadataForTag.account
  193. self.database.addTag(ocId, tagIOS: tagData, account: account)
  194. /// SIGNAL WORKINGSET
  195. let item = fileProviderData.shared.signalEnumerator(ocId: ocId, type: .workingSet)
  196. completionHandler(item, nil)
  197. }
  198. override func setLastUsedDate(_ lastUsedDate: Date?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
  199. guard let metadata = providerUtility.getTableMetadataFromItemIdentifier(itemIdentifier),
  200. let parentItemIdentifier = providerUtility.getParentItemIdentifier(metadata: metadata) else {
  201. return completionHandler(nil, NSFileProviderError(.noSuchItem))
  202. }
  203. let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)
  204. completionHandler(item, nil)
  205. }
  206. }