NCNetworkingE2EERename.swift 4.9 KB

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