NCNetworkingAutoUpload.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. timerProcess = Timer.scheduledTimer(timeInterval: TimeInterval(k_timerAutoUpload), target: self, selector: #selector(process), userInfo: nil, repeats: false)
  31. }
  32. @objc func startProcess() {
  33. if timerProcess?.isValid ?? false {
  34. process()
  35. }
  36. }
  37. @objc private func process() {
  38. var counterUpload = 0
  39. var sizeUpload = 0
  40. var maxConcurrentOperationUpload = k_maxConcurrentOperation
  41. timerProcess?.invalidate()
  42. let metadatasUpload = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", k_metadataStatusInUpload, k_metadataStatusUploading), freeze: true)
  43. counterUpload = metadatasUpload.count
  44. for metadata in metadatasUpload {
  45. sizeUpload = sizeUpload + Int(metadata.size)
  46. }
  47. debugPrint("[LOG] PROCESS-AUTO-UPLOAD \(counterUpload)")
  48. // ------------------------- <selector Upload> -------------------------
  49. while counterUpload < maxConcurrentOperationUpload {
  50. if sizeUpload > k_maxSizeOperationUpload { break }
  51. var predicate = NSPredicate()
  52. if UIApplication.shared.applicationState == .background {
  53. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d AND typeFile != %@", selectorUploadFile, k_metadataStatusWaitUpload, k_metadataTypeFile_video)
  54. } else {
  55. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d", selectorUploadFile, k_metadataStatusWaitUpload)
  56. }
  57. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: predicate, sorted: "date", ascending: true, freeze: true) {
  58. if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account) {
  59. if UIApplication.shared.applicationState == .background { break }
  60. maxConcurrentOperationUpload = 1
  61. }
  62. NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload))
  63. NCNetworking.shared.upload(metadata: metadata, background: true) { (_, _) in }
  64. counterUpload += 1
  65. sizeUpload = sizeUpload + Int(metadata.size)
  66. } else {
  67. break
  68. }
  69. }
  70. // ------------------------- <selector Auto Upload> -------------------------
  71. while counterUpload < maxConcurrentOperationUpload {
  72. if sizeUpload > k_maxSizeOperationUpload { break }
  73. var predicate = NSPredicate()
  74. if UIApplication.shared.applicationState == .background {
  75. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d AND typeFile != %@", selectorUploadAutoUpload, k_metadataStatusWaitUpload, k_metadataTypeFile_video)
  76. } else {
  77. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d", selectorUploadAutoUpload, k_metadataStatusWaitUpload)
  78. }
  79. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: predicate, sorted: "date", ascending: true, freeze: true) {
  80. if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account) {
  81. if UIApplication.shared.applicationState == .background { break }
  82. maxConcurrentOperationUpload = 1
  83. }
  84. NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload))
  85. NCNetworking.shared.upload(metadata: metadata, background: true) { (_, _) in }
  86. counterUpload += 1
  87. sizeUpload = sizeUpload + Int(metadata.size)
  88. } else {
  89. break
  90. }
  91. }
  92. // ------------------------- <selector Auto Upload All> ----------------------
  93. // Verify num error k_maxErrorAutoUploadAll after STOP (100)
  94. let metadatasInError = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "sessionSelector == %@ AND status == %d", selectorUploadAutoUploadAll, k_metadataStatusUploadError), freeze: true)
  95. if metadatasInError.count >= k_maxErrorAutoUploadAll {
  96. NCContentPresenter.shared.messageNotification("_error_", description: "_too_errors_automatic_all_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  97. } else {
  98. while counterUpload < maxConcurrentOperationUpload {
  99. if sizeUpload > k_maxSizeOperationUpload { break }
  100. var predicate = NSPredicate()
  101. if UIApplication.shared.applicationState == .background {
  102. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d AND typeFile != %@", selectorUploadAutoUploadAll, k_metadataStatusWaitUpload, k_metadataTypeFile_video)
  103. } else {
  104. predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d", selectorUploadAutoUploadAll, k_metadataStatusWaitUpload)
  105. }
  106. if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: predicate, sorted: "date", ascending: true, freeze: true) {
  107. if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account) {
  108. if UIApplication.shared.applicationState == .background { break }
  109. maxConcurrentOperationUpload = 1
  110. }
  111. NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload))
  112. NCNetworking.shared.upload(metadata: metadata, background: true) { (_, _) in }
  113. counterUpload += 1
  114. sizeUpload = sizeUpload + Int(metadata.size)
  115. } else {
  116. break
  117. }
  118. }
  119. }
  120. // No upload available ? --> Retry Upload in Error
  121. if counterUpload == 0 {
  122. let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status == %d", selectorUploadAutoUploadAll, k_metadataStatusUploadError), freeze: true)
  123. for metadata in metadatas {
  124. NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, session: NCCommunicationCommon.shared.sessionIdentifierBackground, sessionError: "", sessionTaskIdentifier: 0 ,status: Int(k_metadataStatusInUpload))
  125. }
  126. }
  127. // verify delete Asset Local Identifiers in auto upload (Photos album)
  128. if (counterUpload == 0 && appDelegate.passcodeViewController == nil) {
  129. NCUtility.sharedInstance.deleteAssetLocalIdentifiers(account: appDelegate.activeAccount, sessionSelector: selectorUploadAutoUpload)
  130. }
  131. timerProcess = Timer.scheduledTimer(timeInterval: TimeInterval(k_timerAutoUpload), target: self, selector: #selector(process), userInfo: nil, repeats: false)
  132. }
  133. }