NCNetworkingE2EERename.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // NCNetworkingE2EERename.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 NCNetworkingE2EERename: NSObject {
  28. public static let shared: NCNetworkingE2EERename = {
  29. let instance = NCNetworkingE2EERename()
  30. return instance
  31. }()
  32. let errorEncodeMetadata = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: NSLocalizedString("_e2e_error_encode_metadata_", comment: ""))
  33. let fileAlreadyExists = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_file_already_exists_")
  34. func rename(metadata: tableMetadata, fileNameNew: String, indexPath: IndexPath) async -> (NKError) {
  35. var error = NKError()
  36. func sendE2EMetadata(e2eToken: String, fileId: String) async -> (NKError) {
  37. // Get last metadata
  38. let getE2EEMetadataResults = await NextcloudKit.shared.getE2EEMetadata(fileId: fileId, e2eToken: e2eToken)
  39. guard getE2EEMetadataResults.error == .success, let e2eMetadata = getE2EEMetadataResults.e2eMetadata else {
  40. return errorEncodeMetadata
  41. }
  42. error = NCEndToEndMetadata().decoderMetadata(e2eMetadata, serverUrl: metadata.serverUrl, account: metadata.account, urlBase: metadata.urlBase, userId: metadata.userId, ownerId: metadata.ownerId)
  43. if error != .success { return error }
  44. // rename
  45. NCManageDatabase.shared.renameFileE2eEncryption(serverUrl: metadata.serverUrl, fileNameIdentifier: metadata.fileName, newFileName: fileNameNew, newFileNamePath: CCUtility.returnFileNamePath(fromFileName: fileNameNew, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, account: metadata.account))
  46. // Rebuild metadata
  47. guard let tableE2eEncryption = NCManageDatabase.shared.getE2eEncryptions(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) else {
  48. return errorEncodeMetadata
  49. }
  50. let resultEncoder = NCEndToEndMetadata().encoderMetadata(tableE2eEncryption, account: metadata.account, serverUrl: metadata.serverUrl)
  51. if resultEncoder.metadata == nil {
  52. return errorEncodeMetadata
  53. }
  54. // send metadata
  55. let putE2EEMetadataResults = await NextcloudKit.shared.putE2EEMetadata(fileId: fileId, e2eToken: e2eToken, e2eMetadata: resultEncoder.metadata, signature: resultEncoder.signature, method: "PUT")
  56. return putE2EEMetadataResults.error
  57. }
  58. // verify if exists the new fileName
  59. if NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, fileNameNew)) != nil {
  60. return fileAlreadyExists
  61. }
  62. // ** Lock **
  63. let lockResults = await NCNetworkingE2EE.shared.lock(account: metadata.account, serverUrl: metadata.serverUrl)
  64. error = lockResults.error
  65. if error == .success, let e2eToken = lockResults.e2eToken, let fileId = lockResults.fileId {
  66. let sendE2EMetadataError = await sendE2EMetadata(e2eToken: e2eToken, fileId: fileId)
  67. error = sendE2EMetadataError
  68. if error == .success {
  69. NCManageDatabase.shared.setMetadataFileNameView(serverUrl: metadata.serverUrl, fileName: metadata.fileName, newFileNameView: fileNameNew, account: metadata.account)
  70. // Move file system
  71. let atPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileNameView
  72. let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + fileNameNew
  73. do {
  74. try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
  75. } catch { }
  76. }
  77. }
  78. // ** Unlock **
  79. await NCNetworkingE2EE.shared.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  80. if error == .success {
  81. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterRenameFile, userInfo: ["ocId": metadata.ocId, "account": metadata.account, "indexPath": indexPath])
  82. }
  83. return error
  84. }
  85. }