NCNetworkingAutoUpload.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // NCNetworkingAutoUpload.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/06/2020.
  6. // Copyright © 2020 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 Foundation
  24. import NCCommunication
  25. class NCNetworkingAutoUpload: NSObject {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var timerProcess: Timer?
  28. override init() {
  29. super.init()
  30. startTimer()
  31. }
  32. @objc func startProcess() {
  33. if timerProcess?.isValid ?? false {
  34. process()
  35. }
  36. }
  37. func startTimer() {
  38. timerProcess = Timer.scheduledTimer(timeInterval: TimeInterval(k_timerAutoUpload), target: self, selector: #selector(process), userInfo: nil, repeats: true)
  39. }
  40. @objc private func process() {
  41. var counterUpload: Int = 0
  42. var sizeUpload = 0
  43. var maxConcurrentOperationUpload = Int(k_maxConcurrentOperation)
  44. let sessionSelectors = [selectorUploadFile, selectorUploadAutoUpload, selectorUploadAutoUploadAll]
  45. if appDelegate.account == nil || appDelegate.account.count == 0 || appDelegate.maintenanceMode {
  46. return
  47. }
  48. let metadatasUpload = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", k_metadataStatusInUpload, k_metadataStatusUploading))
  49. counterUpload = metadatasUpload.count
  50. for metadata in metadatasUpload {
  51. sizeUpload = sizeUpload + Int(metadata.size)
  52. }
  53. if sizeUpload > k_maxSizeOperationUpload { return }
  54. timerProcess?.invalidate()
  55. debugPrint("[LOG] PROCESS-AUTO-UPLOAD \(counterUpload)")
  56. NCNetworking.shared.getOcIdInBackgroundSession { (listOcId) in
  57. for sessionSelector in sessionSelectors {
  58. if counterUpload < maxConcurrentOperationUpload {
  59. let limit = maxConcurrentOperationUpload - counterUpload
  60. var predicate = NSPredicate()
  61. if UIApplication.shared.applicationState == .background {
  62. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d AND (typeFile != %@ || livePhoto == true)", sessionSelector, k_metadataStatusWaitUpload, k_metadataTypeFile_video)
  63. } else {
  64. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d", sessionSelector, k_metadataStatusWaitUpload)
  65. }
  66. let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: predicate, page: 1, limit: limit, sorted: "date", ascending: true)
  67. if metadatas.count > 0 {
  68. NCCommunicationCommon.shared.writeLog("PROCESS-AUTO-UPLOAD find \(metadatas.count) items")
  69. }
  70. for metadata in metadatas {
  71. // Is already in upload ? skipped
  72. if listOcId.contains(metadata.ocId) {
  73. NCCommunicationCommon.shared.writeLog("Process auto upload skipped file: \(metadata.serverUrl)/\(metadata.fileNameView), because is already in session.")
  74. continue
  75. }
  76. // Session Extension ? skipped
  77. if metadata.session == NCNetworking.shared.sessionIdentifierBackgroundExtension {
  78. continue
  79. }
  80. if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) {
  81. if UIApplication.shared.applicationState == .background { break }
  82. maxConcurrentOperationUpload = 1
  83. counterUpload += 1
  84. if let metadata = NCManageDatabase.sharedInstance.setMetadataStatus(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload)) {
  85. NCNetworking.shared.upload(metadata: metadata) { (_, _) in }
  86. }
  87. self.startTimer()
  88. return
  89. } else {
  90. counterUpload += 1
  91. if let metadata = NCManageDatabase.sharedInstance.setMetadataStatus(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload)) {
  92. NCNetworking.shared.upload(metadata: metadata) { (_, _) in }
  93. }
  94. sizeUpload = sizeUpload + Int(metadata.size)
  95. if sizeUpload > k_maxSizeOperationUpload {
  96. self.startTimer()
  97. return
  98. }
  99. }
  100. }
  101. } else {
  102. self.startTimer()
  103. return
  104. }
  105. }
  106. // No upload available ? --> Retry Upload in Error
  107. if counterUpload == 0 {
  108. let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status == %d", k_metadataStatusUploadError))
  109. for metadata in metadatas {
  110. NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, session: NCNetworking.shared.sessionIdentifierBackground, sessionError: "", sessionTaskIdentifier: 0 ,status: Int(k_metadataStatusWaitUpload))
  111. }
  112. }
  113. // verify delete Asset Local Identifiers in auto upload (DELETE Photos album)
  114. if (counterUpload == 0 && self.appDelegate.passcodeViewController == nil) {
  115. NCUtility.shared.deleteAssetLocalIdentifiers(account: self.appDelegate.account, sessionSelector: selectorUploadAutoUpload) {
  116. self.startTimer()
  117. }
  118. } else {
  119. self.startTimer()
  120. }
  121. }
  122. }
  123. }