Repository Public License
1// ORIX Convert — App/Sources/Features/Preview/FilePreviewModel.swift2// Description: Loads a real, bounded preview of a file through the public preview API of its engine: picture, PDF3// pages, 3D scene (parsed in the helper process), first rows of a table, archive listing (nothing is4// extracted), font specimen and glyphs, subtitle cues, map of geometries, waveform, poster frame, or a5// Quick Look thumbnail for documents. Lazy, cancellable, never on the main thread; where no API exists6// the preview says so instead of inventing one.7// Author: Simon-Pierre Boucher — contact@spboucher.ai8import AppKit9import ConvertCore10import EngineArchive11import EngineData12import EngineFont13import EngineGeo14import EngineImage15import EngineMedia16import EngineModel3D17import EnginePDF18import EngineSubtitle19import Observation20import QuickLookThumbnailing2122/// A font specimen: lines rendered by the font itself, and what the font says about embedding.23nonisolated struct FontSpecimen: @unchecked Sendable {24 struct Line: @unchecked Sendable { let pointSize: Int; let image: CGImage }25 let font: LoadedFont26 let lines: [Line]27 let glyphCount: Int28 let unitsPerEm: Int29 let fullName: String?30 /// The font's own embedding statement (OS/2 fsType), verbatim from the engine; `nil` when the font has none.31 let embedding: String?32 let isRestricted: Bool33 let memberCount: Int34}3536nonisolated struct ArchiveListing: Sendable {37 var summary: ArchiveSummary38 var entries: [ArchiveEntryInfo]39 /// Entries listed so far out of `summary.entryCount`.40 var canLoadMore: Bool { entries.count < summary.entryCount }41}4243nonisolated struct SubtitleListing: Sendable {44 let cues: [SubtitleCue]45 let total: Int46 let encoding: String47}4849nonisolated enum FilePreviewContent: @unchecked Sendable {50 /// One picture (image, vector drawing, video poster frame, document thumbnail), with what it is.51 case picture(CGImage, caption: String)52 /// First pages of a PDF.53 case pages([CGImage], total: Int)54 case model(Model3DPreview)55 case table(DataPreviewResult)56 case archive(ArchiveListing)57 case font(FontSpecimen)58 case subtitles(SubtitleListing)59 case map(GeoPreviewResult)60 case waveform(AudioWaveform)61 /// No preview API for this kind of file: the inspector's facts are all there is.62 case none(String)63}6465@Observable final class FilePreviewModel {66 enum Phase: Equatable { case idle, loading, ready, failed(String) }6768 /// Bounds of every preview (pixels, rows, entries, cues, features).69 nonisolated enum Bounds {70 static let picturePixels = 160071 static let pdfPages = 372 static let pdfPagePixels = 90073 static let tableRows = 10074 static let archivePage = 20075 static let archiveScanSeconds: TimeInterval = 876 static let subtitleCues = 40077 static let mapFeatures = 40078 static let waveformBuckets = 120079 static let glyphPage = 9680 /// A model above this size is not loaded for a preview (the importer would hold it all in memory).81 static let modelBytes: Int64 = 512 << 2082 }8384 private(set) var phase: Phase = .idle85 private(set) var content: FilePreviewContent?86 private(set) var url: URL?87 private(set) var format: FormatID?88 /// Table / sheet shown by a data preview.89 private(set) var table: String?90 private(set) var isLoadingMore = false91 @ObservationIgnored private var task: Task<Void, Never>?92 @ObservationIgnored private var generation = 09394 /// `true` when a preview of some kind exists for this format in this build.95 nonisolated static func kind(for format: FormatID?) -> String? {96 guard let format, let category = FormatCatalog.format(format)?.category else { return nil }97 if DataPreview.formats.contains(format) { return "table" }98 if GeoPreview.formats.contains(format) { return "map" }99 switch category {100 case .image, .vector: return "picture"101 case .pdf: return "pages"102 case .model3D: return Model3DPreviewLoader.supportedFormats.contains(format) ? "model" : nil103 case .archive: return ArchiveEngine.archiveFormats.contains(format) ? "archive" : nil104 case .font: return "font"105 case .subtitle: return "subtitles"106 case .audio: return "waveform"107 case .video: return "picture"108 case .document, .ebook, .presentation, .spreadsheet: return "thumbnail"109 default: return nil110 }111 }112113 func clear() {114 task?.cancel()115 generation += 1116 phase = .idle; content = nil; url = nil; format = nil; table = nil117 }118119 /// Starts loading the preview of `url`; a previous load is cancelled and its result ignored.120 func load(_ url: URL, format: FormatID?, inspection: FileInspection? = nil, table: String? = nil) {121 task?.cancel()122 generation += 1123 let token = generation124 self.url = url; self.format = format; self.table = table125 guard let format, Self.kind(for: format) != nil else {126 content = FilePreviewContent.none(L("No preview exists for this kind of file. The facts read by the inspector are listed below."))127 phase = .ready128 return129 }130 if content == nil || table == nil { phase = .loading }131 let isolation = EngineCatalog.model3DIsolation132 let duration = inspection?.durationSeconds133 task = Task { [weak self] in134 let result: Result<FilePreviewContent, Error>135 do {136 result = .success(try await Task.detached(priority: .userInitiated) {137 try await Self.make(url, format: format, table: table, duration: duration, isolation: isolation)138 }.value)139 } catch { result = .failure(error) }140 guard let self, !Task.isCancelled, self.generation == token else { return }141 switch result {142 case .success(let value): self.content = value; self.phase = .ready143 case .failure(let error): self.content = nil; self.phase = .failed(LE((error as? LocalizedError)?.errorDescription ?? error.localizedDescription))144 }145 }146 }147148 /// Waits for the current load (self-test, screenshots).149 func settle() async { await task?.value }150151 /// Lists the next page of an archive.152 func loadMoreEntries() {153 guard case .archive(let listing)? = content, listing.canLoadMore, !isLoadingMore, let url, let format else { return }154 isLoadingMore = true155 let token = generation156 let offset = listing.entries.count157 Task { [weak self] in158 let page = try? await ArchiveBrowser.list(url, format: format, offset: offset, limit: Bounds.archivePage, timeLimit: Bounds.archiveScanSeconds)159 guard let self, self.generation == token else { return }160 self.isLoadingMore = false161 guard let page, case .archive(var current)? = self.content else { return }162 current.entries += page.entries163 self.content = .archive(current)164 }165 }166167 // MARK: Loading (off the main actor)168169 nonisolated private static func make(_ url: URL, format: FormatID, table: String?, duration: Double?, isolation: ImportIsolation) async throws -> FilePreviewContent {170 let category = FormatCatalog.format(format)?.category ?? .other171 if DataPreview.formats.contains(format) {172 return .table(try await DataPreview.preview(url: url, format: format, table: table, rows: Bounds.tableRows))173 }174 if GeoPreview.formats.contains(format) {175 return .map(try GeoPreview.preview(url: url, format: format, limit: Bounds.mapFeatures))176 }177 switch category {178 case .image:179 return .picture(try ImagePreview.decode(url: url, maxPixelSize: Bounds.picturePixels), caption: L("Decoded by the image engine, at most %lld px on the long side", Bounds.picturePixels))180 case .vector:181 return .picture(try await thumbnail(url), caption: L("Rendered by Quick Look"))182 case .pdf:183 let total = try PDFPreview.pageCount(of: url)184 var pages: [CGImage] = []185 for index in 0..<min(total, Bounds.pdfPages) {186 try Task.checkCancellation()187 pages.append(try PDFPreview.thumbnail(of: url, pageIndex: index, maxPixelSize: Bounds.pdfPagePixels))188 }189 return .pages(pages, total: total)190 case .model3D:191 let size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize.map(Int64.init) ?? 0192 guard size <= Bounds.modelBytes else {193 return FilePreviewContent.none(L("This model is %@: too large for a preview. It can still be converted.", Fmt.bytes(size)))194 }195 return .model(try await Model3DPreviewLoader.load(url, format: format, limits: .preview, isolation: isolation))196 case .archive:197 let page = try await ArchiveBrowser.list(url, format: format, offset: 0, limit: Bounds.archivePage, timeLimit: Bounds.archiveScanSeconds)198 return .archive(ArchiveListing(summary: page.summary, entries: page.entries))199 case .font:200 let file = try FontFile.open(url)201 let font = try LoadedFont(file: file, member: 0)202 let facts = font.facts203 let sample = "The quick brown fox jumps over the lazy dog 0123456789"204 let lines = [14, 24, 40, 64].compactMap { size -> FontSpecimen.Line? in205 font.preview(size <= 24 ? sample : "Hamburgefonstiv 123", pointSize: CGFloat(size), scale: 2).map { FontSpecimen.Line(pointSize: size, image: $0) }206 }207 return .font(FontSpecimen(font: font, lines: lines, glyphCount: font.glyphCount, unitsPerEm: font.unitsPerEm, fullName: facts.fullName ?? facts.postScriptName,208 embedding: facts.embedding?.summary, isRestricted: facts.embedding?.isRestricted ?? false, memberCount: file.fonts.count))209 case .subtitle:210 let document = try SubtitleParser.parse(url: url, format: format)211 return .subtitles(SubtitleListing(cues: Array(document.cues.prefix(Bounds.subtitleCues)), total: document.cues.count, encoding: "\(document.encoding.encoding)"))212 case .audio:213 return .waveform(try await MediaQualityLab.waveform(of: url, buckets: Bounds.waveformBuckets))214 case .video:215 let time = duration.map { min(max(0, $0 * 0.1), 10) } ?? 0216 return .picture(try await MediaQualityLab.videoFrame(of: url, atSeconds: time, maxDimension: Bounds.picturePixels),217 caption: L("Frame at %@, decoded by the bundled FFmpeg", Fmt.duration(time)))218 default:219 return .picture(try await thumbnail(url), caption: L("First page, rendered by Quick Look"))220 }221 }222223 struct ThumbnailUnavailable: LocalizedError { var errorDescription: String? { "Quick Look has no preview for this file on this Mac." } }224225 /// First page / cover through the system's Quick Look thumbnail generator (local; no network).226 nonisolated private static func thumbnail(_ url: URL) async throws -> CGImage {227 let request = QLThumbnailGenerator.Request(fileAt: url, size: CGSize(width: 900, height: 900), scale: 2, representationTypes: .thumbnail)228 do {229 return try await QLThumbnailGenerator.shared.generateBestRepresentation(for: request).cgImage230 } catch {231 throw ThumbnailUnavailable()232 }233 }234}235