SPB Git

Repository Public

AppEnvironment.swift 7.9 KB 168 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Drive pour macOS9// File             : App/AppEnvironment.swift10// Description      : What the application and its File Provider extension must11//                    agree on: identifiers, the shared container, the keychain12//                    group, the server, and how the engine objects are built.13//                    Compiled into both targets.14// Module           : App15// Repository       : https://git.spboucher.ai/sp-drive-macos16// Created          : 2026-11-1817// Last Modified    : 2026-11-1918//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : None specified (no license file in this repository)84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import FileProvider92import Foundation93import OSLog94import SPDriveCore95import SPDriveSync9697enum AppEnvironment {98    static let teamID = "3YM54G49SN"99    #if SPD_DEV100    /// Development variant: its own identifiers, container, keychain item and Finder location, so it never touches the installed app.101    static let variant = ".dev"102    static let productName = "SP Drive Dev"103    #else104    static let variant = ""105    static let productName = "SP Drive"106    #endif107    static let appBundleID = "ai.spboucher.spdrive\(variant)"108    /// App group shared by the app and the extension (team-prefixed form of macOS).109    static let groupID = "\(teamID).ai.spboucher.spdrive\(variant)"110    static let keychainGroup = "\(teamID).ai.spboucher.spdrive\(variant).shared"111    static let productionServer = URL(string: "https://drive.spboucher.ai")!112    static let domainIdentifier = NSFileProviderDomainIdentifier("spdrive")113    /// Darwin notification posted by either process after it changed the shared database.114    static let changedNotification = "ai.spboucher.spdrive\(variant).changed"115    static let log = Logger(subsystem: appBundleID, category: "app")116117    nonisolated(unsafe) static let defaults: UserDefaults = UserDefaults(suiteName: groupID) ?? .standard // UserDefaults is thread-safe118    /// The server. Fixed in the released application; the development variant may point at a local test backend.119    static var serverURL: URL {120        #if SPD_DEV121        if let s = defaults.string(forKey: "serverURL"), let u = URL(string: s) { return u }122        return URL(string: "http://127.0.0.1:8665")!123        #else124        return productionServer125        #endif126    }127    static var containerURL: URL {128        FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID)129            ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0].appendingPathComponent(appBundleID)130    }131    /// One database per server and account, so signing in elsewhere never mixes two trees.132    static func databaseURL(account: String) -> URL {133        let safe = account.unicodeScalars.map { CharacterSet.alphanumerics.contains($0) ? String($0) : "_" }.joined()134        return containerURL.appendingPathComponent("accounts/\(serverURL.host ?? "server")-\(safe)/metadata.sqlite")135    }136    static var workDirectory: URL { containerURL.appendingPathComponent("work", isDirectory: true) }137    static var currentAccount: String? {138        get { defaults.string(forKey: "accountID") }139        set { defaults.set(newValue, forKey: "accountID") }140    }141    static var deviceName: String {142        let name = Host.current().localizedName ?? "Mac"143        return name.isEmpty ? "Mac" : name144    }145    static var userAgent: String {146        let v = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "0"147        let os = ProcessInfo.processInfo.operatingSystemVersion148        return "SPDrive-macOS/\(v) (macOS \(os.majorVersion).\(os.minorVersion))"149    }150    /// `X-SPD-Client` of SP Drive 2.0, sent on every request by the app and by the File Provider extension: `macos/2.0.0 (27.0)`.151    static var clientHeader: String {152        let v = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "0"153        let os = ProcessInfo.processInfo.operatingSystemVersion154        return "macos/\(v) (\(os.majorVersion).\(os.minorVersion))"155    }156    static let tokens: TokenStore = KeychainTokenStore(service: "ai.spboucher.spdrive\(variant)", accessGroup: keychainGroup)157    static func makeAPI() throws -> APIClient { try APIClient(baseURL: serverURL, tokens: tokens, userAgent: userAgent, clientHeader: clientHeader) }158    /// The mirror of the signed-in account, or nil when nobody is signed in on this Mac.159    static func openStore() throws -> MetadataStore? { try currentAccount.map { try MetadataStore(url: databaseURL(account: $0)) } }160161    static func postChanged() {162        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFNotificationName(changedNotification as CFString), nil, nil, true)163    }164}165166/// SP Drive's share link (SwiftUI has a type of the same name).167typealias DriveShare = SPDriveCore.ShareLink168
Files main
Menu

Theme