NCManageDatabase+Metadata+Session.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // NCManageDatabase+Metadata+Session.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/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 Foundation
  24. import RealmSwift
  25. import NextcloudKit
  26. extension NCManageDatabase {
  27. func setMetadataSession(ocId: String,
  28. newFileName: String? = nil,
  29. session: String? = nil,
  30. sessionError: String? = nil,
  31. selector: String? = nil,
  32. status: Int? = nil,
  33. etag: String? = nil,
  34. errorCode: Int? = nil) {
  35. do {
  36. let realm = try Realm()
  37. try realm.write {
  38. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first {
  39. if let newFileName = newFileName {
  40. result.fileName = newFileName
  41. result.fileNameView = newFileName
  42. }
  43. if let session {
  44. result.session = session
  45. }
  46. if let sessionError {
  47. result.sessionError = sessionError
  48. if sessionError.isEmpty {
  49. result.errorCode = 0
  50. }
  51. }
  52. if let selector {
  53. result.sessionSelector = selector
  54. }
  55. if let status {
  56. result.status = status
  57. if status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload {
  58. result.sessionDate = Date()
  59. } else if status == NCGlobal.shared.metadataStatusNormal {
  60. result.sessionDate = nil
  61. }
  62. }
  63. if let etag {
  64. result.etag = etag
  65. }
  66. if let errorCode {
  67. result.errorCode = errorCode
  68. }
  69. }
  70. }
  71. } catch let error {
  72. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  73. }
  74. }
  75. func setMetadataSession(ocId: String,
  76. status: Int? = nil,
  77. taskIdentifier: Int? = nil) {
  78. do {
  79. let realm = try Realm()
  80. try realm.write {
  81. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first {
  82. if let status {
  83. result.status = status
  84. if status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload {
  85. result.sessionDate = Date()
  86. } else if status == NCGlobal.shared.metadataStatusNormal {
  87. result.sessionDate = nil
  88. }
  89. }
  90. if let taskIdentifier {
  91. result.sessionTaskIdentifier = taskIdentifier
  92. }
  93. }
  94. }
  95. } catch let error {
  96. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  97. }
  98. }
  99. @discardableResult
  100. func setMetadatasSessionInWaitDownload(metadatas: [tableMetadata], session: String, selector: String) -> tableMetadata? {
  101. if metadatas.isEmpty { return nil }
  102. var metadataUpdated: tableMetadata?
  103. do {
  104. let realm = try Realm()
  105. try realm.write {
  106. for metadata in metadatas {
  107. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", metadata.ocId).first {
  108. result.session = session
  109. result.sessionError = ""
  110. result.sessionSelector = selector
  111. result.status = NCGlobal.shared.metadataStatusWaitDownload
  112. result.sessionDate = Date()
  113. metadataUpdated = tableMetadata(value: result)
  114. } else {
  115. metadata.session = session
  116. metadata.sessionError = ""
  117. metadata.sessionSelector = selector
  118. metadata.status = NCGlobal.shared.metadataStatusWaitDownload
  119. metadata.sessionDate = Date()
  120. realm.add(metadata, update: .all)
  121. metadataUpdated = tableMetadata(value: metadata)
  122. }
  123. }
  124. }
  125. } catch let error {
  126. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  127. }
  128. return metadataUpdated
  129. }
  130. func clearMetadataSession(metadatas: Results<tableMetadata>) {
  131. do {
  132. let realm = try Realm()
  133. try realm.write {
  134. for metadata in metadatas {
  135. metadata.session = ""
  136. metadata.sessionError = ""
  137. metadata.sessionSelector = ""
  138. metadata.sessionDate = nil
  139. metadata.status = NCGlobal.shared.metadataStatusNormal
  140. }
  141. }
  142. } catch let error {
  143. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  144. }
  145. }
  146. @discardableResult
  147. func setMetadataStatus(ocId: String, status: Int) -> tableMetadata? {
  148. var result: tableMetadata?
  149. do {
  150. let realm = try Realm()
  151. try realm.write {
  152. result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first
  153. result?.status = status
  154. }
  155. } catch let error {
  156. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  157. }
  158. if let result {
  159. return tableMetadata.init(value: result)
  160. } else {
  161. return nil
  162. }
  163. }
  164. }