NCManageDatabase+Video.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 duration: Int64 = 0
  29. @objc dynamic var ocId = ""
  30. @objc dynamic var time: Int64 = 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. func addVideoTime(metadata: tableMetadata, time: CMTime?, durationTime: CMTime?) {
  41. if metadata.livePhoto { return }
  42. let realm = try! Realm()
  43. do {
  44. try realm.write {
  45. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  46. if let durationTime = durationTime {
  47. result.duration = durationTime.convertScale(1000, method: .default).value
  48. }
  49. if let time = time {
  50. result.time = time.convertScale(1000, method: .default).value
  51. }
  52. realm.add(result, update: .all)
  53. } else {
  54. let addObject = tableVideo()
  55. addObject.account = metadata.account
  56. if let durationTime = durationTime {
  57. addObject.duration = durationTime.convertScale(1000, method: .default).value
  58. }
  59. addObject.ocId = metadata.ocId
  60. if let time = time {
  61. addObject.time = time.convertScale(1000, method: .default).value
  62. }
  63. realm.add(addObject, update: .all)
  64. }
  65. }
  66. } catch let error {
  67. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  68. }
  69. }
  70. func addVideoCodec(metadata: tableMetadata, codecNameVideo: String?, codecNameAudio: String?, codecAudioChannelLayout: String?, codecAudioLanguage: String?, codecMaxCompatibility: Bool, codecQuality: String?) {
  71. let realm = try! Realm()
  72. do {
  73. try realm.write {
  74. if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
  75. if let codecNameVideo = codecNameVideo { result.codecNameVideo = codecNameVideo }
  76. if let codecNameAudio = codecNameAudio { result.codecNameAudio = codecNameAudio }
  77. if let codecAudioChannelLayout = codecAudioChannelLayout { result.codecAudioChannelLayout = codecAudioChannelLayout }
  78. if let codecAudioLanguage = codecAudioLanguage { result.codecAudioLanguage = codecAudioLanguage }
  79. result.codecMaxCompatibility = codecMaxCompatibility
  80. if let codecQuality = codecQuality { result.codecQuality = codecQuality }
  81. realm.add(result, update: .all)
  82. } else {
  83. let addObject = tableVideo()
  84. addObject.account = metadata.account
  85. addObject.ocId = metadata.ocId
  86. addObject.codecNameVideo = codecNameVideo
  87. addObject.codecNameAudio = codecNameAudio
  88. addObject.codecAudioChannelLayout = codecAudioChannelLayout
  89. addObject.codecAudioLanguage = codecAudioLanguage
  90. addObject.codecMaxCompatibility = codecMaxCompatibility
  91. addObject.codecQuality = codecQuality
  92. realm.add(addObject, update: .all)
  93. }
  94. }
  95. } catch let error {
  96. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  97. }
  98. }
  99. func getVideo(metadata: tableMetadata?) -> tableVideo? {
  100. guard let metadata = metadata else { return nil }
  101. let realm = try! Realm()
  102. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  103. return nil
  104. }
  105. return tableVideo.init(value: result)
  106. }
  107. func getVideoDurationTime(metadata: tableMetadata?) -> CMTime? {
  108. guard let metadata = metadata else { return nil }
  109. if metadata.livePhoto { return nil }
  110. let realm = try! Realm()
  111. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  112. return nil
  113. }
  114. if result.duration == 0 { return nil }
  115. let duration = CMTimeMake(value: result.duration, timescale: 1000)
  116. return duration
  117. }
  118. func getVideoTime(metadata: tableMetadata) -> CMTime? {
  119. if metadata.livePhoto { return nil }
  120. let realm = try! Realm()
  121. guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
  122. return nil
  123. }
  124. if result.time == 0 { return nil }
  125. let time = CMTimeMake(value: result.time, timescale: 1000)
  126. return time
  127. }
  128. func deleteVideo(metadata: tableMetadata) {
  129. let realm = try! Realm()
  130. do {
  131. try realm.write {
  132. let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId)
  133. realm.delete(result)
  134. }
  135. } catch let error {
  136. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  137. }
  138. }
  139. }