NCManageDatabase+Video.swift 6.0 KB

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