NCNetworkingE2EERename.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. func rename(metadata: tableMetadata, fileNameNew: String) async -> (NKError) {
  33. // verify if exists the new fileName
  34. if NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, fileNameNew)) != nil {
  35. return NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_file_already_exists_")
  36. } else {
  37. // Lock & Send metadata
  38. let sendE2EMetadataResults = await
  39. NCNetworkingE2EE.shared.sendE2EMetadata(account: metadata.account,
  40. serverUrl: metadata.serverUrl,
  41. fileNameRename: metadata.fileName,
  42. fileNameNewRename: fileNameNew,
  43. deleteE2eEncryption: nil,
  44. urlBase: metadata.urlBase,
  45. userId: metadata.userId)
  46. if sendE2EMetadataResults.error == .success {
  47. NCManageDatabase.shared.setMetadataFileNameView(serverUrl: metadata.serverUrl, fileName: metadata.fileName, newFileNameView: fileNameNew, account: metadata.account)
  48. // Move file system
  49. let atPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileNameView
  50. let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + fileNameNew
  51. do {
  52. try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
  53. } catch { }
  54. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterRenameFile, userInfo: ["ocId": metadata.ocId, "account": metadata.account])
  55. }
  56. // Unlock
  57. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: metadata.account, serverUrl: metadata.serverUrl) {
  58. await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, method: "DELETE")
  59. }
  60. return sendE2EMetadataResults.error
  61. }
  62. }
  63. }