2023-01-05 22:59:50 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@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 2 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RealmSwift
|
2023-01-10 20:36:10 +03:00
|
|
|
import FileProvider
|
2023-01-05 22:59:50 +03:00
|
|
|
|
|
|
|
class NextcloudFilesDatabaseManager : NSObject {
|
|
|
|
static let shared = {
|
|
|
|
return NextcloudFilesDatabaseManager();
|
|
|
|
}()
|
|
|
|
|
2023-01-12 18:51:44 +03:00
|
|
|
let relativeDatabaseFolderPath: String = "Database/"
|
2023-01-05 22:59:50 +03:00
|
|
|
let databaseFilename: String = "fileproviderextdatabase.realm"
|
|
|
|
let relativeDatabaseFilePath: String
|
|
|
|
var databasePath: URL?
|
|
|
|
|
|
|
|
let schemaVersion: UInt64 = 100
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
self.relativeDatabaseFilePath = self.relativeDatabaseFolderPath + self.databaseFilename
|
|
|
|
|
2023-01-12 18:51:44 +03:00
|
|
|
guard let fileProviderDataDirUrl = pathForFileProviderExtData() else {
|
2023-01-05 22:59:50 +03:00
|
|
|
super.init()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:51:44 +03:00
|
|
|
self.databasePath = fileProviderDataDirUrl.appendingPathExtension(self.relativeDatabaseFilePath)
|
2023-01-05 22:59:50 +03:00
|
|
|
|
|
|
|
// 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
|
2023-01-12 18:51:44 +03:00
|
|
|
let folderPath = fileProviderDataDirUrl.appendingPathComponent(self.relativeDatabaseFolderPath).path
|
|
|
|
do {
|
|
|
|
try FileManager.default.setAttributes([FileAttributeKey.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication], ofItemAtPath: folderPath)
|
|
|
|
} catch {
|
|
|
|
print("Could not set permission level for File Provider database folder")
|
2023-01-05 22:59:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let config = Realm.Configuration(
|
|
|
|
fileURL: self.databasePath,
|
|
|
|
schemaVersion: self.schemaVersion,
|
2023-01-12 23:16:49 +03:00
|
|
|
objectTypes: [NextcloudItemMetadataTable.self]
|
2023-01-05 22:59:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
Realm.Configuration.defaultConfiguration = config
|
|
|
|
|
|
|
|
do {
|
|
|
|
let realm = try Realm()
|
|
|
|
print("Successfully started Realm db for FileProviderExt")
|
|
|
|
} catch let error as NSError {
|
|
|
|
print("Error opening Realm db: %@", error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
}
|
2023-01-10 19:58:00 +03:00
|
|
|
|
2023-01-10 21:23:33 +03:00
|
|
|
private func ncDatabase() -> Realm {
|
2023-01-10 19:58:00 +03:00
|
|
|
let realm = try! Realm()
|
|
|
|
realm.refresh()
|
2023-01-10 21:23:33 +03:00
|
|
|
return realm
|
2023-01-10 19:58:00 +03:00
|
|
|
}
|
2023-01-10 20:36:10 +03:00
|
|
|
|
2023-01-12 23:16:49 +03:00
|
|
|
func itemMetadataFromOcId(_ ocId: String) -> NextcloudItemMetadataTable? {
|
|
|
|
return ncDatabase().objects(NextcloudItemMetadataTable.self).filter("ocId == %@", ocId).first
|
2023-01-10 21:23:33 +03:00
|
|
|
}
|
|
|
|
|
2023-01-13 16:17:04 +03:00
|
|
|
func itemMetadatas(account: String, serverUrl: String) -> [NextcloudItemMetadataTable] {
|
|
|
|
let metadatas = ncDatabase().objects(NextcloudItemMetadataTable.self).filter("account == %@ AND serverUrl == %@", account, serverUrl)
|
|
|
|
let sortedMetadatas = metadatas.sorted(byKeyPath: "fileName", ascending: true)
|
|
|
|
return Array(sortedMetadatas.map { $0 })
|
|
|
|
}
|
|
|
|
|
2023-01-12 23:16:49 +03:00
|
|
|
func itemMetadataFromFileProviderItemIdentifier(_ identifier: NSFileProviderItemIdentifier) -> NextcloudItemMetadataTable? {
|
2023-01-10 20:36:10 +03:00
|
|
|
let ocId = identifier.rawValue
|
2023-01-12 23:16:49 +03:00
|
|
|
return itemMetadataFromOcId(ocId)
|
2023-01-10 20:36:10 +03:00
|
|
|
}
|
2023-01-10 22:24:34 +03:00
|
|
|
|
2023-01-13 03:59:50 +03:00
|
|
|
func directoryMetadata(account: String, serverUrl: String) -> NextcloudDirectoryMetadataTable? {
|
|
|
|
return ncDatabase().objects(NextcloudDirectoryMetadataTable.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
|
|
|
|
}
|
|
|
|
|
2023-01-12 23:16:49 +03:00
|
|
|
func parentDirectoryMetadataForItem(_ itemMetadata: NextcloudItemMetadataTable) -> NextcloudDirectoryMetadataTable? {
|
2023-01-13 03:59:50 +03:00
|
|
|
return directoryMetadata(account: itemMetadata.account, serverUrl: itemMetadata.serverUrl)
|
2023-01-10 22:24:34 +03:00
|
|
|
}
|
2023-01-12 23:36:51 +03:00
|
|
|
|
|
|
|
func localFileMetadataFromOcId(_ ocId: String) -> NextcloudLocalFileMetadataTable? {
|
|
|
|
return ncDatabase().objects(NextcloudLocalFileMetadataTable.self).filter("ocId == %@", ocId).first
|
|
|
|
}
|
2023-01-05 22:59:50 +03:00
|
|
|
}
|