NCNetworkingE2EEDelete.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 UIKit
  23. import NextcloudKit
  24. class NCNetworkingE2EEDelete: NSObject {
  25. let database = NCManageDatabase.shared
  26. let networkingE2EE = NCNetworkingE2EE()
  27. func delete(metadata: tableMetadata) async -> NKError {
  28. let session = NCSession.shared.getSession(account: metadata.account)
  29. guard let directory = self.database.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, metadata.serverUrl)) else {
  30. return NKError(errorCode: NCGlobal.shared.errorUnexpectedResponseFromDB, errorDescription: "_e2e_error_")
  31. }
  32. // TEST UPLOAD IN PROGRESS
  33. //
  34. if networkingE2EE.isInUpload(account: metadata.account, serverUrl: metadata.serverUrl) {
  35. return NKError(errorCode: NCGlobal.shared.errorE2EEUploadInProgress, errorDescription: NSLocalizedString("_e2e_in_upload_", comment: ""))
  36. }
  37. // LOCK
  38. //
  39. let resultsLock = await networkingE2EE.lock(account: metadata.account, serverUrl: metadata.serverUrl)
  40. guard resultsLock.error == .success, let e2eToken = resultsLock.e2eToken, let fileId = resultsLock.fileId else { return resultsLock.error }
  41. // DELETE FILE
  42. //
  43. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  44. let options = NKRequestOptions(customHeader: ["e2e-token": e2eToken], taskDescription: NCGlobal.shared.taskDescriptionDeleteFileOrFolder)
  45. let result = await NCNetworking.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: metadata.account, options: options)
  46. if result.error == .success || result.error.errorCode == NCGlobal.shared.errorResourceNotFound {
  47. do {
  48. try FileManager.default.removeItem(atPath: NCUtilityFileSystem().getDirectoryProviderStorageOcId(metadata.ocId))
  49. database.deleteVideo(metadata: metadata)
  50. database.deleteMetadataOcId(metadata.ocId)
  51. database.deleteLocalFileOcId(metadata.ocId)
  52. // LIVE PHOTO SERVER
  53. if let metadataLive = self.database.getMetadataLivePhoto(metadata: metadata), metadataLive.isFlaggedAsLivePhotoByServer {
  54. do {
  55. try FileManager.default.removeItem(atPath: NCUtilityFileSystem().getDirectoryProviderStorageOcId(metadataLive.ocId))
  56. } catch { }
  57. self.database.deleteVideo(metadata: metadataLive)
  58. self.database.deleteMetadataOcId(metadataLive.ocId)
  59. self.database.deleteLocalFileOcId(metadataLive.ocId)
  60. }
  61. if metadata.directory {
  62. self.database.deleteDirectoryAndSubDirectory(serverUrl: NCUtilityFileSystem().stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName), account: metadata.account)
  63. }
  64. } catch { }
  65. } else {
  66. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  67. return result.error
  68. }
  69. // DOWNLOAD METADATA
  70. //
  71. let errorDownloadMetadata = await networkingE2EE.downloadMetadata(serverUrl: metadata.serverUrl, fileId: fileId, e2eToken: e2eToken, session: session)
  72. guard errorDownloadMetadata == .success else {
  73. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  74. return errorDownloadMetadata
  75. }
  76. // UPDATE DB
  77. //
  78. self.database.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@",
  79. metadata.account,
  80. metadata.serverUrl,
  81. metadata.fileName))
  82. // UPLOAD METADATA
  83. //
  84. let uploadMetadataError = await networkingE2EE.uploadMetadata(serverUrl: metadata.serverUrl,
  85. ocIdServerUrl: directory.ocId,
  86. fileId: fileId,
  87. e2eToken: e2eToken,
  88. method: "PUT",
  89. session: session)
  90. guard uploadMetadataError == .success else {
  91. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  92. return uploadMetadataError
  93. }
  94. // UNLOCK
  95. //
  96. await networkingE2EE.unlock(account: metadata.account, serverUrl: metadata.serverUrl)
  97. return NKError()
  98. }
  99. }