NCManageDatabase+Video.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 = tableVideoV3
  27. class tableVideoV3: Object {
  28. @Persisted var account = ""
  29. @Persisted(primaryKey: true) var ocId = ""
  30. @Persisted var position: Float?
  31. @Persisted var width: Int?
  32. @Persisted var height: Int?
  33. @Persisted var length: Int?
  34. @Persisted var codecNameVideo: String?
  35. @Persisted var codecNameAudio: String?
  36. @Persisted var codecAudioChannelLayout: String?
  37. @Persisted var codecAudioLanguage: String?
  38. @Persisted var codecMaxCompatibility: Bool = false
  39. @Persisted var codecQuality: String?
  40. }
  41. extension NCManageDatabase {
  42. func addVideo(metadata: tableMetadata, position: Float? = nil, width: Int? = nil, height: Int? = nil, 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 position = position {
  49. result.position = position
  50. }
  51. if let width = width {
  52. result.width = width
  53. }
  54. if let height = height {
  55. result.height = height
  56. }
  57. if let length = length {
  58. result.length = length
  59. }
  60. realm.add(result, update: .all)
  61. } else {
  62. let result = tableVideo()
  63. result.account = metadata.account
  64. result.ocId = metadata.ocId
  65. if let position = position {
  66. result.position = position
  67. }
  68. if let width = width {
  69. result.width = width
  70. }
  71. if let height = height {
  72. result.height = height
  73. }
  74. if let length = length {
  75. result.length = length
  76. }
  77. realm.add(result, update: .all)
  78. }
  79. }
  80. } catch let error {
  81. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  82. }
  83. }
  84. func addVideoCodec(metadata: tableMetadata, codecNameVideo: String?, codecNameAudio: String?, codecAudioChannelLayout: String?, codecAudioLanguage: String?, codecMaxCompatibility: Bool, codecQuality: String?) {
  85. let realm = try! Realm()
  86. do {
  87. try realm.write {
  88. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  89. if let codecNameVideo = codecNameVideo { result.codecNameVideo = codecNameVideo }
  90. if let codecNameAudio = codecNameAudio { result.codecNameAudio = codecNameAudio }
  91. if let codecAudioChannelLayout = codecAudioChannelLayout { result.codecAudioChannelLayout = codecAudioChannelLayout }
  92. if let codecAudioLanguage = codecAudioLanguage { result.codecAudioLanguage = codecAudioLanguage }
  93. result.codecMaxCompatibility = codecMaxCompatibility
  94. if let codecQuality = codecQuality { result.codecQuality = codecQuality }
  95. realm.add(result, update: .all)
  96. } else {
  97. let addObject = tableVideo()
  98. addObject.account = metadata.account
  99. addObject.ocId = metadata.ocId
  100. addObject.codecNameVideo = codecNameVideo
  101. addObject.codecNameAudio = codecNameAudio
  102. addObject.codecAudioChannelLayout = codecAudioChannelLayout
  103. addObject.codecAudioLanguage = codecAudioLanguage
  104. addObject.codecMaxCompatibility = codecMaxCompatibility
  105. addObject.codecQuality = codecQuality
  106. realm.add(addObject, update: .all)
  107. }
  108. }
  109. } catch let error {
  110. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  111. }
  112. }
  113. func getVideo(metadata: tableMetadata?) -> tableVideo? {
  114. guard let metadata = metadata else { return nil }
  115. let realm = try! Realm()
  116. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  117. return nil
  118. }
  119. return tableVideo.init(value: result)
  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. }