NCNetworkingE2EE.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // NCNetworkingE2EE.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/05/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import Foundation
  22. import OpenSSL
  23. import NCCommunication
  24. import CFNetwork
  25. @objc class NCNetworkingE2EE: NSObject {
  26. @objc public static let shared: NCNetworkingE2EE = {
  27. let instance = NCNetworkingE2EE()
  28. return instance
  29. }()
  30. //MARK: - WebDav Create Folder
  31. func createFolder(fileName: String, serverUrl: String, account: String, url: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
  32. var fileNameFolder = CCUtility.removeForbiddenCharactersServer(fileName)!
  33. var fileNameFolderUrl = ""
  34. var fileNameIdentifier = ""
  35. var key: NSString?
  36. var initializationVector: NSString?
  37. fileNameFolder = NCUtility.sharedInstance.createFileName(fileNameFolder, serverUrl: serverUrl, account: account)
  38. if fileNameFolder.count == 0 {
  39. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": Int(0)], errorDescription: "", completion: completion)
  40. return
  41. }
  42. fileNameIdentifier = CCUtility.generateRandomIdentifier()
  43. fileNameFolderUrl = serverUrl + "/" + fileNameIdentifier
  44. self.lock(account: account, serverUrl: serverUrl) { (directory, e2eToken, errorCode, errorDescription) in
  45. if errorCode == 0 && e2eToken != nil && directory != nil {
  46. NCCommunication.shared.createFolder(fileNameFolderUrl, addCustomHeaders: ["e2e-token" : e2eToken!]) { (account, ocId, date, errorCode, errorDescription) in
  47. if errorCode == 0 {
  48. NCNetworking.shared.readFile(serverUrlFileName: fileNameFolderUrl, account: account) { (account, metadataFolder, errorCode, errorDescription) in
  49. if errorCode == 0 {
  50. // Add Metadata
  51. metadataFolder?.fileNameView = fileNameFolder
  52. metadataFolder?.e2eEncrypted = true
  53. NCManageDatabase.sharedInstance.addMetadata(metadataFolder!)
  54. // Add folder
  55. NCManageDatabase.sharedInstance.addDirectory(encrypted: true, favorite: metadataFolder!.favorite, ocId: metadataFolder!.ocId, fileId: metadataFolder!.fileId, etag: nil, permissions: metadataFolder!.permissions, serverUrl: fileNameFolderUrl, richWorkspace: metadataFolder!.richWorkspace, account: account)
  56. NCCommunication.shared.markE2EEFolder(fileId: metadataFolder!.fileId, delete: false) { (account, errorCode, errorDescription) in
  57. if errorCode == 0 {
  58. let object = tableE2eEncryption()
  59. NCEndToEndEncryption.sharedManager()?.encryptkey(&key, initializationVector: &initializationVector)
  60. object.account = account
  61. object.authenticationTag = nil
  62. object.fileName = fileNameFolder
  63. object.fileNameIdentifier = fileNameIdentifier
  64. object.fileNamePath = ""
  65. object.key = key! as String
  66. object.initializationVector = initializationVector! as String
  67. if let result = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) {
  68. object.metadataKey = result.metadataKey
  69. object.metadataKeyIndex = result.metadataKeyIndex
  70. } else {
  71. object.metadataKey = (NCEndToEndEncryption.sharedManager()?.generateKey(16)?.base64EncodedString(options: []))! as String // AES_KEY_128_LENGTH
  72. object.metadataKeyIndex = 0
  73. }
  74. object.mimeType = "httpd/unix-directory"
  75. object.serverUrl = serverUrl
  76. if let e2eeApiVersion = NCManageDatabase.sharedInstance.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion) {
  77. object.version = Int(e2eeApiVersion) ?? 1
  78. } else {
  79. object.version = 1
  80. }
  81. let _ = NCManageDatabase.sharedInstance.addE2eEncryption(object)
  82. self.sendE2EMetadata(account: account, serverUrl: serverUrl, fileNameRename: nil, fileNameNewRename: nil, deleteE2eEncryption: nil, url: url) { (e2eToken, errorCode, errorDescription) in
  83. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  84. }
  85. } else {
  86. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  87. }
  88. }
  89. } else {
  90. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  91. }
  92. }
  93. } else {
  94. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  95. }
  96. }
  97. } else {
  98. self.NotificationPost(name: k_notificationCenter_createFolder, serverUrl: serverUrl, userInfo: ["fileName": fileName, "serverUrl": serverUrl, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  99. }
  100. }
  101. }
  102. //MARK: - WebDav Delete
  103. func deleteMetadata(_ metadata: tableMetadata, url: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
  104. self.lock(account:metadata.account, serverUrl: metadata.serverUrl) { (directory, e2eToken, errorCode, errorDescription) in
  105. if errorCode == 0 && e2eToken != nil && directory != nil {
  106. let deleteE2eEncryption = NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, metadata.serverUrl, metadata.fileName)
  107. NCNetworking.shared.deleteMetadataPlain(metadata, addCustomHeaders: ["e2e-token" :e2eToken!]) { (errorCode, errorDescription) in
  108. let webDavRoot = NCManageDatabase.sharedInstance.getCapabilitiesServerString(account: metadata.account, elements: NCElementsJSON.shared.capabilitiesWebDavRoot) ?? "remote.php/webdav"
  109. let home = url + "/" + webDavRoot
  110. if metadata.serverUrl != home {
  111. self.sendE2EMetadata(account: metadata.account, serverUrl: metadata.serverUrl, fileNameRename: nil, fileNameNewRename: nil, deleteE2eEncryption: deleteE2eEncryption, url: url) { (e2eToken, errorCode, errorDescription) in
  112. self.NotificationPost(name: k_notificationCenter_deleteFile, serverUrl: metadata.serverUrl, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  113. }
  114. } else {
  115. self.NotificationPost(name: k_notificationCenter_deleteFile, serverUrl: metadata.serverUrl, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  116. }
  117. }
  118. } else {
  119. self.NotificationPost(name: k_notificationCenter_deleteFile, serverUrl: metadata.serverUrl, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  120. }
  121. }
  122. }
  123. //MARK: - WebDav Rename
  124. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String, url: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String?)->()) {
  125. // verify if exists the new fileName
  126. if NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, fileNameNew)) != nil {
  127. self.NotificationPost(name: k_notificationCenter_renameFile, serverUrl: metadata.serverUrl, userInfo: ["metadata": metadata, "errorCode": Int(k_CCErrorInternalError)], errorDescription: "_file_already_exists_", completion: completion)
  128. } else {
  129. self.sendE2EMetadata(account: metadata.account, serverUrl: metadata.serverUrl, fileNameRename: metadata.fileName, fileNameNewRename: fileNameNew, deleteE2eEncryption: nil, url: url) { (e2eToken, errorCode, errorDescription) in
  130. if errorCode == 0 {
  131. NCManageDatabase.sharedInstance.setMetadataFileNameView(serverUrl: metadata.serverUrl, fileName: metadata.fileName, newFileNameView: fileNameNew, account: metadata.account)
  132. // Move file system
  133. let atPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileNameView
  134. let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + fileNameNew
  135. do {
  136. try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
  137. } catch { }
  138. let atPathIcon = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  139. let toPathIcon = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: fileNameNew)!
  140. do {
  141. try FileManager.default.moveItem(atPath: atPathIcon, toPath: toPathIcon)
  142. } catch { }
  143. }
  144. self.NotificationPost(name: k_notificationCenter_deleteFile, serverUrl: metadata.serverUrl, userInfo: ["metadata": metadata, "errorCode": errorCode], errorDescription: errorDescription, completion: completion)
  145. }
  146. }
  147. }
  148. //MARK: - Upload
  149. func upload(metadataForUpload: tableMetadata, account: tableAccount) {
  150. let object = tableE2eEncryption()
  151. var key: NSString?, initializationVector: NSString?, authenticationTag: NSString?
  152. var e2eMetadataKey = ""
  153. var e2eMetadataKeyIndex = 0
  154. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadataForUpload.ocId, fileNameView: metadataForUpload.fileNameView)!
  155. let serverUrlFileName = metadataForUpload.serverUrl + "/" + metadataForUpload.fileName
  156. if NCEndToEndEncryption.sharedManager()?.encryptFileName(metadataForUpload.fileNameView, fileNameIdentifier: metadataForUpload.fileName, directory: CCUtility.getDirectoryProviderStorageOcId(metadataForUpload.ocId), key: &key, initializationVector: &initializationVector, authenticationTag: &authenticationTag) == false {
  157. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadedFile), object: nil, userInfo: ["metadata":metadataForUpload, "errorCode":k_CCErrorInternalError, "errorDescription":"_e2e_error_create_encrypted_"])
  158. return
  159. }
  160. if let object = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadataForUpload.account, metadataForUpload.serverUrl)) {
  161. e2eMetadataKey = object.metadataKey
  162. e2eMetadataKeyIndex = object.metadataKeyIndex
  163. } else {
  164. let key = NCEndToEndEncryption.sharedManager()?.generateKey(16) as NSData?
  165. e2eMetadataKey = key!.base64EncodedString()
  166. }
  167. object.account = metadataForUpload.account
  168. object.authenticationTag = authenticationTag as String?
  169. object.fileName = metadataForUpload.fileNameView
  170. object.fileNameIdentifier = metadataForUpload.fileName
  171. object.fileNamePath = fileNameLocalPath
  172. object.key = key! as String
  173. object.initializationVector = initializationVector! as String
  174. object.metadataKey = e2eMetadataKey
  175. object.metadataKeyIndex = e2eMetadataKeyIndex
  176. object.mimeType = metadataForUpload.contentType
  177. object.serverUrl = metadataForUpload.serverUrl
  178. let e2eeApiVersion = NCManageDatabase.sharedInstance.getCapabilitiesServerString(account: metadataForUpload.account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion)!
  179. object.version = Int(e2eeApiVersion) ?? 1
  180. if NCManageDatabase.sharedInstance.addE2eEncryption(object) == false {
  181. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadedFile), object: nil, userInfo: ["metadata":metadataForUpload, "errorCode":k_CCErrorInternalError, "errorDescription":"_e2e_error_create_encrypted_"])
  182. return
  183. }
  184. NCNetworkingE2EE.shared.sendE2EMetadata(account: metadataForUpload.account, serverUrl: metadataForUpload.serverUrl, fileNameRename: nil, fileNameNewRename: nil, deleteE2eEncryption: nil, url: account.url, upload: true) { (e2eToken, errorCode, errorDescription) in
  185. if errorCode == 0 && e2eToken != nil {
  186. NCCommunication.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, dateCreationFile: metadataForUpload.date as Date, dateModificationFile: metadataForUpload.date as Date, addCustomHeaders: ["e2e-token":e2eToken!], taskHandler: { (task) in
  187. NCManageDatabase.sharedInstance.setMetadataSession(metadataForUpload.session, sessionError: "", sessionSelector: nil, sessionTaskIdentifier: task.taskIdentifier, status: Int(k_metadataStatusUploading), predicate: NSPredicate(format: "ocId == %@", metadataForUpload.ocId))
  188. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadFileStart), object: nil, userInfo: ["ocId":metadataForUpload.ocId, "task":task, "serverUrl":metadataForUpload.serverUrl, "account": metadataForUpload.account])
  189. print("[LOG] Upload file " + metadataForUpload.fileNameView)
  190. }, progressHandler: { (progress) in
  191. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_progressTask), object: nil, userInfo: ["account":metadataForUpload.account, "ocId":metadataForUpload.ocId, "serverUrl":metadataForUpload.serverUrl, "status":NSNumber(value: k_metadataStatusInUpload), "progress":NSNumber(value: progress.fractionCompleted), "totalBytes":NSNumber(value: progress.totalUnitCount), "totalBytesExpected":NSNumber(value: progress.completedUnitCount)])
  192. }) { (account, ocId, etag, date, size, errorCode, errorDescription) in
  193. if (errorCode == 0 && date != nil && etag != nil && ocId != nil) {
  194. CCUtility.moveFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadataForUpload.ocId), toPath: CCUtility.getDirectoryProviderStorageOcId(ocId))
  195. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadataForUpload.ocId))
  196. metadataForUpload.date = date!
  197. metadataForUpload.etag = etag!
  198. metadataForUpload.ocId = ocId!
  199. metadataForUpload.session = ""
  200. metadataForUpload.sessionError = ""
  201. metadataForUpload.sessionTaskIdentifier = Int(k_taskIdentifierDone)
  202. metadataForUpload.status = Int(k_metadataStatusNormal)
  203. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  204. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadataForUpload)
  205. CCGraphics.createNewImage(from: metadataForUpload.fileNameView, ocId: metadataForUpload.ocId, filterGrayScale: false, typeFile: metadataForUpload.typeFile, writeImage: true)
  206. print("[LOG] Insert new upload : " + metadataForUpload.fileNameView)
  207. } else if errorCode == Int(CFNetworkErrors.cfurlErrorCancelled.rawValue) {
  208. if (metadataForUpload.status == k_metadataStatusUploadForcedStart) {
  209. metadataForUpload.sessionError = ""
  210. metadataForUpload.sessionTaskIdentifier = 0
  211. metadataForUpload.status = Int(k_metadataStatusInUpload)
  212. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  213. } else {
  214. CCUtility.removeFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadataForUpload.ocId))
  215. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadataForUpload.ocId))
  216. }
  217. } else {
  218. if errorCode == 401 || errorCode == 403 {
  219. NCNetworkingCheckRemoteUser.shared.checkRemoteUser(account: metadataForUpload.account)
  220. } else if errorCode == Int(CFNetworkErrors.cfurlErrorServerCertificateUntrusted.rawValue) {
  221. CCUtility.setCertificateError(metadataForUpload.account, error: true)
  222. }
  223. NCManageDatabase.sharedInstance.setMetadataSession(nil, sessionError: errorDescription, sessionSelector: nil, sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusUploadError), predicate: NSPredicate(format: "ocId == %@", metadataForUpload.ocId))
  224. }
  225. NCNetworkingE2EE.shared.unlock(account: metadataForUpload.account, serverUrl: metadataForUpload.serverUrl) { (_, _, _, _) in }
  226. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadedFile), object: nil, userInfo: ["metadata":metadataForUpload, "errorCode":errorCode, "errorDescription":errorDescription ?? ""])
  227. }
  228. } else {
  229. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadedFile), object: nil, userInfo: ["metadata":metadataForUpload, "errorCode":errorCode, "errorDescription":errorDescription ?? ""])
  230. }
  231. }
  232. }
  233. //MARK: - E2EE
  234. @objc func lock(account:String, serverUrl: String, completion: @escaping (_ direcrtory: tableDirectory?, _ e2eToken: String?, _ errorCode: Int, _ errorDescription: String?)->()) {
  235. var e2eToken: String?
  236. guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  237. completion(nil, nil, 0, "")
  238. return
  239. }
  240. if let tableLock = NCManageDatabase.sharedInstance.getE2ETokenLock(serverUrl: serverUrl) {
  241. e2eToken = tableLock.e2eToken
  242. }
  243. NCCommunication.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, delete: false) { (account, e2eToken, errorCode, errorDescription) in
  244. if errorCode == 0 && e2eToken != nil {
  245. NCManageDatabase.sharedInstance.setE2ETokenLock(serverUrl: serverUrl, fileId: directory.fileId, e2eToken: e2eToken!)
  246. }
  247. completion(directory, e2eToken, errorCode, errorDescription)
  248. }
  249. }
  250. @objc func unlock(account:String, serverUrl: String, completion: @escaping (_ direcrtory: tableDirectory?, _ e2eToken: String?, _ errorCode: Int, _ errorDescription: String?)->()) {
  251. var e2eToken: String?
  252. guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  253. completion(nil, nil, 0, "")
  254. return
  255. }
  256. if let tableLock = NCManageDatabase.sharedInstance.getE2ETokenLock(serverUrl: serverUrl) {
  257. e2eToken = tableLock.e2eToken
  258. }
  259. NCCommunication.shared.lockE2EEFolder(fileId: directory.fileId, e2eToken: e2eToken, delete: true) { (account, e2eToken, errorCode, errorDescription) in
  260. if errorCode == 0 {
  261. NCManageDatabase.sharedInstance.deteleE2ETokenLock(serverUrl: serverUrl)
  262. }
  263. completion(directory, e2eToken, errorCode, errorDescription)
  264. }
  265. }
  266. @objc func sendE2EMetadata(account: String, serverUrl: String, fileNameRename: String?, fileNameNewRename: String?, deleteE2eEncryption : NSPredicate?, url: String, upload: Bool = false, completion: @escaping (_ e2eToken: String?, _ errorCode: Int, _ errorDescription: String?)->()) {
  267. self.lock(account: account, serverUrl: serverUrl) { (directory, e2eToken, errorCode, errorDescription) in
  268. if errorCode == 0 && e2eToken != nil && directory != nil {
  269. NCCommunication.shared.getE2EEMetadata(fileId: directory!.fileId, e2eToken: e2eToken) { (account, e2eMetadata, errorCode, errorDescription) in
  270. var method = "POST"
  271. var e2eMetadataNew: String?
  272. if errorCode == 0 && e2eMetadata != nil {
  273. if !NCEndToEndMetadata.sharedInstance.decoderMetadata(e2eMetadata!, privateKey: CCUtility.getEndToEndPrivateKey(account), serverUrl: serverUrl, account: account, url: url) {
  274. completion(e2eToken, Int(k_CCErrorInternalError), NSLocalizedString("_e2e_error_encode_metadata_", comment: ""))
  275. return
  276. }
  277. method = "PUT"
  278. }
  279. // Rename
  280. if (fileNameRename != nil && fileNameNewRename != nil) {
  281. NCManageDatabase.sharedInstance.renameFileE2eEncryption(serverUrl: serverUrl, fileNameIdentifier: fileNameRename!, newFileName: fileNameNewRename!, newFileNamePath: CCUtility.returnFileNamePath(fromFileName: fileNameNewRename!, serverUrl: serverUrl, activeUrl: url))
  282. }
  283. // Delete
  284. if deleteE2eEncryption != nil {
  285. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: deleteE2eEncryption!)
  286. }
  287. // Rebuild metadata for send it
  288. let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryptions(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl))
  289. if tableE2eEncryption != nil {
  290. e2eMetadataNew = NCEndToEndMetadata.sharedInstance.encoderMetadata(tableE2eEncryption!, privateKey: CCUtility.getEndToEndPrivateKey(account), serverUrl: serverUrl)
  291. }
  292. NCCommunication.shared.putE2EEMetadata(fileId: directory!.fileId, e2eToken: e2eToken!, e2eMetadata: e2eMetadataNew, method: method) { (account, e2eMetadata, errorCode, errorDescription) in
  293. if upload {
  294. completion(e2eToken, errorCode, errorDescription)
  295. } else {
  296. self.unlock(account: account, serverUrl: serverUrl) { (_, e2eToken, _, _) in
  297. completion(e2eToken, errorCode, errorDescription)
  298. }
  299. }
  300. }
  301. }
  302. } else {
  303. completion(e2eToken, errorCode, errorDescription)
  304. }
  305. }
  306. }
  307. //MARK: - Notification Post
  308. private func NotificationPost(name: String, serverUrl: String, userInfo: [AnyHashable : Any], errorDescription: Any?, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
  309. var userInfo = userInfo
  310. DispatchQueue.main.async {
  311. // unlock
  312. if let tableLock = NCManageDatabase.sharedInstance.getE2ETokenLock(serverUrl: serverUrl) {
  313. NCCommunication.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, delete: true) { (_, _, _, _) in }
  314. }
  315. if errorDescription == nil { userInfo["errorDescription"] = "" }
  316. else { userInfo["errorDescription"] = NSLocalizedString(errorDescription as! String, comment: "") }
  317. NotificationCenter.default.post(name: Notification.Name.init(rawValue: name), object: nil, userInfo: userInfo)
  318. completion(userInfo["errorCode"] as! Int, userInfo["errorDescription"] as! String)
  319. }
  320. }
  321. }