NCManageDatabase+Video.swift 6.4 KB

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