NCManageDatabase+Video.swift 6.2 KB

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