NCManageDatabase+Video.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // NCManageDatabase+Video.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 15/03/22.
  6. // Copyright © 2022 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. typealias tableVideo = tableVideoV2
  27. class tableVideoV2: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var ocId = ""
  30. @objc dynamic var position: Float = 0
  31. @objc dynamic var width: Int = 0
  32. @objc dynamic var height: Int = 0
  33. @objc dynamic var length: Int = 0
  34. @objc dynamic var codecNameVideo: String?
  35. @objc dynamic var codecNameAudio: String?
  36. @objc dynamic var codecAudioChannelLayout: String?
  37. @objc dynamic var codecAudioLanguage: String?
  38. @objc dynamic var codecMaxCompatibility: Bool = false
  39. @objc dynamic var codecQuality: String?
  40. override static func primaryKey() -> String {
  41. return "ocId"
  42. }
  43. }
  44. extension NCManageDatabase {
  45. func addVideo(metadata: tableMetadata, position: Float? = nil, width: Int? = nil, height: Int? = nil, length: Int? = nil) {
  46. if metadata.livePhoto { return }
  47. let realm = try! Realm()
  48. do {
  49. try realm.write {
  50. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  51. if let position = position {
  52. result.position = position
  53. }
  54. if let width = width {
  55. result.width = width
  56. }
  57. if let height = height {
  58. result.height = height
  59. }
  60. if let length = length {
  61. result.length = length
  62. }
  63. realm.add(result, update: .all)
  64. } else {
  65. let result = tableVideo()
  66. result.account = metadata.account
  67. result.ocId = metadata.ocId
  68. if let position = position {
  69. result.position = position
  70. }
  71. if let width = width {
  72. result.width = width
  73. }
  74. if let height = height {
  75. result.height = height
  76. }
  77. if let length = length {
  78. result.length = length
  79. }
  80. realm.add(result, update: .all)
  81. }
  82. }
  83. } catch let error {
  84. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  85. }
  86. }
  87. func addVideoCodec(metadata: tableMetadata, codecNameVideo: String?, codecNameAudio: String?, codecAudioChannelLayout: String?, codecAudioLanguage: String?, codecMaxCompatibility: Bool, codecQuality: String?) {
  88. let realm = try! Realm()
  89. do {
  90. try realm.write {
  91. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  92. if let codecNameVideo = codecNameVideo { result.codecNameVideo = codecNameVideo }
  93. if let codecNameAudio = codecNameAudio { result.codecNameAudio = codecNameAudio }
  94. if let codecAudioChannelLayout = codecAudioChannelLayout { result.codecAudioChannelLayout = codecAudioChannelLayout }
  95. if let codecAudioLanguage = codecAudioLanguage { result.codecAudioLanguage = codecAudioLanguage }
  96. result.codecMaxCompatibility = codecMaxCompatibility
  97. if let codecQuality = codecQuality { result.codecQuality = codecQuality }
  98. realm.add(result, update: .all)
  99. } else {
  100. let addObject = tableVideo()
  101. addObject.account = metadata.account
  102. addObject.ocId = metadata.ocId
  103. addObject.codecNameVideo = codecNameVideo
  104. addObject.codecNameAudio = codecNameAudio
  105. addObject.codecAudioChannelLayout = codecAudioChannelLayout
  106. addObject.codecAudioLanguage = codecAudioLanguage
  107. addObject.codecMaxCompatibility = codecMaxCompatibility
  108. addObject.codecQuality = codecQuality
  109. realm.add(addObject, update: .all)
  110. }
  111. }
  112. } catch let error {
  113. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  114. }
  115. }
  116. func getVideo(metadata: tableMetadata?) -> tableVideo? {
  117. guard let metadata = metadata else { return nil }
  118. let realm = try! Realm()
  119. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  120. return nil
  121. }
  122. return tableVideo.init(value: result)
  123. }
  124. func getVideoPosition(metadata: tableMetadata) -> Float? {
  125. if metadata.livePhoto { return nil }
  126. let realm = try! Realm()
  127. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  128. return nil
  129. }
  130. if result.position == 0 { return nil }
  131. return result.position
  132. }
  133. func deleteVideo(metadata: tableMetadata) {
  134. let realm = try! Realm()
  135. do {
  136. try realm.write {
  137. let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId)
  138. realm.delete(result)
  139. }
  140. } catch let error {
  141. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  142. }
  143. }
  144. }