NCNetworkingE2EERename.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. @objc class NCNetworkingE2EERename: NSObject {
  28. @objc public static let shared: NCNetworkingE2EERename = {
  29. let instance = NCNetworkingE2EERename()
  30. return instance
  31. }()
  32. func renameMetadata(_ 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. let sendE2EMetadataResults = await
  38. NCNetworkingE2EE.shared.sendE2EMetadata(account: metadata.account,
  39. serverUrl: metadata.serverUrl,
  40. fileNameRename: metadata.fileName,
  41. fileNameNewRename: fileNameNew,
  42. deleteE2eEncryption: nil,
  43. urlBase: metadata.urlBase,
  44. userId: metadata.userId)
  45. if sendE2EMetadataResults.error == .success {
  46. NCManageDatabase.shared.setMetadataFileNameView(serverUrl: metadata.serverUrl, fileName: metadata.fileName, newFileNameView: fileNameNew, account: metadata.account)
  47. // Move file system
  48. let atPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileNameView
  49. let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + fileNameNew
  50. do {
  51. try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
  52. } catch { }
  53. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterRenameFile, userInfo: ["ocId": metadata.ocId, "account": metadata.account])
  54. }
  55. // unlock
  56. if let tableLock = NCManageDatabase.shared.getE2ETokenLock(account: metadata.account, serverUrl: metadata.serverUrl) {
  57. await NextcloudKit.shared.lockE2EEFolder(fileId: tableLock.fileId, e2eToken: tableLock.e2eToken, method: "DELETE")
  58. }
  59. return sendE2EMetadataResults.error
  60. }
  61. }
  62. }