NCManageDatabase+Video.swift 6.0 KB

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