From 36b2842ee932792f5bd7e898894475b686842b59 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 31 May 2023 22:36:23 +0200 Subject: [PATCH] Add all video versions for video input --- Sources/Generator/Files/VideoType.swift | 3 ++ .../Generators/PageContentGenerator.swift | 37 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Sources/Generator/Files/VideoType.swift b/Sources/Generator/Files/VideoType.swift index ab5105c..cdb8f6e 100644 --- a/Sources/Generator/Files/VideoType.swift +++ b/Sources/Generator/Files/VideoType.swift @@ -3,6 +3,7 @@ import Foundation enum VideoType: String, CaseIterable { case mp4 case m4v + case webm var htmlType: String { switch self { @@ -10,6 +11,8 @@ enum VideoType: String, CaseIterable { return "video/mp4" case .m4v: return "video/mp4" + case .webm: + return "video/webm" } } } diff --git a/Sources/Generator/Generators/PageContentGenerator.swift b/Sources/Generator/Generators/PageContentGenerator.swift index e26b837..6e228c7 100644 --- a/Sources/Generator/Generators/PageContentGenerator.swift +++ b/Sources/Generator/Generators/PageContentGenerator.swift @@ -201,14 +201,43 @@ struct PageContentGenerator { return option } } ?? [] - // TODO: Check page folder for alternative video versions - let sources: [PageVideoTemplate.VideoSource] = [(url: file, type: .mp4)] - let filePath = page.pathRelativeToRootForContainedInputFile(file) - results.require(file: filePath, source: page.path) + let prefix = file.lastComponentAfter("/").dropAfterLast(".") + + // Find all video files starting with the name of the video as a prefix + var sources: [PageVideoTemplate.VideoSource] = [] + do { + let folder = results.contentFolder.appendingPathComponent(page.path) + let filesInFolder = try FileManager.default.contentsOfDirectory(atPath: folder.path) + sources += selectVideoFiles(with: prefix, from: filesInFolder) + } catch { + results.warning("Failed to check for additional videos", source: page.path) + } + // Also look in external files + sources += selectVideoFiles(with: prefix, from: page.externalFiles) + + // Require all video files + sources.forEach { + let path = page.pathRelativeToRootForContainedInputFile($0.url) + results.require(file: path, source: page.path) + } + // Sort, so that order of selection in browser is defined + sources.sort { $0.url < $1.url } return factory.video.generate(sources: sources, options: options) } + private func selectVideoFiles(with prefix: String, from all: T) -> [PageVideoTemplate.VideoSource] where T: Sequence, T.Element == String { + all.compactMap { + guard $0.lastComponentAfter("/").hasPrefix(prefix) else { + return nil + } + guard let type = VideoType(rawValue: $0.lastComponentAfter(".").lowercased()) else { + return nil + } + return (url: $0, type: type) + } + } + private func handleGif(page: Element, file: String, altText: String) -> String { let imagePath = page.pathRelativeToRootForContainedInputFile(file) results.require(file: imagePath, source: page.path)