NCManageDatabase+Video.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 = tableVideoV4
  27. class tableVideoV4: 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. @Persisted var currentAudioTrackIndex: Int?
  41. @Persisted var currentVideoSubTitleIndex: Int?
  42. }
  43. extension NCManageDatabase {
  44. func addVideo(metadata: tableMetadata, position: Float? = nil, width: Int? = nil, height: Int? = nil, length: Int? = nil, currentAudioTrackIndex: Int? = nil, currentVideoSubTitleIndex: Int? = nil) {
  45. if metadata.livePhoto { return }
  46. do {
  47. let realm = try Realm()
  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. if let currentAudioTrackIndex = currentAudioTrackIndex {
  63. result.currentAudioTrackIndex = currentAudioTrackIndex
  64. }
  65. if let currentVideoSubTitleIndex = currentVideoSubTitleIndex {
  66. result.currentVideoSubTitleIndex = currentVideoSubTitleIndex
  67. }
  68. realm.add(result, update: .all)
  69. } else {
  70. let result = tableVideo()
  71. result.account = metadata.account
  72. result.ocId = metadata.ocId
  73. if let position = position {
  74. result.position = position
  75. }
  76. if let width = width {
  77. result.width = width
  78. }
  79. if let height = height {
  80. result.height = height
  81. }
  82. if let length = length {
  83. result.length = length
  84. }
  85. if let currentAudioTrackIndex = currentAudioTrackIndex {
  86. result.currentAudioTrackIndex = currentAudioTrackIndex
  87. }
  88. if let currentVideoSubTitleIndex = currentVideoSubTitleIndex {
  89. result.currentVideoSubTitleIndex = currentVideoSubTitleIndex
  90. }
  91. realm.add(result, update: .all)
  92. }
  93. }
  94. } catch let error {
  95. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  96. }
  97. }
  98. func addVideoCodec(metadata: tableMetadata, codecNameVideo: String?, codecNameAudio: String?, codecAudioChannelLayout: String?, codecAudioLanguage: String?, codecMaxCompatibility: Bool, codecQuality: String?) {
  99. do {
  100. let realm = try Realm()
  101. try realm.write {
  102. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  103. if let codecNameVideo = codecNameVideo { result.codecNameVideo = codecNameVideo }
  104. if let codecNameAudio = codecNameAudio { result.codecNameAudio = codecNameAudio }
  105. if let codecAudioChannelLayout = codecAudioChannelLayout { result.codecAudioChannelLayout = codecAudioChannelLayout }
  106. if let codecAudioLanguage = codecAudioLanguage { result.codecAudioLanguage = codecAudioLanguage }
  107. result.codecMaxCompatibility = codecMaxCompatibility
  108. if let codecQuality = codecQuality { result.codecQuality = codecQuality }
  109. realm.add(result, update: .all)
  110. } else {
  111. let addObject = tableVideo()
  112. addObject.account = metadata.account
  113. addObject.ocId = metadata.ocId
  114. addObject.codecNameVideo = codecNameVideo
  115. addObject.codecNameAudio = codecNameAudio
  116. addObject.codecAudioChannelLayout = codecAudioChannelLayout
  117. addObject.codecAudioLanguage = codecAudioLanguage
  118. addObject.codecMaxCompatibility = codecMaxCompatibility
  119. addObject.codecQuality = codecQuality
  120. realm.add(addObject, update: .all)
  121. }
  122. }
  123. } catch let error {
  124. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  125. }
  126. }
  127. func getVideo(metadata: tableMetadata?) -> tableVideo? {
  128. guard let metadata = metadata else { return nil }
  129. do {
  130. let realm = try Realm()
  131. realm.refresh()
  132. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else { return nil }
  133. return tableVideo.init(value: result)
  134. } catch let error as NSError {
  135. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  136. }
  137. return nil
  138. }
  139. func deleteVideo(metadata: tableMetadata) {
  140. do {
  141. let realm = try Realm()
  142. try realm.write {
  143. let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId)
  144. realm.delete(result)
  145. }
  146. } catch let error {
  147. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  148. }
  149. }
  150. }