NCManageDatabase+Video.swift 6.7 KB

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