SPB Git

Repository Public License

ConversionJobManager.swift 5.6 KB 84 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Sources/ConvertCore/Jobs/ConversionJobManager.swift2// Description: The entry point the app uses: owns the registry, the queue and the history, and turns "these files,3//              to that format" into queue jobs.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation67/// Wires the core services together. Create one per app (or per test) and keep it.8public struct ConversionJobManager: Sendable {9    public let registry: FormatRegistry10    public let queue: ConversionQueue11    public let history: ConversionHistoryStore?12    public let workDirectories: WorkDirectoryManager1314    public var resolver: ConversionCapabilityResolver { ConversionCapabilityResolver(registry: registry) }15    public var planner: ConversionPlanner { ConversionPlanner(registry: registry) }16    public var inspector: FileInspector { FileInspector(registry: registry) }17    public var smartDrop: SmartDropAnalyzer { SmartDropAnalyzer(registry: registry) }18    public var workflowRunner: WorkflowRunner { WorkflowRunner(queue: queue, history: history, workDirectories: workDirectories) }1920    /// - Parameters:21    ///   - engines: every engine of the build (`EngineImage.makeEngines() + …`).22    ///   - history: pass `nil` to keep no history.23    public init(engines: [any ConversionEngine], configuration: QueueConfiguration = QueueConfiguration(), history: ConversionHistoryStore? = nil,24                workDirectories: WorkDirectoryManager = WorkDirectoryManager(), monitor: any ResourceMonitoring = SystemResourceMonitor()) {25        let registry = FormatRegistry(engines: engines)26        self.registry = registry27        self.history = history28        self.workDirectories = workDirectories29        queue = ConversionQueue(registry: registry, configuration: configuration, history: history, workDirectories: workDirectories, monitor: monitor)30    }3132    /// Enqueues one job per file, as a batch. Formats are detected from content when each job starts.33    @discardableResult34    public func convert(_ urls: [URL], to destinationFormat: FormatID, operation: String? = nil, options: ConversionOptions = .init(),35                        destination: OutputDestination = .init(), priority: JobPriority = .normal) async -> [UUID] {36        await queue.enqueue(batch: urls.map {37            ConversionJobRequest(sources: [$0], destinationFormat: destinationFormat, operation: operation, options: options,38                                 destination: destination, priority: priority)39        })40    }4142    /// Enqueues a single job that consumes all the files at once (merge, images → PDF, archive creation). The path43    /// must declare `acceptsMultipleSources`; otherwise the job fails with an explanation.44    @discardableResult45    public func combine(_ urls: [URL], to destinationFormat: FormatID, operation: String? = nil, options: ConversionOptions = .init(),46                        destination: OutputDestination = .init(), priority: JobPriority = .normal) async -> UUID {47        await queue.enqueue(ConversionJobRequest(sources: urls, destinationFormat: destinationFormat, operation: operation, options: options,48                                                 destination: destination, priority: priority))49    }5051    /// Enqueues a generic operation (`ConversionCapabilityResolver.genericOperations`) — one that does not look at52    /// what the files are, such as "Create ZIP archive" or "Compress to gzip". The jobs carry `sourceFormat: .folder`,53    /// the pseudo-source those paths are declared from, so nothing is detected and any file qualifies.54    ///55    /// A target that accepts several sources gives ONE job for the whole selection (files and folders, in order);56    /// another gives one job per item (folders are refused by the engine with its own message).57    @discardableResult58    public func enqueueGeneric(_ urls: [URL], target: ConversionTarget, options: ConversionOptions = .init(),59                               destination: OutputDestination = .init(), priority: JobPriority = .normal) async -> [UUID] {60        guard !urls.isEmpty else { return [] }61        if target.best.acceptsMultipleSources {62            return [await queue.enqueue(ConversionJobRequest(sources: urls, sourceFormat: .folder, destinationFormat: target.destination, operation: target.operation,63                                                             options: options, destination: destination, priority: priority))]64        }65        return await queue.enqueue(batch: urls.map {66            ConversionJobRequest(sources: [$0], sourceFormat: .folder, destinationFormat: target.destination, operation: target.operation, options: options,67                                 destination: destination, priority: priority)68        })69    }7071    /// Enqueues the files a preset applies to; the others are returned as `skipped`.72    public func apply(_ preset: ConversionPreset, to urls: [URL], destination: OutputDestination = .init(),73                      priority: JobPriority = .normal) async -> (jobs: [UUID], skipped: [URL]) {74        var matching: [ConversionJobRequest] = []75        var skipped: [URL] = []76        for url in urls {77            guard let format = FileTypeDetector.detect(url).format, preset.source.matches(format) else { skipped.append(url); continue }78            matching.append(ConversionJobRequest(sources: [url], sourceFormat: format, destinationFormat: preset.destinationFormat,79                                                 operation: preset.operation, options: preset.options, destination: destination, priority: priority))80        }81        return (await queue.enqueue(batch: matching), skipped)82    }83}84
Files main
Menu

Theme