1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
//
// NCManageDatabase+Video.swift
// Nextcloud
//
// Created by Marino Faggiana on 15/03/22.
// Copyright © 2022 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import RealmSwift
import NextcloudKit
extension NCManageDatabase {
func addVideoTime(metadata: tableMetadata, time: CMTime?, durationTime: CMTime?) {
if metadata.livePhoto { return }
let realm = try! Realm()
do {
try realm.safeWrite {
if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
if let durationTime = durationTime {
result.duration = durationTime.convertScale(1000, method: .default).value
}
if let time = time {
result.time = time.convertScale(1000, method: .default).value
}
realm.add(result, update: .all)
} else {
let addObject = tableVideo()
addObject.account = metadata.account
if let durationTime = durationTime {
addObject.duration = durationTime.convertScale(1000, method: .default).value
}
addObject.ocId = metadata.ocId
if let time = time {
addObject.time = time.convertScale(1000, method: .default).value
}
realm.add(addObject, update: .all)
}
}
} catch let error {
NKCommon.shared.writeLog("Could not write to database: \(error)")
}
}
func addVideoCodec(metadata: tableMetadata, codecNameVideo: String?, codecNameAudio: String?, codecAudioChannelLayout: String?, codecAudioLanguage: String?, codecMaxCompatibility: Bool, codecQuality: String?) {
let realm = try! Realm()
do {
try realm.safeWrite {
if let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first {
if let codecNameVideo = codecNameVideo { result.codecNameVideo = codecNameVideo }
if let codecNameAudio = codecNameAudio { result.codecNameAudio = codecNameAudio }
if let codecAudioChannelLayout = codecAudioChannelLayout { result.codecAudioChannelLayout = codecAudioChannelLayout }
if let codecAudioLanguage = codecAudioLanguage { result.codecAudioLanguage = codecAudioLanguage }
result.codecMaxCompatibility = codecMaxCompatibility
if let codecQuality = codecQuality { result.codecQuality = codecQuality }
realm.add(result, update: .all)
} else {
let addObject = tableVideo()
addObject.account = metadata.account
addObject.ocId = metadata.ocId
addObject.codecNameVideo = codecNameVideo
addObject.codecNameAudio = codecNameAudio
addObject.codecAudioChannelLayout = codecAudioChannelLayout
addObject.codecAudioLanguage = codecAudioLanguage
addObject.codecMaxCompatibility = codecMaxCompatibility
addObject.codecQuality = codecQuality
realm.add(addObject, update: .all)
}
}
} catch let error {
NKCommon.shared.writeLog("Could not write to database: \(error)")
}
}
func getVideo(metadata: tableMetadata?) -> tableVideo? {
guard let metadata = metadata else { return nil }
let realm = try! Realm()
guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
return nil
}
return tableVideo.init(value: result)
}
func getVideoDurationTime(metadata: tableMetadata?) -> CMTime? {
guard let metadata = metadata else { return nil }
if metadata.livePhoto { return nil }
let realm = try! Realm()
guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
return nil
}
if result.duration == 0 { return nil }
let duration = CMTimeMake(value: result.duration, timescale: 1000)
return duration
}
func getVideoTime(metadata: tableMetadata) -> CMTime? {
if metadata.livePhoto { return nil }
let realm = try! Realm()
guard let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId).first else {
return nil
}
if result.time == 0 { return nil }
let time = CMTimeMake(value: result.time, timescale: 1000)
return time
}
func deleteVideo(metadata: tableMetadata) {
let realm = try! Realm()
do {
try realm.safeWrite {
let result = realm.objects(tableVideo.self).filter("account == %@ AND ocId == %@", metadata.account, metadata.ocId)
realm.delete(result)
}
} catch let error {
NKCommon.shared.writeLog("Could not write to database: \(error)")
}
}
}
|