NCNetworking+Synchronization.swift 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 JGProgressHUD
  25. import NextcloudKit
  26. import Alamofire
  27. extension NCNetworking {
  28. func synchronization(account: String,
  29. serverUrl: String,
  30. add: Bool,
  31. completion: @escaping (_ errorCode: Int, _ items: Int) -> Void = { _, _ in }) {
  32. let startDate = Date()
  33. let options = NKRequestOptions(timeout: 120, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  34. NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl,
  35. depth: "infinity",
  36. showHiddenFiles: NCKeychain().showHiddenFiles,
  37. account: account,
  38. options: options) { resultAccount, files, _, error in
  39. guard account == resultAccount else { return }
  40. var metadatasDirectory: [tableMetadata] = []
  41. var metadatasSynchronizationOffline: [tableMetadata] = []
  42. if !add {
  43. if NCManageDatabase.shared.getResultMetadata(predicate: NSPredicate(format: "account == %@ AND sessionSelector == %@ AND (status == %d OR status == %d)", account, NCGlobal.shared.selectorSynchronizationOffline, NCGlobal.shared.metadataStatusWaitDownload, NCGlobal.shared.metadataStatusDownloading)) != nil { return }
  44. }
  45. if error == .success {
  46. for file in files {
  47. if file.directory {
  48. metadatasDirectory.append(NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: false))
  49. } else if self.isSynchronizable(ocId: file.ocId, fileName: file.fileName, etag: file.etag) {
  50. metadatasSynchronizationOffline.append(NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: false))
  51. }
  52. }
  53. NCManageDatabase.shared.addMetadatas(metadatasDirectory)
  54. NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: metadatasSynchronizationOffline,
  55. session: NCNetworking.shared.sessionDownloadBackground,
  56. selector: NCGlobal.shared.selectorSynchronizationOffline)
  57. NCManageDatabase.shared.setDirectorySynchronizationDate(serverUrl: serverUrl, account: account)
  58. let diffDate = Date().timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
  59. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Synchronization \(serverUrl) in \(diffDate)")
  60. completion(0, metadatasSynchronizationOffline.count)
  61. } else {
  62. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Synchronization \(serverUrl), \(error.errorCode), \(error.description)")
  63. completion(error.errorCode, metadatasSynchronizationOffline.count)
  64. }
  65. }
  66. }
  67. @discardableResult
  68. func synchronization(account: String, serverUrl: String, add: Bool) async -> (errorCode: Int, items: Int) {
  69. await withUnsafeContinuation({ continuation in
  70. synchronization(account: account, serverUrl: serverUrl, add: add) { errorCode, items in
  71. continuation.resume(returning: (errorCode, items))
  72. }
  73. })
  74. }
  75. func isSynchronizable(ocId: String, fileName: String, etag: String) -> Bool {
  76. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId),
  77. (metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status == NCGlobal.shared.metadataStatusWaitDownload) {
  78. return false
  79. }
  80. let localFile = NCManageDatabase.shared.getResultsTableLocalFile(predicate: NSPredicate(format: "ocId == %@", ocId))?.first
  81. if localFile?.etag != etag || NCUtilityFileSystem().fileProviderStorageSize(ocId, fileNameView: fileName) == 0 {
  82. return true
  83. } else {
  84. return false
  85. }
  86. }
  87. }