SPB Git

Repository Public

FileProviderManager.swift 3.2 KB 78 lines swift

1// SP Drive iOS2// File: FileProviderExtension/FileProviderManager.swift3// Description: What the extension needs to work on its own: its metadata mirror (one database per domain, in the group container, never4//              opened by the app), the API client authenticated by the shared keychain item, and the mapping of errors to File Provider errors.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import FileProvider9import Foundation10import Synchronization1112final class ProviderContext: @unchecked Sendable {13    let domain: NSFileProviderDomain14    let store: DriveStore15    let api: DriveAPI16    private let refreshing = Mutex<Task<Void, Error>?>(nil)1718    init(domain: NSFileProviderDomain) throws {19        self.domain = domain20        let name = LocalFiles.safeID(domain.identifier.rawValue)21        store = try DriveStore(path: AppGroup.fileProviderState.appendingPathComponent("\(name).sqlite").path)22        api = DriveAPI(tokens: KeychainTokenStore())23    }2425    var manager: NSFileProviderManager? { NSFileProviderManager(for: domain) }2627    /// Brings the mirror up to date. Concurrent callers share one pull.28    func refresh() async throws {29        let task = refreshing.withLock { current -> Task<Void, Error> in30            if let current { return current }31            let t = Task { [store, api] in _ = try await MirrorSync.pull(store: store, remote: api) }32            current = t33            return t34        }35        defer { refreshing.withLock { if $0 == task { $0 = nil } } }36        try await task.value37    }3839    func item(_ identifier: NSFileProviderItemIdentifier) -> DriveItem? {40        if identifier == .rootContainer { return store.rootID.flatMap(store.item) }41        return store.item(identifier.rawValue)42    }4344    func id(_ identifier: NSFileProviderItemIdentifier) -> String? {45        identifier == .rootContainer ? store.rootID : identifier.rawValue46    }4748    func providerItem(_ item: DriveItem) -> FileProviderItem { FileProviderItem(item, rootID: store.rootID ?? "") }4950    /// Records the server's answer and tells the system the working set moved.51    func record(_ item: DriveItem) {52        try? store.upsert(item)53        signal()54    }5556    func signal() {57        nonisolated(unsafe) let manager = manager58        manager?.signalEnumerator(for: .workingSet) { _ in }59    }6061    func providerError(_ error: Error) -> Error {62        if let e = error as? DirectTransfer.UploadRefused { return providerError(e.error) }63        if (error as NSError).domain == NSFileProviderErrorDomain { return error }64        guard let e = error as? RemoteError else {65            if error is CancellationError { return CocoaError(.userCancelled) }66            return NSFileProviderError(.cannotSynchronize)67        }68        switch e {69        case .unauthenticated: return NSFileProviderError(.notAuthenticated)70        case .offline, .rateLimited: return NSFileProviderError(.serverUnreachable)71        case .notFound: return NSFileProviderError(.noSuchItem)72        case .quotaExceeded, .fileTooLarge: return NSFileProviderError(.insufficientQuota)73        case .server(let status, _, _) where status >= 500: return NSFileProviderError(.serverUnreachable)74        default: return NSFileProviderError(.cannotSynchronize)75        }76    }77}78
Files main
Menu

Theme