Allow NextcloudFilesDatabaseManager to take a specific realmconfig in constructor
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 15 Apr 2024 07:44:10 +0000 (15:44 +0800)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Wed, 17 Apr 2024 08:44:12 +0000 (08:44 +0000)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager.swift

index c2290b2271c5bba3fd37a60f601b65ed55187c69..06bdc266fac98f0db4b88c22f3ef4ce9f5ff0da9 100644 (file)
@@ -19,57 +19,50 @@ import OSLog
 import RealmSwift
 
 class NextcloudFilesDatabaseManager {
-    static let shared = NextcloudFilesDatabaseManager()
+    static let shared = NextcloudFilesDatabaseManager()!
 
-    let relativeDatabaseFolderPath = "Database/"
-    let databaseFilename = "fileproviderextdatabase.realm"
-    let relativeDatabaseFilePath: String
-    var databasePath: URL?
+    private static let relativeDatabaseFolderPath = "Database/"
+    private static let databaseFilename = "fileproviderextdatabase.realm"
+    private static let schemaVersion: UInt64 = 100
 
-    let schemaVersion: UInt64 = 100
+    init(realmConfig: Realm.Configuration = Realm.Configuration.defaultConfiguration) {
+        Realm.Configuration.defaultConfiguration = realmConfig
 
-    override init() {
-        relativeDatabaseFilePath = relativeDatabaseFolderPath + databaseFilename
-
-        guard let fileProviderDataDirUrl = pathForFileProviderExtData() else { return }
+        do {
+            _ = try Realm()
+            Logger.ncFilesDatabase.info("Successfully started Realm db for FileProviderExt")
+        } catch let error {
+            Logger.ncFilesDatabase.error("Error opening Realm db: \(error, privacy: .public)")
+        }
+    }
 
-        databasePath = fileProviderDataDirUrl.appendingPathComponent(relativeDatabaseFilePath)
+    convenience init?() {
+        let relativeDatabaseFilePath = Self.relativeDatabaseFolderPath + Self.databaseFilename
+        guard let fileProviderDataDirUrl = pathForFileProviderExtData() else { return nil }
+        let databasePath = fileProviderDataDirUrl.appendingPathComponent(relativeDatabaseFilePath)
 
         // 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
-        let dbFolder = fileProviderDataDirUrl.appendingPathComponent(relativeDatabaseFolderPath)
+        // https://docs.mongodb.com/realm/sdk/ios/examples/configure-and-open-a-realm/
+        let dbFolder = fileProviderDataDirUrl.appendingPathComponent(Self.relativeDatabaseFolderPath)
         let dbFolderPath = dbFolder.path
         do {
             try FileManager.default.createDirectory(at: dbFolder, withIntermediateDirectories: true)
             try FileManager.default.setAttributes(
-                [
-                    FileAttributeKey.protectionKey: FileProtectionType
-                        .completeUntilFirstUserAuthentication
-                ],
-                ofItemAtPath: dbFolderPath)
+                [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication],
+                ofItemAtPath: dbFolderPath
+            )
         } catch {
             Logger.ncFilesDatabase.error(
-                "Could not set permission level for File Provider database folder, received error: \(error.localizedDescription, privacy: .public)"
+                "Could not set permission level for db folder: \(error, privacy: .public)"
             )
         }
 
         let config = Realm.Configuration(
             fileURL: databasePath,
-            schemaVersion: schemaVersion,
+            schemaVersion: Self.schemaVersion,
             objectTypes: [NextcloudItemMetadataTable.self, NextcloudLocalFileMetadataTable.self]
         )
-
-        Realm.Configuration.defaultConfiguration = config
-
-        do {
-            _ = try Realm()
-            Logger.ncFilesDatabase.info("Successfully started Realm db for FileProviderExt")
-        } catch let error as NSError {
-            Logger.ncFilesDatabase.error(
-                "Error opening Realm db: \(error.localizedDescription, privacy: .public)")
-        }
-
-        super.init()
+        self.init(realmConfig: config)
     }
 
     func ncDatabase() -> Realm {