NCNetworkingE2EEDelete.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // NCNetworkingE2EEDelete.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 Foundation
  22. import NextcloudKit
  23. class NCNetworkingE2EEDelete: NSObject {
  24. let networkingE2EE = NCNetworkingE2EE()
  25. func delete(metadata: tableMetadata) async -> NKError {
  26. guard let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) else {
  27. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  28. }
  29. // LOCK
  30. //
  31. let resultsLock = await networkingE2EE.lock(account: metadata.account, serverUrl: metadata.serverUrl)
  32. guard resultsLock.error == .success, let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId else { return resultsLock.error }
  33. // DELETE FILE
  34. //
  35. let deleteMetadataPlainError = await NCNetworking.shared.deleteMetadataPlain(metadata, customHeader: ["e2e-token": e2eToken])
  36. guard deleteMetadataPlainError == .success else {
  37. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  38. return deleteMetadataPlainError
  39. }
  40. // DOWNLOAD METADATA
  41. //
  42. let errorDownloadMetadata = await networkingE2EE.downloadMetadata(account: metadata.account, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, fileId: fileId, e2eToken: e2eToken)
  43. guard errorDownloadMetadata == .success else {
  44. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  45. return errorDownloadMetadata
  46. }
  47. // UPDATE DB
  48. //
  49. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, metadata.serverUrl, metadata.fileName))
  50. // UPLOAD METADATA
  51. //
  52. let uploadMetadataError = await networkingE2EE.uploadMetadata(account: metadata.account,
  53. serverUrl: metadata.serverUrl,
  54. ocIdServerUrl: directory.ocId,
  55. fileId: fileId,
  56. userId: metadata.userId,
  57. e2eToken: e2eToken,
  58. method: "PUT")
  59. guard uploadMetadataError == .success else {
  60. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  61. return uploadMetadataError
  62. }
  63. // UNLOCK
  64. //
  65. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  66. return NKError()
  67. }
  68. }