SPB Git

Repository Public License

FolderWatcher.swift 33.3 KB 696 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Sources/ConvertCore/FolderWatcher.swift2// Description: Watched folders. Rules (folder + match → preset/workflow/conversion → destination) are applied to3//              files that arrive in folders the user explicitly added. Files are processed only once they are stable,4//              never when the watcher itself produced them, and never twice (path + size + mtime, content hash).5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import CoreServices7import CryptoKit8import Foundation910// MARK: - Model1112public struct WatchRule: Sendable, Hashable, Codable, Identifiable {13    public enum Match: Sendable, Hashable, Codable {14        case any15        case formats(Set<FormatID>)16        case categories(Set<FormatCategory>)17        /// Shell glob on the file name (`*.heic`, `scan-*.pdf`), case-insensitive.18        case glob(String)19    }2021    /// A copy whose action carries no secret option value (keys containing `password`). This is what the watcher22    /// keeps and writes to `watch-rules.json`.23    public func redactingSecrets() -> WatchRule {24        guard case .convert(let destination, let operation, let options) = action else { return self }25        var copy = self26        copy.action = .convert(destination: destination, operation: operation, options: options.redactingSecrets())27        return copy28    }2930    public enum Action: Sendable, Hashable, Codable {31        case convert(destination: FormatID, operation: String?, options: ConversionOptions)32        /// A preset of the `PresetStore`.33        case preset(UUID)34        /// A workflow of the `WorkflowLibrary`. Its export nodes decide the destinations; paths without an export35        /// node write to the rule's destination folder.36        case workflow(UUID)37    }3839    public var id: UUID40    public var name: String41    /// The watched folder (added explicitly by the user).42    public var folder: URL43    /// Bookmark of `folder` (security-scoped when the system grants one), so the rule follows a moved folder.44    public var folderBookmark: Data?45    public var match: Match46    public var action: Action47    public var destinationFolder: URL48    public var destinationBookmark: Data?49    public var conflictPolicy: ConflictPolicy50    public var includeSubfolders: Bool51    public var isPaused: Bool52    /// Also process the files already present when the rule starts (default: only files that arrive afterwards).53    public var processExistingFiles: Bool5455    public init(id: UUID = UUID(), name: String, folder: URL, match: Match, action: Action, destinationFolder: URL,56                conflictPolicy: ConflictPolicy = .rename, includeSubfolders: Bool = false, isPaused: Bool = false, processExistingFiles: Bool = false) {57        self.id = id; self.name = name; self.folder = folder; self.match = match; self.action = action58        self.destinationFolder = destinationFolder; self.conflictPolicy = conflictPolicy; self.includeSubfolders = includeSubfolders59        self.isPaused = isPaused; self.processExistingFiles = processExistingFiles60    }6162    /// `true` when a file with this name and detected format is covered by the rule.63    public func matches(name: String, format: FormatID?) -> Bool {64        switch match {65        case .any: true66        case .formats(let formats): format.map(formats.contains) ?? false67        case .categories(let categories): format.flatMap(FormatCatalog.format).map { categories.contains($0.category) } ?? false68        case .glob(let pattern): Glob.matches(pattern, name)69        }70    }71}7273public struct WatchActivity: Sendable, Hashable, Codable, Identifiable {74    public enum Kind: String, Sendable, Codable {75        /// The rule's action ran and wrote files.76        case processed77        /// The action ran and wrote nothing (filtered by the workflow, or the destination already existed).78        case nothingWritten79        case failed80        /// Same path, size and modification date, or same content, as a file already processed by the rule.81        case duplicate82    }8384    public var id: UUID85    public var date: Date86    public var ruleID: UUID87    public var path: String88    public var kind: Kind89    public var detail: String90    public var outputs: [String]91}9293public enum WatchEvent: Sendable {94    case activity(WatchActivity)95    case rulesChanged([WatchRule])96    case stateChanged(isRunning: Bool, isPaused: Bool)97}9899public struct WatcherConfiguration: Sendable, Hashable {100    /// A file is stable when its size and modification date have not changed for this long.101    public var settleInterval: TimeInterval102    /// How often pending files are re-examined.103    public var tickInterval: TimeInterval104    /// Folders are also re-listed at this interval, in case a file-system event was missed.105    public var rescanInterval: TimeInterval106    /// Skip a file whose content was already processed by the same rule (re-drop, copy under another name).107    public var skipDuplicateContent: Bool108    public var maxActivityEntries: Int109    /// Fingerprints remembered per rule.110    public var maxFingerprintsPerRule: Int111    /// Most files listed per folder scan.112    public var maxFilesPerScan: Int113114    public init(settleInterval: TimeInterval = 2, tickInterval: TimeInterval = 0.5, rescanInterval: TimeInterval = 15, skipDuplicateContent: Bool = true,115                maxActivityEntries: Int = 1000, maxFingerprintsPerRule: Int = 5000, maxFilesPerScan: Int = 20_000) {116        self.settleInterval = max(0.05, settleInterval); self.tickInterval = max(0.02, tickInterval); self.rescanInterval = max(0.05, rescanInterval)117        self.skipDuplicateContent = skipDuplicateContent; self.maxActivityEntries = max(10, maxActivityEntries)118        self.maxFingerprintsPerRule = max(10, maxFingerprintsPerRule); self.maxFilesPerScan = max(1, maxFilesPerScan)119    }120}121122public enum WatchError: Error, Sendable, LocalizedError, Equatable {123    /// The rule could feed itself: its outputs land where it watches and match it.124    case loopRisk(String)125    case folderUnavailable(String)126    case notFound(String)127    case storage(String)128129    public var errorDescription: String? {130        switch self {131        case .loopRisk(let m): "This rule would process its own results. \(m)"132        case .folderUnavailable(let m): "The folder is not available. \(m)"133        case .notFound(let m): m134        case .storage(let m): "Watched folders could not be saved. \(m)"135        }136    }137}138139// MARK: - Watcher140141/// Applies watch rules to arriving files.142///143/// Guarantees:144/// - only the folders of the rules are looked at; nothing is watched implicitly;145/// - a file is processed after `settleInterval` without change, and never while its name marks it as partial146///   (`.crdownload`, `.part`, `.download`, `.tmp`, dot files, `~$` lock files…). Only regular files are processed;147/// - a file written by the watcher is never processed by any rule, and a rule whose destination lies in its watched148///   folder while its outputs can match it is refused (`WatchError.loopRisk`);149/// - a file is processed once per rule: unchanged path + size + modification date is skipped silently, an150///   identical content arriving again is reported as `duplicate`.151public actor FolderWatcher {152    private struct Fingerprint: Codable, Hashable {153        var size: Int64154        var modified: Double155    }156157    private struct Pending {158        var ruleID: UUID159        var fingerprint: Fingerprint160        var since: Date161    }162163    private struct Store: Codable {164        var version = 1165        var rules: [WatchRule] = []166        var activity: [WatchActivity] = []167        var seen: [String: [String: Fingerprint]] = [:]168        var hashes: [String: [String]] = [:]169        var produced: [String] = []170    }171172    public nonisolated let storeURL: URL173    private let runner: WorkflowRunner174    private let presets: PresetStore175    private let workflows: WorkflowLibrary176    private let configuration: WatcherConfiguration177178    private var store = Store()179    private var pending: [String: Pending] = [:]180    private var inFlight: [String: [String]] = [:]181    private var dirty = Set<UUID>()182    private var lastScan: [UUID: Date] = [:]183    private var driver: Task<Void, Never>?184    private var monitor: FSEventsMonitor?185    private var globallyPaused = false186    private var subscribers: [UUID: AsyncStream<WatchEvent>.Continuation] = [:]187    /// Why the stored file could not be loaded, if it could not.188    public private(set) var loadError: String?189190    public init(runner: WorkflowRunner, presets: PresetStore, workflows: WorkflowLibrary, storeURL: URL = AppPaths.watcherFile,191                configuration: WatcherConfiguration = WatcherConfiguration()) {192        self.runner = runner; self.presets = presets; self.workflows = workflows; self.storeURL = storeURL; self.configuration = configuration193        if let data = try? Data(contentsOf: storeURL) {194            do {195                store = try JSONDecoder().decode(Store.self, from: data)196                store.rules = store.rules.map { $0.redactingSecrets() }197            } catch { loadError = error.localizedDescription }198        }199    }200201    // MARK: Rules202203    public var rules: [WatchRule] { store.rules }204    public func rule(id: UUID) -> WatchRule? { store.rules.first { $0.id == id } }205206    /// Adds a rule after checking that both folders exist (the destination is created if needed) and that the rule207    /// cannot loop. Bookmarks are recorded for both folders.208    @discardableResult209    public func addRule(_ rule: WatchRule) async throws -> WatchRule {210        let prepared = try await prepare(rule)211        store.rules.removeAll { $0.id == prepared.id }212        store.rules.append(prepared)213        try persist()214        if driver != nil {215            baseline(prepared)216            restartMonitor()217        }218        emit(.rulesChanged(store.rules))219        return prepared220    }221222    /// Replaces a rule (same identifier) with the same checks as `addRule`.223    public func updateRule(_ rule: WatchRule) async throws {224        guard store.rules.contains(where: { $0.id == rule.id }) else { throw WatchError.notFound("The rule no longer exists.") }225        try await addRule(rule)226    }227228    public func removeRule(id: UUID) throws {229        guard store.rules.contains(where: { $0.id == id }) else { throw WatchError.notFound("The rule no longer exists.") }230        store.rules.removeAll { $0.id == id }231        store.seen[id.uuidString] = nil232        store.hashes[id.uuidString] = nil233        pending = pending.filter { $0.value.ruleID != id }234        try persist()235        if driver != nil { restartMonitor() }236        emit(.rulesChanged(store.rules))237    }238239    /// Pauses or resumes one rule. A paused rule ignores arrivals; files that arrived meanwhile are picked up when240    /// it resumes only if `processExistingFiles` is set.241    public func setRulePaused(_ id: UUID, _ paused: Bool) throws {242        guard let index = store.rules.firstIndex(where: { $0.id == id }) else { throw WatchError.notFound("The rule no longer exists.") }243        store.rules[index].isPaused = paused244        if paused {245            pending = pending.filter { $0.value.ruleID != id }246        } else if driver != nil {247            baseline(store.rules[index])248        }249        try persist()250        emit(.rulesChanged(store.rules))251    }252253    /// Pauses every rule without changing their own paused flag.254    public func pauseAll() {255        globallyPaused = true256        pending = [:]257        emit(.stateChanged(isRunning: driver != nil, isPaused: true))258    }259260    public func resumeAll() {261        globallyPaused = false262        if driver != nil { store.rules.forEach(baseline) }263        emit(.stateChanged(isRunning: driver != nil, isPaused: false))264    }265266    public var isPaused: Bool { globallyPaused }267    public var isRunning: Bool { driver != nil }268269    // MARK: Lifecycle270271    /// Starts watching the folders of the rules. Folders are resolved from their bookmarks first.272    public func start() {273        guard driver == nil else { return }274        resolveBookmarks()275        store.rules.forEach(baseline)276        restartMonitor()277        let tick = configuration.tickInterval278        driver = Task { [weak self] in279            while !Task.isCancelled {280                try? await Task.sleep(nanoseconds: UInt64(tick * 1_000_000_000))281                guard let self, !Task.isCancelled else { return }282                await self.tick()283            }284        }285        emit(.stateChanged(isRunning: true, isPaused: globallyPaused))286    }287288    /// Stops watching. Conversions already started continue.289    public func stop() {290        driver?.cancel()291        driver = nil292        monitor?.stop()293        monitor = nil294        pending = [:]295        emit(.stateChanged(isRunning: false, isPaused: globallyPaused))296    }297298    /// Lists every watched folder now and examines pending files (also happens on file-system events and timers).299    public func scanNow() {300        dirty.formUnion(store.rules.map(\.id))301        tick()302    }303304    // MARK: Activity305306    /// Most recent last.307    public var activity: [WatchActivity] { store.activity }308309    public func clearActivity() throws {310        store.activity = []311        try persist()312    }313314    public func events() -> AsyncStream<WatchEvent> {315        let token = UUID()316        let (stream, continuation) = AsyncStream<WatchEvent>.makeStream(bufferingPolicy: .bufferingNewest(1024))317        subscribers[token] = continuation318        continuation.onTermination = { [weak self] _ in Task { await self?.unsubscribe(token) } }319        return stream320    }321322    // MARK: Loop prevention323324    /// Explains why a rule could process its own results, or returns `nil` when it cannot.325    ///326    /// A destination is dangerous when it is the watched folder itself, or lies inside it while the rule includes327    /// subfolders. In that case the rule is accepted only if its outputs cannot match it: their formats (the328    /// conversion's destination, or everything the workflow can export) are outside the rule's formats/categories,329    /// or none of their extensions fits the rule's glob (for a glob, the formats that can arrive are those whose330    /// extensions fit it). A rule matching any file, or a workflow able to export an unconverted file, can always331    /// match.332    public static func loopRisk(rule: WatchRule, workflow: Workflow, registry: FormatRegistry) -> String? {333        let watched = canonical(rule.folder)334        var destinations = [rule.destinationFolder]335        func collect(_ nodes: [WorkflowNode]) {336            for node in nodes {337                switch node {338                case .export(let folder, _): destinations.append(folder)339                case .branch(_, let then, let otherwise): collect(then); collect(otherwise)340                default: break341                }342            }343        }344        collect(workflow.nodes)345        let inside = destinations.map(canonical).filter { $0 == watched || (rule.includeSubfolders && $0.hasPrefix(watched + "/")) }346        guard let risky = inside.first else { return nil }347348        let inputs: Set<FormatID>?349        switch rule.match {350        case .formats(let formats): inputs = formats351        case .categories(let categories): inputs = Set(FormatCatalog.all.filter { categories.contains($0.category) }.map(\.id))352        case .glob(let pattern):353            // A glob on the extension (`*.txt`) tells which formats can arrive; any other glob tells nothing.354            let byExtension = Set(FormatCatalog.all.filter { $0.extensions.contains { Glob.matches(pattern, "name." + $0) } }.map(\.id))355            inputs = byExtension.isEmpty ? nil : byExtension356        case .any: inputs = nil357        }358        let validation = WorkflowValidator.validate(workflow, registry: registry, inputFormats: inputs)359        let outputs = validation.exportedFormats360        let canMatch: Bool361        switch rule.match {362        case .any:363            canMatch = true364        case .formats(let formats):365            canMatch = validation.canExportUnconverted || !outputs.isDisjoint(with: formats)366        case .categories(let categories):367            canMatch = validation.canExportUnconverted || outputs.contains { FormatCatalog.format($0).map { categories.contains($0.category) } ?? true }368        case .glob(let pattern):369            canMatch = validation.canExportUnconverted || outputs.contains { format in370                let extensions = FormatCatalog.format(format)?.extensions ?? [format.rawValue]371                return extensions.isEmpty || extensions.contains { Glob.matches(pattern, "name." + $0) }372            }373        }374        guard canMatch else { return nil }375        return "Its results are written to \(risky), inside the folder it watches, and can match the rule again. Choose a destination outside the watched folder, or restrict the rule to formats it does not produce."376    }377378    /// Names that mark a file as temporary, partial or internal: never processed.379    public static func isTemporaryName(_ name: String) -> Bool {380        let lower = name.lowercased()381        if lower.hasPrefix(".") || lower.hasPrefix("~$") || lower.hasSuffix("~") { return true }382        if ["thumbs.db", "desktop.ini", "icon\r"].contains(lower) { return true }383        let partial = ["crdownload", "part", "partial", "download", "tmp", "temp", "opdownload", "!ut", "aria2", "swp", "filepart", "lock"]384        return partial.contains((lower as NSString).pathExtension)385    }386387    // MARK: Internals388389    private static func canonical(_ url: URL) -> String { url.standardizedFileURL.resolvingSymlinksInPath().path }390391    private func prepare(_ rule: WatchRule) async throws -> WatchRule {392        // A rule runs unattended and is stored on disk: it never carries a secret (same as presets and workflows).393        var rule = rule.redactingSecrets()394        var isDirectory: ObjCBool = false395        guard FileManager.default.fileExists(atPath: rule.folder.path, isDirectory: &isDirectory), isDirectory.boolValue else {396            throw WatchError.folderUnavailable("\(rule.folder.path) does not exist or is not a folder.")397        }398        do {399            try FileManager.default.createDirectory(at: rule.destinationFolder, withIntermediateDirectories: true)400        } catch {401            throw WatchError.folderUnavailable("\(rule.destinationFolder.path) could not be created: \(error.localizedDescription)")402        }403        let workflow = try await workflow(for: rule)404        if let risk = Self.loopRisk(rule: rule, workflow: workflow, registry: runner.queue.registry) { throw WatchError.loopRisk(risk) }405        rule.folderBookmark = rule.folderBookmark ?? Self.bookmark(rule.folder)406        rule.destinationBookmark = rule.destinationBookmark ?? Self.bookmark(rule.destinationFolder)407        return rule408    }409410    private static func bookmark(_ url: URL) -> Data? {411        (try? url.bookmarkData(options: .withSecurityScope)) ?? (try? url.bookmarkData())412    }413414    private func resolveBookmarks() {415        for index in store.rules.indices {416            for keyPath in [(\WatchRule.folderBookmark, \WatchRule.folder), (\WatchRule.destinationBookmark, \WatchRule.destinationFolder)] {417                guard let data = store.rules[index][keyPath: keyPath.0] else { continue }418                var stale = false419                let resolved = (try? URL(resolvingBookmarkData: data, options: .withSecurityScope, bookmarkDataIsStale: &stale))420                    ?? (try? URL(resolvingBookmarkData: data, bookmarkDataIsStale: &stale))421                if let resolved, resolved.path != store.rules[index][keyPath: keyPath.1].path { store.rules[index][keyPath: keyPath.1] = resolved }422            }423        }424    }425426    /// The workflow a rule runs: the referenced one, or a two-node workflow built from a preset or a conversion.427    private func workflow(for rule: WatchRule) async throws -> Workflow {428        switch rule.action {429        case .workflow(let id):430            guard let workflow = await workflows.workflow(id: id) else { throw WatchError.notFound("The workflow of rule “\(rule.name)” no longer exists.") }431            return workflow432        case .preset(let id):433            guard let preset = await presets.preset(id: id) else { throw WatchError.notFound("The preset of rule “\(rule.name)” no longer exists.") }434            return Workflow(name: preset.name, nodes: [.convert(destination: preset.destinationFormat, operation: preset.operation, options: preset.options),435                                                       .export(folder: rule.destinationFolder, conflictPolicy: rule.conflictPolicy)])436        case .convert(let destination, let operation, let options):437            return Workflow(name: rule.name, nodes: [.convert(destination: destination, operation: operation, options: options),438                                                     .export(folder: rule.destinationFolder, conflictPolicy: rule.conflictPolicy)])439        }440    }441442    private func restartMonitor() {443        monitor?.stop()444        let paths = Array(Set(store.rules.map { $0.folder.path }))445        guard !paths.isEmpty else { monitor = nil; return }446        monitor = FSEventsMonitor(paths: paths) { [weak self] changed in447            Task { await self?.fileSystemChanged(changed) }448        }449    }450451    private func fileSystemChanged(_ paths: [String]) {452        let changed = paths.map { Self.canonical(URL(fileURLWithPath: $0)) }453        for rule in store.rules {454            let root = Self.canonical(rule.folder)455            if changed.contains(where: { $0 == root || $0.hasPrefix(root + "/") }) { dirty.insert(rule.id) }456        }457    }458459    private func candidates(of rule: WatchRule) -> [(path: String, fingerprint: Fingerprint)] {460        let fm = FileManager.default461        let keys: [URLResourceKey] = [.isRegularFileKey, .fileSizeKey, .contentModificationDateKey]462        var urls: [URL] = []463        if rule.includeSubfolders {464            if let enumerator = fm.enumerator(at: rule.folder, includingPropertiesForKeys: keys, options: [.skipsHiddenFiles, .skipsPackageDescendants]) {465                for case let url as URL in enumerator {466                    urls.append(url)467                    if urls.count >= configuration.maxFilesPerScan { break }468                }469            }470        } else {471            urls = Array(((try? fm.contentsOfDirectory(at: rule.folder, includingPropertiesForKeys: keys, options: [.skipsHiddenFiles])) ?? []).prefix(configuration.maxFilesPerScan))472        }473        var result: [(String, Fingerprint)] = []474        for url in urls where !Self.isTemporaryName(url.lastPathComponent) {475            guard let values = try? url.resourceValues(forKeys: Set(keys)), values.isRegularFile == true else { continue }476            result.append((Self.canonical(url), Fingerprint(size: Int64(values.fileSize ?? 0), modified: values.contentModificationDate?.timeIntervalSince1970 ?? 0)))477        }478        return result479    }480481    /// Marks the files already present as seen, so only later arrivals are processed.482    private func baseline(_ rule: WatchRule) {483        guard !rule.processExistingFiles else { dirty.insert(rule.id); return }484        for candidate in candidates(of: rule) where store.seen[rule.id.uuidString]?[candidate.path] == nil {485            remember(candidate.path, candidate.fingerprint, rule: rule.id)486        }487        lastScan[rule.id] = Date()488    }489490    private func remember(_ path: String, _ fingerprint: Fingerprint, rule: UUID) {491        var seen = store.seen[rule.uuidString] ?? [:]492        seen[path] = fingerprint493        if seen.count > configuration.maxFingerprintsPerRule {494            // Forget the oldest files first.495            for key in seen.sorted(by: { $0.value.modified < $1.value.modified }).prefix(seen.count - configuration.maxFingerprintsPerRule).map(\.key) { seen[key] = nil }496        }497        store.seen[rule.uuidString] = seen498    }499500    private func tick() {501        guard !globallyPaused else { return }502        let now = Date()503        let produced = Set(store.produced)504        for rule in store.rules where !rule.isPaused {505            let due = now.timeIntervalSince(lastScan[rule.id] ?? .distantPast) >= configuration.rescanInterval506            guard dirty.contains(rule.id) || due else { continue }507            dirty.remove(rule.id)508            lastScan[rule.id] = now509            for candidate in candidates(of: rule) {510                if produced.contains(candidate.path) || inFlight[candidate.path] != nil { continue }511                if store.seen[rule.id.uuidString]?[candidate.path] == candidate.fingerprint { continue }512                if let existing = pending[candidate.path] {513                    if existing.fingerprint != candidate.fingerprint { pending[candidate.path] = Pending(ruleID: existing.ruleID, fingerprint: candidate.fingerprint, since: now) }514                } else {515                    pending[candidate.path] = Pending(ruleID: rule.id, fingerprint: candidate.fingerprint, since: now)516                }517            }518        }519        // Stability: process the pending files whose size and date stopped changing.520        let busyDestinations = Set(inFlight.values.flatMap { $0 })521        for (path, entry) in pending {522            guard let rule = rule(id: entry.ruleID), !rule.isPaused else { pending[path] = nil; continue }523            guard let attributes = try? FileManager.default.attributesOfItem(atPath: path), attributes[.type] as? FileAttributeType == .typeRegular else {524                pending[path] = nil525                continue526            }527            let current = Fingerprint(size: (attributes[.size] as? NSNumber)?.int64Value ?? 0,528                                      modified: (attributes[.modificationDate] as? Date)?.timeIntervalSince1970 ?? 0)529            if current != entry.fingerprint {530                pending[path] = Pending(ruleID: entry.ruleID, fingerprint: current, since: now)531                continue532            }533            guard current.size > 0, now.timeIntervalSince(entry.since) >= configuration.settleInterval else { continue }534            // A run writing into this file's folder may be about to declare it as its own output: wait for it.535            let parent = (path as NSString).deletingLastPathComponent536            if busyDestinations.contains(where: { parent == $0 || parent.hasPrefix($0 + "/") }) { continue }537            if store.produced.contains(path) { pending[path] = nil; continue }538            pending[path] = nil539            inFlight[path] = []540            let needsHash = configuration.skipDuplicateContent541            Task {542                let url = URL(fileURLWithPath: path)543                let format = FileTypeDetector.detect(url).format544                let hash = needsHash ? Self.contentHash(url) : nil545                await self.handle(path: path, fingerprint: current, rule: rule, format: format, hash: hash)546            }547        }548    }549550    private func handle(path: String, fingerprint: Fingerprint, rule: WatchRule, format: FormatID?, hash: String?) async {551        defer { inFlight[path] = nil }552        let url = URL(fileURLWithPath: path)553        guard rule.matches(name: url.lastPathComponent, format: format) else {554            remember(path, fingerprint, rule: rule.id)555            return556        }557        if let hash, store.hashes[rule.id.uuidString]?.contains(hash) == true {558            remember(path, fingerprint, rule: rule.id)559            record(WatchActivity(id: UUID(), date: Date(), ruleID: rule.id, path: path, kind: .duplicate,560                                 detail: "The same content was already processed by this rule.", outputs: []))561            return562        }563        do {564            let workflow = try await workflow(for: rule)565            var destinations = [Self.canonical(rule.destinationFolder)]566            func collect(_ nodes: [WorkflowNode]) {567                for node in nodes {568                    if case .export(let folder, _) = node { destinations.append(Self.canonical(folder)) }569                    if case .branch(_, let then, let otherwise) = node { collect(then); collect(otherwise) }570                }571            }572            collect(workflow.nodes)573            inFlight[path] = destinations574            let options = WorkflowRunOptions(defaultExportFolder: rule.destinationFolder, defaultConflictPolicy: rule.conflictPolicy)575            let run = try await runner.run(workflow, inputs: [url], options: options)576            let item = run.items.first577            let outputs = (item?.outputs ?? []).map(Self.canonical)578            store.produced.append(contentsOf: outputs)579            if store.produced.count > 50_000 { store.produced.removeFirst(store.produced.count - 50_000) }580            remember(path, fingerprint, rule: rule.id)581            if let hash {582                var hashes = store.hashes[rule.id.uuidString] ?? []583                hashes.append(hash)584                if hashes.count > configuration.maxFingerprintsPerRule { hashes.removeFirst(hashes.count - configuration.maxFingerprintsPerRule) }585                store.hashes[rule.id.uuidString] = hashes586            }587            let kind: WatchActivity.Kind588            let detail: String589            switch item?.status {590            case .succeeded?, .succeededWithErrors?:591                kind = outputs.isEmpty ? .nothingWritten : .processed592                detail = item?.errors.first ?? (outputs.isEmpty ? "Nothing was written." : "Wrote \(outputs.count) file\(outputs.count == 1 ? "" : "s").")593            case .filtered?:594                kind = .nothingWritten595                detail = "Excluded by the workflow's filter."596            case .cancelled?:597                kind = .failed598                detail = "Cancelled."599            case .failed?, nil:600                kind = .failed601                detail = item?.errors.first ?? "The conversion failed."602            }603            record(WatchActivity(id: UUID(), date: Date(), ruleID: rule.id, path: path, kind: kind, detail: detail, outputs: outputs))604        } catch {605            remember(path, fingerprint, rule: rule.id)606            record(WatchActivity(id: UUID(), date: Date(), ruleID: rule.id, path: path, kind: .failed,607                                 detail: (error as? LocalizedError)?.errorDescription ?? error.localizedDescription, outputs: []))608        }609    }610611    private func record(_ activity: WatchActivity) {612        store.activity.append(activity)613        if store.activity.count > configuration.maxActivityEntries { store.activity.removeFirst(store.activity.count - configuration.maxActivityEntries) }614        try? persist()615        emit(.activity(activity))616    }617618    /// SHA-256 of the file content, read in 1 MiB blocks.619    static func contentHash(_ url: URL) -> String? {620        guard let handle = try? FileHandle(forReadingFrom: url) else { return nil }621        defer { try? handle.close() }622        var hasher = SHA256()623        while true {624            guard let chunk = try? handle.read(upToCount: 1 << 20), !chunk.isEmpty else { break }625            hasher.update(data: chunk)626        }627        return hasher.finalize().map { String(format: "%02x", $0) }.joined()628    }629630    private func persist() throws {631        do {632            try FileManager.default.createDirectory(at: storeURL.deletingLastPathComponent(), withIntermediateDirectories: true)633            let encoder = JSONEncoder()634            encoder.outputFormatting = [.sortedKeys]635            try encoder.encode(store).write(to: storeURL, options: .atomic)636        } catch {637            throw WatchError.storage(error.localizedDescription)638        }639    }640641    private func emit(_ event: WatchEvent) {642        for continuation in subscribers.values { continuation.yield(event) }643    }644645    private func unsubscribe(_ token: UUID) { subscribers[token] = nil }646}647648/// Thin wrapper over an FSEventStream delivering changed paths on a private dispatch queue.649final class FSEventsMonitor: @unchecked Sendable {650    private var stream: FSEventStreamRef?651    private let handler: @Sendable ([String]) -> Void652    private let queue = DispatchQueue(label: "ai.spboucher.orix.convert.fsevents")653654    init?(paths: [String], latency: TimeInterval = 0.2, handler: @escaping @Sendable ([String]) -> Void) {655        self.handler = handler656        var context = FSEventStreamContext(version: 0, info: Unmanaged.passUnretained(self).toOpaque(), retain: nil, release: nil, copyDescription: nil)657        let callback: FSEventStreamCallback = { _, info, _, eventPaths, _, _ in658            guard let info else { return }659            let monitor = Unmanaged<FSEventsMonitor>.fromOpaque(info).takeUnretainedValue()660            let paths = (unsafeBitCast(eventPaths, to: NSArray.self) as? [String]) ?? []661            monitor.handler(paths)662        }663        let flags = UInt32(kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer)664        guard let created = FSEventStreamCreate(kCFAllocatorDefault, callback, &context, paths as CFArray,665                                                FSEventStreamEventId(kFSEventStreamEventIdSinceNow), latency, flags) else { return nil }666        stream = created667        FSEventStreamSetDispatchQueue(created, queue)668        guard FSEventStreamStart(created) else {669            FSEventStreamInvalidate(created)670            FSEventStreamRelease(created)671            stream = nil672            return nil673        }674    }675676    func stop() {677        queue.sync {678            guard let stream else { return }679            FSEventStreamStop(stream)680            FSEventStreamInvalidate(stream)681            FSEventStreamRelease(stream)682            self.stream = nil683        }684    }685686    deinit {687        guard let stream else { return }688        // Run on the callback queue so a callback in progress has returned before the object goes away.689        queue.sync {690            FSEventStreamStop(stream)691            FSEventStreamInvalidate(stream)692            FSEventStreamRelease(stream)693        }694    }695}696
Files main
Menu

Theme