-
-
Notifications
You must be signed in to change notification settings - Fork 906
/
Copy pathNCManageDatabase.swift
296 lines (269 loc) · 13 KB
/
NCManageDatabase.swift
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// NCManageDatabase.swift
// Nextcloud
//
// Created by Marino Faggiana on 06/05/17.
// Copyright © 2017 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
// Author Henrik Storch <[email protected]>
//
// 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 UIKit
import RealmSwift
import NextcloudKit
import CoreMedia
import Photos
import CommonCrypto
protocol DateCompareable {
var dateKey: Date { get }
}
class NCManageDatabase: NSObject {
@objc static let shared: NCManageDatabase = {
let instance = NCManageDatabase()
return instance
}()
let utilityFileSystem = NCUtilityFileSystem()
override init() {
func migrationSchema(_ migration: Migration, _ oldSchemaVersion: UInt64) {
if oldSchemaVersion < 365 {
migration.deleteData(forType: tableMetadata.className())
migration.enumerateObjects(ofType: tableDirectory.className()) { _, newObject in
newObject?["etag"] = ""
}
}
}
func compactDB(_ totalBytes: Int, _ usedBytes: Int) -> Bool {
let usedPercentage = (Double(usedBytes) / Double(totalBytes)) * 100
/// Compact the database if more than 25% of the space is free
let shouldCompact = (usedPercentage < 75.0) && (totalBytes > 100 * 1024 * 1024)
return shouldCompact
}
var realm: Realm?
let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
let databaseFileUrlPath = dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName)
let bundleUrl: URL = Bundle.main.bundleURL
let bundlePathExtension: String = bundleUrl.pathExtension
let bundleFileName: String = (bundleUrl.path as NSString).lastPathComponent
let isAppex: Bool = bundlePathExtension == "appex"
var objectTypesAppex = [NCKeyValue.self,
tableMetadata.self,
tableLocalFile.self,
tableDirectory.self,
tableTag.self,
tableAccount.self,
tableCapabilities.self,
tablePhotoLibrary.self,
tableE2eEncryption.self,
tableE2eEncryptionLock.self,
tableE2eMetadata12.self,
tableE2eMetadata.self,
tableE2eUsers.self,
tableE2eCounter.self,
tableShare.self,
tableChunk.self,
tableAvatar.self,
tableDashboardWidget.self,
tableDashboardWidgetButton.self,
NCDBLayoutForView.self,
TableSecurityGuardDiagnostics.self]
// Disable file protection for directory DB
// https://docs.mongodb.com/realm/sdk/ios/examples/configure-and-open-a-realm/#std-label-ios-open-a-local-realm
if let folderPathURL = dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud) {
let folderPath = folderPathURL.path
do {
try FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication], ofItemAtPath: folderPath)
} catch {
print("Dangerous error")
}
}
if isAppex {
if bundleFileName == "File Provider Extension.appex" {
objectTypesAppex = [NCKeyValue.self,
tableMetadata.self,
tableLocalFile.self,
tableDirectory.self,
tableTag.self,
tableAccount.self,
tableCapabilities.self,
tableE2eEncryption.self]
}
do {
Realm.Configuration.defaultConfiguration =
Realm.Configuration(fileURL: databaseFileUrlPath,
schemaVersion: databaseSchemaVersion,
migrationBlock: { migration, oldSchemaVersion in
migrationSchema(migration, oldSchemaVersion)
}, shouldCompactOnLaunch: { totalBytes, usedBytes in
compactDB(totalBytes, usedBytes)
}, objectTypes: objectTypesAppex)
realm = try Realm()
if let realm, let url = realm.configuration.fileURL {
print("Realm is located at: \(url)")
}
} catch let error {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] DATABASE ERROR: \(error.localizedDescription)")
}
} else {
do {
Realm.Configuration.defaultConfiguration =
Realm.Configuration(fileURL: databaseFileUrlPath,
schemaVersion: databaseSchemaVersion,
migrationBlock: { migration, oldSchemaVersion in
migrationSchema(migration, oldSchemaVersion)
}, shouldCompactOnLaunch: { totalBytes, usedBytes in
compactDB(totalBytes, usedBytes)
})
realm = try Realm()
if let realm, let url = realm.configuration.fileURL {
print("Realm is located at: \(url)")
}
} catch let error {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] DATABASE ERROR: \(error.localizedDescription)")
}
}
}
// MARK: -
// MARK: Utility Database
func clearTable(_ table: Object.Type, account: String? = nil) {
do {
let realm = try Realm()
try realm.write {
var results: Results<Object>
if let account = account {
results = realm.objects(table).filter("account == %@", account)
} else {
results = realm.objects(table)
}
realm.delete(results)
}
} catch let error {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
}
}
func clearDatabase(account: String? = nil, removeAccount: Bool = false) {
if removeAccount {
self.clearTable(tableAccount.self, account: account)
}
self.clearTable(tableActivity.self, account: account)
self.clearTable(tableActivityLatestId.self, account: account)
self.clearTable(tableActivityPreview.self, account: account)
self.clearTable(tableActivitySubjectRich.self, account: account)
self.clearTable(tableAvatar.self)
self.clearTable(tableCapabilities.self, account: account)
self.clearTable(tableChunk.self, account: account)
self.clearTable(tableComments.self, account: account)
self.clearTable(tableDashboardWidget.self, account: account)
self.clearTable(tableDashboardWidgetButton.self, account: account)
self.clearTable(tableDirectEditingCreators.self, account: account)
self.clearTable(tableDirectEditingEditors.self, account: account)
self.clearTable(tableDirectory.self, account: account)
self.clearTablesE2EE(account: account)
self.clearTable(tableExternalSites.self, account: account)
self.clearTable(tableGPS.self, account: nil)
self.clearTable(TableGroupfolders.self, account: account)
self.clearTable(TableGroupfoldersGroups.self, account: account)
self.clearTable(tableLocalFile.self, account: account)
self.clearTable(tableMetadata.self, account: account)
self.clearTable(tablePhotoLibrary.self, account: account)
self.clearTable(tableShare.self, account: account)
self.clearTable(TableSecurityGuardDiagnostics.self, account: account)
self.clearTable(tableTag.self, account: account)
self.clearTable(tableTrash.self, account: account)
self.clearTable(tableUserStatus.self, account: account)
self.clearTable(tableVideo.self, account: account)
self.clearTable(tableRecommendedFiles.self, account: account)
}
func clearTablesE2EE(account: String?) {
self.clearTable(tableE2eEncryption.self, account: account)
self.clearTable(tableE2eEncryptionLock.self, account: account)
self.clearTable(tableE2eMetadata12.self, account: account)
self.clearTable(tableE2eMetadata.self, account: account)
self.clearTable(tableE2eUsers.self, account: account)
self.clearTable(tableE2eCounter.self, account: account)
}
func getThreadConfined(_ object: Object) -> Any {
return ThreadSafeReference(to: object)
}
func putThreadConfined(_ tableRef: ThreadSafeReference<Object>) -> Object? {
do {
let realm = try Realm()
return realm.resolve(tableRef)
} catch let error as NSError {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
}
return nil
}
func realmRefresh() {
do {
let realm = try Realm()
realm.refresh()
} catch let error as NSError {
NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not refresh database: \(error)")
}
}
func sha256Hash(_ input: String) -> String {
let data = Data(input.utf8)
var digest = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &digest)
}
return digest.map { String(format: "%02hhx", $0) }.joined()
}
// MARK: -
// MARK: Func T
func fetchPagedResults<T: Object>(ofType type: T.Type, primaryKey: String, recordsPerPage: Int, pageNumber: Int, filter: NSPredicate? = nil, sortedByKeyPath: String? = nil, sortedAscending: Bool = true) -> Results<T>? {
let startIndex = recordsPerPage * (pageNumber - 1)
do {
let realm = try Realm()
var results = realm.objects(type)
if let filter, let sortedByKeyPath {
results = results.filter(filter).sorted(byKeyPath: sortedByKeyPath, ascending: sortedAscending)
}
guard startIndex < results.count else {
return nil
}
let pagedResults = results.dropFirst(startIndex).prefix(recordsPerPage)
let pagedResultsKeys = pagedResults.compactMap { $0.value(forKey: primaryKey) as? String }
return realm.objects(type).filter("\(primaryKey) IN %@", Array(pagedResultsKeys))
} catch {
print("Error opening Realm: \(error)")
return nil
}
}
// MARK: -
// MARK: SWIFTUI PREVIEW
func previewCreateDB() {
/// Account
let account = "marinofaggiana https://cloudtest.nextcloud.com"
let account2 = "mariorossi https://cloudtest.nextcloud.com"
addAccount(account, urlBase: "https://cloudtest.nextcloud.com", user: "marinofaggiana", userId: "marinofaggiana", password: "password")
addAccount(account2, urlBase: "https://cloudtest.nextcloud.com", user: "mariorossi", userId: "mariorossi", password: "password")
let userProfile = NKUserProfile()
userProfile.displayName = "Marino Faggiana"
userProfile.address = "Hirschstrasse 26, 70192 Stuttgart, Germany"
userProfile.phone = "+49 (711) 252 428 - 90"
setAccountUserProfile(account: account, userProfile: userProfile)
let userProfile2 = NKUserProfile()
userProfile2.displayName = "Mario Rossi"
setAccountUserProfile(account: account2, userProfile: userProfile2)
}
}
class NCKeyValue: Object {
@Persisted var key: String = ""
@Persisted var value: String?
}