NCNetworkingE2EECreateFolder.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 OpenSSL
  23. import NextcloudKit
  24. import CFNetwork
  25. import Alamofire
  26. import Foundation
  27. class NCNetworkingE2EECreateFolder: NSObject {
  28. let networkingE2EE = NCNetworkingE2EE()
  29. func createFolder(fileName: String, serverUrl: String, account: String, urlBase: String, userId: String, withPush: Bool) async -> NKError {
  30. let fileNameIdentifier = networkingE2EE.generateRandomIdentifier()
  31. let serverUrlFileName = serverUrl + "/" + fileNameIdentifier
  32. let fileNameFolder = NCUtilityFileSystem.shared.createFileName(CCUtility.removeForbiddenCharactersServer(fileName)!, serverUrl: serverUrl, account: account)
  33. if fileNameFolder.isEmpty {
  34. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  35. }
  36. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) else {
  37. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  38. }
  39. func sendE2ee(e2eToken: String, fileId: String) async -> NKError {
  40. var key: NSString?
  41. var initializationVector: NSString?
  42. var method = "POST"
  43. // DOWNLOAD METADATA
  44. //
  45. let errorDownloadMetadata = await networkingE2EE.downloadMetadata(account: account, serverUrl: serverUrl, urlBase: urlBase, userId: userId, fileId: fileId, e2eToken: e2eToken)
  46. if errorDownloadMetadata == .success {
  47. method = "PUT"
  48. } else if errorDownloadMetadata.errorCode != NCGlobal.shared.errorResourceNotFound {
  49. return errorDownloadMetadata
  50. }
  51. NCEndToEndEncryption.sharedManager()?.encodedkey(&key, initializationVector: &initializationVector)
  52. guard let key = key as? String, let initializationVector = initializationVector as? String else {
  53. return NKError(errorCode: NCGlobal.shared.errorE2EEEncodedKey, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  54. }
  55. let object = tableE2eEncryption.init(account: account, ocIdServerUrl: directory.ocId, fileNameIdentifier: fileNameIdentifier)
  56. object.blob = "folders"
  57. if let results = NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl)) {
  58. object.metadataKey = results.metadataKey
  59. object.metadataKeyIndex = results.metadataKeyIndex
  60. } else {
  61. guard let key = NCEndToEndEncryption.sharedManager()?.generateKey() as NSData? else {
  62. return NKError(errorCode: NCGlobal.shared.errorE2EEGenerateKey, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  63. }
  64. object.metadataKey = key.base64EncodedString()
  65. object.metadataKeyIndex = 0
  66. }
  67. object.authenticationTag = ""
  68. object.fileName = fileNameFolder
  69. object.key = key
  70. object.initializationVector = initializationVector
  71. object.mimeType = "httpd/unix-directory"
  72. object.serverUrl = serverUrl
  73. NCManageDatabase.shared.addE2eEncryption(object)
  74. // UPLOAD METADATA
  75. //
  76. let uploadMetadataError = await networkingE2EE.uploadMetadata(account: account,
  77. serverUrl: serverUrl,
  78. ocIdServerUrl: directory.ocId,
  79. fileId: fileId,
  80. userId: userId,
  81. e2eToken: e2eToken,
  82. method: method)
  83. return uploadMetadataError
  84. }
  85. // LOCK
  86. //
  87. let resultsLock = await networkingE2EE.lock(account: account, serverUrl: serverUrl)
  88. guard let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId, resultsLock.error == .success else {
  89. return NKError(errorCode: NCGlobal.shared.errorE2EELock, errorDescription: NSLocalizedString("_e2e_error_", comment: ""))
  90. }
  91. // SEND NEW METADATA
  92. //
  93. let sendE2eeError = await sendE2ee(e2eToken: e2eToken, fileId: fileId)
  94. guard sendE2eeError == .success else {
  95. await networkingE2EE.unlock(account: account, serverUrl: serverUrl)
  96. return sendE2eeError
  97. }
  98. // CREATE FOLDER
  99. //
  100. let resultsCreateFolder = await NextcloudKit.shared.createFolder(serverUrlFileName: serverUrlFileName, options: NKRequestOptions(customHeader: ["e2e-token": e2eToken]))
  101. guard resultsCreateFolder.error == .success, let ocId = resultsCreateFolder.ocId, let fileId = NCUtility.shared.ocIdToFileId(ocId: ocId) else {
  102. await networkingE2EE.unlock(account: account, serverUrl: serverUrl)
  103. return resultsCreateFolder.error
  104. }
  105. // MARK FOLDER AS E2EE
  106. //
  107. let resultsMarkE2EEFolder = await NextcloudKit.shared.markE2EEFolder(fileId: fileId, delete: false)
  108. guard resultsMarkE2EEFolder.error == .success else {
  109. await networkingE2EE.unlock(account: account, serverUrl: serverUrl)
  110. return resultsMarkE2EEFolder.error
  111. }
  112. // UNLOCK
  113. //
  114. await networkingE2EE.unlock(account: account, serverUrl: serverUrl)
  115. //
  116. //
  117. let resultsReadFileOrFolder = await NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0")
  118. guard resultsReadFileOrFolder.error == .success, let file = resultsReadFileOrFolder.files.first else {
  119. await networkingE2EE.unlock(account: account, serverUrl: serverUrl)
  120. return resultsReadFileOrFolder.error
  121. }
  122. let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: true)
  123. NCManageDatabase.shared.addMetadata(metadata)
  124. NCManageDatabase.shared.addDirectory(encrypted: true, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, etag: nil, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  125. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCreateFolder, userInfo: ["ocId": ocId, "serverUrl": serverUrl, "account": account, "withPush": withPush])
  126. return NKError()
  127. }
  128. }