NCManageDatabase+Video.swift 7.1 KB

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