NCNetworkingE2EECreateFolder.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // NCNetworkingE2EECreateFolder.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/11/22.
  6. // Copyright © 2022 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 UIKit
  22. import NextcloudKit
  23. import CFNetwork
  24. import Alamofire
  25. import Foundation
  26. class NCNetworkingE2EECreateFolder: NSObject {
  27. let networkingE2EE = NCNetworkingE2EE()
  28. let utilityFileSystem = NCUtilityFileSystem()
  29. let utility = NCUtility()
  30. let database = NCManageDatabase.shared
  31. func createFolder(fileName: String, serverUrl: String, withPush: Bool, sceneIdentifier: String?, session: NCSession.Session) async -> NKError {
  32. var fileNameFolder = utility.removeForbiddenCharacters(fileName)
  33. if fileName != fileNameFolder {
  34. let errorDescription = String(format: NSLocalizedString("_forbidden_characters_", comment: ""), NCGlobal.shared.forbiddenCharacters.joined(separator: " "))
  35. let error = NKError(errorCode: NCGlobal.shared.errorConflict, errorDescription: errorDescription)
  36. return error
  37. }
  38. let fileNameIdentifier = networkingE2EE.generateRandomIdentifier()
  39. let serverUrlFileName = serverUrl + "/" + fileNameIdentifier
  40. fileNameFolder = utilityFileSystem.createFileName(fileNameFolder, serverUrl: serverUrl, account: session.account)
  41. if fileNameFolder.isEmpty {
  42. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  43. }
  44. guard let directory = self.database.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", session.account, serverUrl)) else {
  45. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  46. }
  47. // TEST UPLOAD IN PROGRESS
  48. //
  49. if networkingE2EE.isInUpload(account: session.account, serverUrl: serverUrl) {
  50. return NKError(errorCode: NCGlobal.shared.errorE2EEUploadInProgress, errorDescription: NSLocalizedString("_e2e_in_upload_", comment: ""))
  51. }
  52. func sendE2ee(e2eToken: String, fileId: String, session: NCSession.Session) async -> NKError {
  53. var key: NSString?
  54. var initializationVector: NSString?
  55. var method = "POST"
  56. // DOWNLOAD METADATA
  57. //
  58. let errorDownloadMetadata = await networkingE2EE.downloadMetadata(serverUrl: serverUrl, fileId: fileId, e2eToken: e2eToken, session: session)
  59. if errorDownloadMetadata == .success {
  60. method = "PUT"
  61. } else if errorDownloadMetadata.errorCode != NCGlobal.shared.errorResourceNotFound {
  62. return errorDownloadMetadata
  63. }
  64. NCEndToEndEncryption.shared().encodedkey(&key, initializationVector: &initializationVector)
  65. guard let key = key as? String, let initializationVector = initializationVector as? String else {
  66. return NKError(errorCode: NCGlobal.shared.errorE2EEEncodedKey, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  67. }
  68. let object = tableE2eEncryption.init(account: session.account, ocIdServerUrl: directory.ocId, fileNameIdentifier: fileNameIdentifier)
  69. object.blob = "folders"
  70. if let results = self.database.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", session.account, serverUrl)) {
  71. object.metadataKey = results.metadataKey
  72. object.metadataKeyIndex = results.metadataKeyIndex
  73. } else {
  74. guard let key = NCEndToEndEncryption.shared().generateKey() as NSData? else {
  75. return NKError(errorCode: NCGlobal.shared.errorE2EEGenerateKey, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  76. }
  77. object.metadataKey = key.base64EncodedString()
  78. object.metadataKeyIndex = 0
  79. }
  80. object.authenticationTag = ""
  81. object.fileName = fileNameFolder
  82. object.key = key
  83. object.initializationVector = initializationVector
  84. object.mimeType = "httpd/unix-directory"
  85. object.serverUrl = serverUrl
  86. self.database.addE2eEncryption(object)
  87. // UPLOAD METADATA
  88. //
  89. let uploadMetadataError = await networkingE2EE.uploadMetadata(serverUrl: serverUrl,
  90. ocIdServerUrl: directory.ocId,
  91. fileId: fileId,
  92. e2eToken: e2eToken,
  93. method: method,
  94. session: session)
  95. return uploadMetadataError
  96. }
  97. // LOCK
  98. //
  99. let resultsLock = await networkingE2EE.lock(account: session.account, serverUrl: serverUrl)
  100. guard let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId, resultsLock.error == .success else {
  101. return NKError(errorCode: NCGlobal.shared.errorE2EELock, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  102. }
  103. // SEND NEW METADATA
  104. //
  105. let sendE2eeError = await sendE2ee(e2eToken: e2eToken, fileId: fileId, session: session)
  106. guard sendE2eeError == .success else {
  107. await networkingE2EE.unlock(account: session.account, serverUrl: serverUrl)
  108. return sendE2eeError
  109. }
  110. // CREATE FOLDER
  111. //
  112. let resultsCreateFolder = await NCNetworking.shared.createFolder(serverUrlFileName: serverUrlFileName, account: session.account, options: NKRequestOptions(customHeader: ["e2e-token": e2eToken]))
  113. guard resultsCreateFolder.error == .success, let ocId = resultsCreateFolder.ocId, let fileId = utility.ocIdToFileId(ocId: ocId) else {
  114. await networkingE2EE.unlock(account: session.account, serverUrl: serverUrl)
  115. return resultsCreateFolder.error
  116. }
  117. // SET FOLDER AS E2EE
  118. //
  119. let resultsMarkE2EEFolder = await NCNetworking.shared.markE2EEFolder(fileId: fileId, delete: false, account: session.account, options: NCNetworkingE2EE().getOptions(account: session.account))
  120. guard resultsMarkE2EEFolder.error == .success else {
  121. await networkingE2EE.unlock(account: session.account, serverUrl: serverUrl)
  122. return resultsMarkE2EEFolder.error
  123. }
  124. // UNLOCK
  125. //
  126. await networkingE2EE.unlock(account: session.account, serverUrl: serverUrl)
  127. // WRITE DB (DIRECTORY - METADATA)
  128. //
  129. let resultsReadFileOrFolder = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", account: session.account)
  130. guard resultsReadFileOrFolder.error == .success, let file = resultsReadFileOrFolder.files?.first else {
  131. await networkingE2EE.unlock(account: session.account, serverUrl: serverUrl)
  132. return resultsReadFileOrFolder.error
  133. }
  134. let metadata = self.database.convertFileToMetadata(file, isDirectoryE2EE: true)
  135. self.database.addMetadata(metadata)
  136. self.database.addDirectory(e2eEncrypted: true, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  137. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCreateFolder, userInfo: ["ocId": ocId, "serverUrl": serverUrl, "account": session.account, "withPush": withPush, "sceneIdentifier": sceneIdentifier as Any])
  138. return NKError()
  139. }
  140. }