NCManageDatabase+Metadata+Session.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. extension NCManageDatabase {
  28. func setMetadataSession(ocId: String,
  29. newFileName: String? = nil,
  30. session: String? = nil,
  31. sessionTaskIdentifier: Int? = nil,
  32. sessionError: String? = nil,
  33. selector: String? = nil,
  34. status: Int? = nil,
  35. etag: String? = nil,
  36. errorCode: Int? = nil) {
  37. do {
  38. let realm = try Realm()
  39. try realm.write {
  40. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first {
  41. if let newFileName = newFileName {
  42. result.fileName = newFileName
  43. result.fileNameView = newFileName
  44. }
  45. if let session {
  46. result.session = session
  47. }
  48. if let sessionTaskIdentifier {
  49. result.sessionTaskIdentifier = sessionTaskIdentifier
  50. }
  51. if let sessionError {
  52. result.sessionError = sessionError
  53. if sessionError.isEmpty {
  54. result.errorCode = 0
  55. }
  56. }
  57. if let selector {
  58. result.sessionSelector = selector
  59. }
  60. if let status {
  61. result.status = status
  62. if status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload {
  63. result.sessionDate = Date()
  64. } else if status == NCGlobal.shared.metadataStatusNormal {
  65. result.sessionDate = nil
  66. }
  67. }
  68. if let etag {
  69. result.etag = etag
  70. }
  71. if let errorCode {
  72. result.errorCode = errorCode
  73. }
  74. }
  75. }
  76. } catch let error {
  77. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  78. }
  79. }
  80. @discardableResult
  81. func setMetadatasSessionInWaitDownload(metadatas: [tableMetadata], session: String, selector: String, sceneIdentifier: String? = nil) -> tableMetadata? {
  82. if metadatas.isEmpty { return nil }
  83. var metadataUpdated: tableMetadata?
  84. do {
  85. let realm = try Realm()
  86. try realm.write {
  87. for metadata in metadatas {
  88. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", metadata.ocId).first {
  89. result.sceneIdentifier = sceneIdentifier
  90. result.session = session
  91. result.sessionTaskIdentifier = 0
  92. result.sessionError = ""
  93. result.sessionSelector = selector
  94. result.status = NCGlobal.shared.metadataStatusWaitDownload
  95. result.sessionDate = Date()
  96. metadataUpdated = tableMetadata(value: result)
  97. } else {
  98. metadata.sceneIdentifier = sceneIdentifier
  99. metadata.session = session
  100. metadata.sessionTaskIdentifier = 0
  101. metadata.sessionError = ""
  102. metadata.sessionSelector = selector
  103. metadata.status = NCGlobal.shared.metadataStatusWaitDownload
  104. metadata.sessionDate = Date()
  105. realm.add(metadata, update: .all)
  106. metadataUpdated = tableMetadata(value: metadata)
  107. }
  108. }
  109. }
  110. } catch let error {
  111. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  112. }
  113. return metadataUpdated
  114. }
  115. func clearMetadataSession(metadatas: [tableMetadata]) {
  116. do {
  117. let realm = try Realm()
  118. try realm.write {
  119. for metadata in metadatas {
  120. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", metadata.ocId).first {
  121. result.sceneIdentifier = nil
  122. result.session = ""
  123. result.sessionTaskIdentifier = 0
  124. result.sessionError = ""
  125. result.sessionSelector = ""
  126. result.sessionDate = nil
  127. result.status = NCGlobal.shared.metadataStatusNormal
  128. }
  129. }
  130. }
  131. } catch let error {
  132. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  133. }
  134. }
  135. func clearMetadataSession(metadata: tableMetadata) {
  136. do {
  137. let realm = try Realm()
  138. try realm.write {
  139. if let result = realm.objects(tableMetadata.self).filter("ocId == %@", metadata.ocId).first {
  140. result.sceneIdentifier = nil
  141. result.session = ""
  142. result.sessionTaskIdentifier = 0
  143. result.sessionError = ""
  144. result.sessionSelector = ""
  145. result.sessionDate = nil
  146. result.status = NCGlobal.shared.metadataStatusNormal
  147. }
  148. }
  149. } catch let error {
  150. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  151. }
  152. }
  153. @discardableResult
  154. func setMetadataStatus(ocId: String, status: Int) -> tableMetadata? {
  155. var result: tableMetadata?
  156. do {
  157. let realm = try Realm()
  158. try realm.write {
  159. result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first
  160. result?.status = status
  161. if status == NCGlobal.shared.metadataStatusNormal {
  162. result?.sessionDate = nil
  163. } else {
  164. result?.sessionDate = Date()
  165. }
  166. }
  167. } catch let error {
  168. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  169. }
  170. if let result {
  171. return tableMetadata.init(value: result)
  172. } else {
  173. return nil
  174. }
  175. }
  176. func getMetadata(from url: URL?, sessionTaskIdentifier: Int) -> tableMetadata? {
  177. guard let url,
  178. var serverUrl = url.deletingLastPathComponent().absoluteString.removingPercentEncoding
  179. else { return nil }
  180. let fileName = url.lastPathComponent
  181. if serverUrl.hasSuffix("/") {
  182. serverUrl = String(serverUrl.dropLast())
  183. }
  184. return getMetadata(predicate: NSPredicate(format: "serverUrl == %@ AND fileName == %@ AND sessionTaskIdentifier == %d",
  185. serverUrl,
  186. fileName,
  187. sessionTaskIdentifier))
  188. }
  189. }