NCNetworking+Synchronization.swift 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // NCNetworking+Synchronization.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import NextcloudKit
  25. import Alamofire
  26. extension NCNetworking {
  27. func synchronization(account: String,
  28. serverUrl: String,
  29. add: Bool,
  30. completion: @escaping (_ errorCode: Int, _ num: Int) -> Void = { _, _ in }) {
  31. let startDate = Date()
  32. let options = NKRequestOptions(timeout: 120, taskDescription: NCGlobal.shared.taskDescriptionSynchronization, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  33. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl,
  34. depth: "infinity",
  35. showHiddenFiles: NCKeychain().showHiddenFiles,
  36. account: account,
  37. options: options) { resultAccount, files, _, error in
  38. guard account == resultAccount else { return }
  39. var metadatasDirectory: [tableMetadata] = []
  40. var metadatasSynchronizationOffline: [tableMetadata] = []
  41. if !add {
  42. if self.database.getResultMetadata(predicate: NSPredicate(format: "account == %@ AND sessionSelector == %@ AND (status == %d OR status == %d)",
  43. account,
  44. self.global.selectorSynchronizationOffline,
  45. self.global.metadataStatusWaitDownload,
  46. self.global.metadataStatusDownloading)) != nil { return }
  47. }
  48. if error == .success, let files {
  49. for file in files {
  50. if file.directory {
  51. metadatasDirectory.append(self.database.convertFileToMetadata(file, isDirectoryE2EE: false))
  52. } else if self.isSynchronizable(ocId: file.ocId, fileName: file.fileName, etag: file.etag) {
  53. metadatasSynchronizationOffline.append(self.database.convertFileToMetadata(file, isDirectoryE2EE: false))
  54. }
  55. }
  56. self.database.addMetadatas(metadatasDirectory)
  57. self.database.setMetadatasSessionInWaitDownload(metadatas: metadatasSynchronizationOffline,
  58. session: self.sessionDownloadBackground,
  59. selector: self.global.selectorSynchronizationOffline)
  60. self.database.setDirectorySynchronizationDate(serverUrl: serverUrl, account: account)
  61. let diffDate = Date().timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
  62. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Synchronization \(serverUrl) in \(diffDate)")
  63. completion(0, metadatasSynchronizationOffline.count)
  64. } else {
  65. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Synchronization \(serverUrl), \(error.errorCode), \(error.description)")
  66. completion(error.errorCode, metadatasSynchronizationOffline.count)
  67. }
  68. }
  69. }
  70. @discardableResult
  71. func synchronization(account: String, serverUrl: String, add: Bool) async -> (errorCode: Int, num: Int) {
  72. await withUnsafeContinuation({ continuation in
  73. synchronization(account: account, serverUrl: serverUrl, add: add) { errorCode, num in
  74. continuation.resume(returning: (errorCode, num))
  75. }
  76. })
  77. }
  78. func isSynchronizable(ocId: String, fileName: String, etag: String) -> Bool {
  79. if let metadata = self.database.getMetadataFromOcId(ocId),
  80. (metadata.status == self.global.metadataStatusDownloading || metadata.status == self.global.metadataStatusWaitDownload) {
  81. return false
  82. }
  83. let localFile = self.database.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))?.first
  84. if localFile?.etag != etag || NCUtilityFileSystem().fileProviderStorageSize(ocId, fileNameView: fileName) == 0 {
  85. return true
  86. } else {
  87. return false
  88. }
  89. }
  90. }