NCNetworkingE2EERename.swift 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 NextcloudKit
  22. import Foundation
  23. class NCNetworkingE2EERename: NSObject {
  24. let networkingE2EE = NCNetworkingE2EE()
  25. func rename(metadata: tableMetadata, fileNameNew: String, indexPath: IndexPath) async -> NKError {
  26. // verify if exists the new fileName
  27. if NCManageDatabase.shared.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, fileNameNew)) != nil {
  28. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_file_already_exists_")
  29. }
  30. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) else {
  31. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  32. }
  33. // LOCK
  34. //
  35. let resultsLock = await networkingE2EE.lock(account: metadata.account, serverUrl: metadata.serverUrl)
  36. guard resultsLock.error == .success, let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId else { return resultsLock.error }
  37. // DOWNLOAD METADATA
  38. //
  39. let errorDownloadMetadata = await networkingE2EE.downloadMetadata(account: metadata.account, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, fileId: fileId, e2eToken: e2eToken)
  40. guard errorDownloadMetadata == .success else {
  41. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  42. return errorDownloadMetadata
  43. }
  44. // DB RENAME
  45. //
  46. NCManageDatabase.shared.renameFileE2eEncryption(account: metadata.account, 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))
  47. // UPLOAD METADATA
  48. //
  49. let uploadMetadataError = await networkingE2EE.uploadMetadata(account: metadata.account,
  50. serverUrl: metadata.serverUrl,
  51. ocIdServerUrl: directory.ocId,
  52. fileId: fileId,
  53. userId: metadata.userId,
  54. e2eToken: e2eToken,
  55. method: "PUT")
  56. guard uploadMetadataError == .success else {
  57. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  58. return uploadMetadataError
  59. }
  60. // UPDATE DB
  61. //
  62. NCManageDatabase.shared.setMetadataFileNameView(serverUrl: metadata.serverUrl, fileName: metadata.fileName, newFileNameView: fileNameNew, account: metadata.account)
  63. // MOVE FILE SYSTEM
  64. //
  65. let atPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileNameView
  66. let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + fileNameNew
  67. do {
  68. try FileManager.default.moveItem(atPath: atPath, toPath: toPath)
  69. } catch { }
  70. // UNLOCK
  71. //
  72. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  73. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterRenameFile, userInfo: ["ocId": metadata.ocId, "account": metadata.account, "indexPath": indexPath])
  74. return NKError()
  75. }
  76. }