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