Files
Marino Faggiana 7ab5ebf959 feat: add GRDB state for background auto upload (#4392)
* feat: add GRDB state for background auto upload

Track photo and video progress per asset, destination and upload session,
preserving confirmed components and assigned pair names on repeated writes.
Add the SQLite table and unit tests, and normalize the PhotoLibraryAsset
source filename. Integration with background upload discovery follows later.

Assisted-by: Codex:GPT-6
Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* wip

Signed-off-by: Marino Faggiana <[email protected]>

* version

Signed-off-by: Marino Faggiana <[email protected]>

* Fix Test

Signed-off-by: Marino Faggiana <[email protected]>

---------

Signed-off-by: Marino Faggiana <[email protected]>
2026-10-13 07:26:29 +02:00

66 lines
2.7 KiB
Swift

// SPDX-FileCopyrightText: Nextcloud GmbH
// SPDX-FileCopyrightText: 2026 Marino Faggiana
// SPDX-License-Identifier: GPL-3.0-or-later
import Foundation
import NextcloudKit
import RealmSwift
import OSLog
final class NCManageDatabase {
static let shared = NCManageDatabase()
internal let core: NCManageDatabaseCore
internal let databaseURL: URL?
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "BackgroundUploadExtension", category: NCGlobal.shared.logTagBackgroundUpload)
/// Creates the extension database facade and resolves the Realm file inside the shared app group.
/// A missing app-group container leaves `databaseURL` unset and causes `openRealm()` to fail safely.
private init() {
self.core = NCManageDatabaseCore()
if let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroup) {
self.databaseURL = dirGroup
.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud)
.appendingPathComponent(databaseName)
} else {
self.databaseURL = nil
}
}
/// Validates the shared Realm schema before installing and opening the extension configuration.
/// Returns `false` without migrating when the database is unavailable, incompatible, or cannot open.
@discardableResult
func openRealm() -> Bool {
guard let databaseURL,
let schemaVersion = try? schemaVersionAtURL(databaseURL),
schemaVersion == databaseSchemaVersion else {
logger.error("Realm was not opened because its schema version does not match the extension")
return false
}
do {
let configuration = Realm.Configuration(
fileURL: databaseURL,
schemaVersion: databaseSchemaVersion,
objectTypes: [
NCKeyValue.self, tableMetadata.self, tableLocalFile.self, tableMetadataTag.self, tableLivePhoto.self,
tableDirectory.self, tableAccount.self, tableCapabilities.self
]
)
Realm.Configuration.defaultConfiguration = configuration
let realm = try Realm(configuration: configuration)
if let url = realm.configuration.fileURL {
logger.notice("Realm is located at: \(url.path, privacy: .public)")
}
return true
} catch let error {
logger.error("Realm error: \(error.localizedDescription, privacy: .public)")
nkLog(tag: NCGlobal.shared.logTagBackgroundUpload, emoji: .error, message: "Realm error: \(error)")
isSuspendingDatabaseOperation = true
return false
}
}
}