Add all video versions for video input

This commit is contained in:
Christoph Hagen 2023-05-31 22:36:23 +02:00
parent 884d456e48
commit 36b2842ee9
2 changed files with 36 additions and 4 deletions

View File

@ -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"
}
}
}

View File

@ -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<T>(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)