NCNetworkingE2EEMarkFolder.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // NCNetworkingE2EEMarkFolder.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/08/23.
  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 Foundation
  22. import UIKit
  23. import NextcloudKit
  24. class NCNetworkingE2EEMarkFolder: NSObject {
  25. let database = NCManageDatabase.shared
  26. func markFolderE2ee(account: String, fileName: String, serverUrl: String, userId: String) async -> NKError {
  27. let serverUrlFileName = serverUrl + "/" + fileName
  28. let resultsReadFileOrFolder = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", account: account)
  29. guard resultsReadFileOrFolder.error == .success,
  30. let file = resultsReadFileOrFolder.files?.first else {
  31. return resultsReadFileOrFolder.error
  32. }
  33. let resultsMarkE2EEFolder = await NCNetworking.shared.markE2EEFolder(fileId: file.fileId, delete: false, account: account, options: NCNetworkingE2EE().getOptions(account: account))
  34. guard resultsMarkE2EEFolder.error == .success else { return resultsMarkE2EEFolder.error }
  35. file.e2eEncrypted = true
  36. guard let metadata = self.database.createMetadata(self.database.convertFileToMetadata(file, isDirectoryE2EE: false)) else {
  37. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  38. }
  39. self.database.addDirectory(e2eEncrypted: true, favorite: metadata.favorite, ocId: metadata.ocId, fileId: metadata.fileId, permissions: metadata.permissions, serverUrl: serverUrlFileName, account: metadata.account)
  40. self.database.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrlFileName))
  41. if NCCapabilities.shared.getCapabilities(account: account).capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  42. self.database.updateCounterE2eMetadata(account: account, ocIdServerUrl: metadata.ocId, counter: 0)
  43. }
  44. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCreateFolder, userInfo: ["ocId": metadata.ocId, "serverUrl": serverUrl, "account": account, "withPush": true])
  45. return NKError()
  46. }
  47. }