Skip to content

Commit 37b1a3d

Browse files
committed
feat: Supporting file lock types.
- Removed unnecessary wrapper implementation and use new file locking method from NextcloudKit directly. - Improved logging in Item+LockFile.swift based on recently introduced logging system. - Introduced new key to FileProviderLogDetailKey for NKLock objects. - Introduced SchemaVersion enum for cleaner references to Realm schema versions. - Introduced lockToken property to data models representing an item. Signed-off-by: Iva Horn <iva.horn@icloud.com>
1 parent 8add813 commit 37b1a3d

14 files changed

Lines changed: 198 additions & 225 deletions

Sources/NextcloudFileProviderKit/Database/FilesDatabaseManager.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import FileProvider
55
import Foundation
66
import RealmSwift
77

8-
internal let stable1_0SchemaVersion: UInt64 = 100
9-
internal let stable2_0SchemaVersion: UInt64 = 200 // Major change: deleted LocalFileMetadata type
10-
118
///
129
/// Realm database abstraction and management.
1310
///
@@ -43,7 +40,7 @@ public final class FilesDatabaseManager: Sendable {
4340
)
4441
}
4542

46-
private static let schemaVersion = stable2_0SchemaVersion
43+
private static let schemaVersion = SchemaVersion.addedLockTokenPropertyToRealmItemMetadata
4744
let logger: FileProviderLogger
4845
let account: Account
4946

@@ -115,9 +112,9 @@ public final class FilesDatabaseManager: Sendable {
115112

116113
let configuration = customConfiguration ?? Realm.Configuration(
117114
fileURL: databaseLocation,
118-
schemaVersion: Self.schemaVersion,
115+
schemaVersion: Self.schemaVersion.rawValue,
119116
migrationBlock: { migration, oldSchemaVersion in
120-
if oldSchemaVersion == stable1_0SchemaVersion {
117+
if oldSchemaVersion == SchemaVersion.initial.rawValue {
121118
var localFileMetadataOcIds = Set<String>()
122119

123120
migration.enumerateObjects(ofType: "LocalFileMetadata") { oldObject, _ in
@@ -171,7 +168,7 @@ public final class FilesDatabaseManager: Sendable {
171168

172169
logger.info("Migrating shared legacy database to new database for \(account.ncKitAccount)")
173170

174-
let legacyConfiguration = Realm.Configuration(fileURL: sharedDatabaseURL, schemaVersion: stable2_0SchemaVersion, objectTypes: [RealmItemMetadata.self, RemoteFileChunk.self])
171+
let legacyConfiguration = Realm.Configuration(fileURL: sharedDatabaseURL, schemaVersion: SchemaVersion.deletedLocalFileMetadata.rawValue, objectTypes: [RealmItemMetadata.self, RemoteFileChunk.self])
175172

176173
do {
177174
let legacyRealm = try Realm(configuration: legacyConfiguration)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
// SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
///
5+
/// Different schema versions shipped with this project.
6+
///
7+
enum SchemaVersion: UInt64 {
8+
case initial = 100
9+
case deletedLocalFileMetadata = 200
10+
case addedLockTokenPropertyToRealmItemMetadata = 201
11+
}

Sources/NextcloudFileProviderKit/Extensions/NKFile+Extensions.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ extension NKFile {
6363
note: note,
6464
ownerId: ownerId,
6565
ownerDisplayName: ownerDisplayName,
66-
lock: lock,
67-
lockOwner: lockOwner,
68-
lockOwnerEditor: lockOwnerEditor,
69-
lockOwnerType: lockOwnerType,
70-
lockOwnerDisplayName: lockOwnerDisplayName,
71-
lockTime: lockTime,
72-
lockTimeOut: lockTimeOut,
66+
lock: lock != nil ? true : false,
67+
lockOwner: lock?.owner,
68+
lockOwnerEditor: lock?.ownerEditor,
69+
lockOwnerType: lock?.ownerType.rawValue,
70+
lockOwnerDisplayName: lock?.ownerDisplayName,
71+
lockTime: lock?.time,
72+
lockTimeOut: lock?.timeOut,
73+
lockToken: lock?.token,
7374
path: path,
7475
permissions: permissions,
7576
quotaUsedBytes: quotaUsedBytes,

Sources/NextcloudFileProviderKit/Interface/NextcloudKit+RemoteInterface.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,8 @@ extension NextcloudKit: RemoteInterface {
304304
}
305305
}
306306

307-
public func setLockStateForFile(
308-
remotePath: String,
309-
lock: Bool,
310-
account: Account,
311-
options: NKRequestOptions,
312-
taskHandler: @escaping (_ task: URLSessionTask) -> Void
313-
) async -> (account: String, response: HTTPURLResponse?, error: NKError) {
314-
return await withCheckedContinuation { continuation in
315-
lockUnlockFile(
316-
serverUrlFileName: remotePath, shouldLock: lock, account: account.ncKitAccount
317-
) { account, response, error in
318-
continuation.resume(returning: (account, response?.response, error))
319-
}
320-
}
307+
public func lockUnlockFile(serverUrlFileName: String, type: NKLockType?, shouldLock: Bool, account: Account, options: NKRequestOptions, taskHandler: @escaping (URLSessionTask) -> Void) async throws -> NKLock? {
308+
return try await lockUnlockFile(serverUrlFileName: serverUrlFileName, type: type, shouldLock: shouldLock, account: account.ncKitAccount, options: options, taskHandler: taskHandler)
321309
}
322310

323311
public func trashedItems(

Sources/NextcloudFileProviderKit/Interface/RemoteInterface.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ public protocol RemoteInterface {
125125
taskHandler: @escaping (_ task: URLSessionTask) -> Void
126126
) async -> (account: String, response: HTTPURLResponse?, error: NKError)
127127

128-
func setLockStateForFile(
129-
remotePath: String,
130-
lock: Bool,
131-
account: Account,
132-
options: NKRequestOptions,
133-
taskHandler: @escaping (_ task: URLSessionTask) -> Void
134-
) async -> (account: String, response: HTTPURLResponse?, error: NKError)
128+
func lockUnlockFile(serverUrlFileName: String, type: NKLockType?, shouldLock: Bool, account: Account, options: NKRequestOptions, taskHandler: @escaping (_ task: URLSessionTask) -> Void) async throws -> NKLock?
135129

136130
func trashedItems(
137131
account: Account,

0 commit comments

Comments
 (0)