From 6c1b473ab8ce04f2529393730df1c13ef85e6ccc Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Jan 2025 10:08:50 +0100 Subject: [PATCH] Write AVIF image generation script --- CHDataManagement.xcodeproj/project.pbxproj | 2 +- CHDataManagement/Generator/ImageGenerator.swift | 16 ++++++++++++++-- CHDataManagement/Model/Content+Generation.swift | 2 +- CHDataManagement/Model/FileResource.swift | 4 ++-- CHDataManagement/Storage/Storage.swift | 11 +++++++++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHDataManagement.xcodeproj/project.pbxproj b/CHDataManagement.xcodeproj/project.pbxproj index 74af446..2c944b0 100644 --- a/CHDataManagement.xcodeproj/project.pbxproj +++ b/CHDataManagement.xcodeproj/project.pbxproj @@ -656,8 +656,8 @@ E2A21C342CB9A3CA0060935B /* Settings */ = { isa = PBXGroup; children = ( - E2FE0F3D2D2B4225002963B7 /* AudioSettingsDetailView.swift */, E29D318C2D0B2E5E0051B7F4 /* Content */, + E2FE0F3D2D2B4225002963B7 /* AudioSettingsDetailView.swift */, E29D316E2D0822720051B7F4 /* SettingsListView.swift */, E29D31702D08234D0051B7F4 /* GenerationDetailView.swift */, E25DA5942D023BCC00AEF16D /* PageSettingsDetailView.swift */, diff --git a/CHDataManagement/Generator/ImageGenerator.swift b/CHDataManagement/Generator/ImageGenerator.swift index b5e1b73..a14db81 100644 --- a/CHDataManagement/Generator/ImageGenerator.swift +++ b/CHDataManagement/Generator/ImageGenerator.swift @@ -38,8 +38,20 @@ final class ImageGenerator { private var avifCommands: Set = [] - func printAvifCommands() { - avifCommands.sorted().forEach { print($0) } + /** + Write a file to the output folder containing a script to generate all missing AVIF images. + + - Note: AVIF images could be generated internally, but the process is very slow. + */ + func writeAvifCommandScript() { + guard !avifCommands.isEmpty else { + if storage.hasFileInOutputFolder("generate-images.sh") { + storage.deleteInOutputFolder("generate-images.sh") + } + return + } + let content = avifCommands.sorted().joined(separator: "\n") + storage.write(content, to: "generate-images.sh") } /** diff --git a/CHDataManagement/Model/Content+Generation.swift b/CHDataManagement/Model/Content+Generation.swift index 924c730..ae51721 100644 --- a/CHDataManagement/Model/Content+Generation.swift +++ b/CHDataManagement/Model/Content+Generation.swift @@ -81,7 +81,7 @@ extension Content { } imageGenerator.save() - imageGenerator.printAvifCommands() + imageGenerator.writeAvifCommandScript() //let images = Set(self.images.map { $0.id }) //imageGenerator.recalculateGeneratedImages(by: images) } diff --git a/CHDataManagement/Model/FileResource.swift b/CHDataManagement/Model/FileResource.swift index 249b957..273e2a7 100644 --- a/CHDataManagement/Model/FileResource.swift +++ b/CHDataManagement/Model/FileResource.swift @@ -144,7 +144,7 @@ final class FileResource: Item { func removeGeneratedImages() { guard type.isImage else { return } content.imageGenerator.removeVersions(of: id) - content.storage.deleteInOutputFolder(path: outputImageFolder) + content.storage.deleteInOutputFolder(outputImageFolder) } private var failureImage: Image { @@ -182,7 +182,7 @@ final class FileResource: Item { // MARK: Paths func removeFileFromOutputFolder() { - content.storage.deleteInOutputFolder(path: absoluteUrl) + content.storage.deleteInOutputFolder(absoluteUrl) if type.isImage { removeGeneratedImages() } diff --git a/CHDataManagement/Storage/Storage.swift b/CHDataManagement/Storage/Storage.swift index ffdfb73..2881d0d 100644 --- a/CHDataManagement/Storage/Storage.swift +++ b/CHDataManagement/Storage/Storage.swift @@ -280,9 +280,9 @@ final class Storage: ObservableObject { } @discardableResult - func deleteInOutputFolder(path: String) -> Bool { + func deleteInOutputFolder(_ relativePath: String) -> Bool { guard let outputScope else { return false } - return outputScope.deleteFile(at: path) + return outputScope.deleteFile(at: relativePath) } /** @@ -400,11 +400,18 @@ final class Storage: ObservableObject { // MARK: Output files + /** + Write the content of a file to a relative path of the output folder. + */ + @discardableResult func write(_ content: String, to relativeOutputPath: String) -> Bool { guard let outputScope else { return false } return outputScope.write(content, to: relativeOutputPath) } + /** + Write the data of a file to a relative path of the output folder. + */ func write(_ data: Data, to relativeOutputPath: String) -> Bool { guard let outputScope else { return false } return outputScope.write(data, to: relativeOutputPath)