NCManageDatabase+Video.swift 5.4 KB

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