NCManageDatabase+Video.swift 6.5 KB

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