Added an optional per-asset folder to the Asset protocol and the AssetExtension extension in the Infrastructure package.
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
/// An asset shipped with a website: a file stored under the static files root and served by Hummingbird's `FileMiddleware` middleware.
|
||||
///
|
||||
/// A conforming asset supplies its file name and the extensions it is available with, each resolving to its own file; the protocol derives the paths from them:
|
||||
/// the file's path within the static files root and the URL path it is served at, optionally versioned to bust caches.
|
||||
/// the file's path within the static files root and the URL path it is served at, optionally versioned to bust caches. Each file lands in its extension's own
|
||||
/// folder unless the asset names a ``folder`` of its own.
|
||||
public protocol Asset: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The folder within the static files root that holds the asset's files, or `nil` (the default) to use each extension's own folder.
|
||||
var folder: String? { get }
|
||||
|
||||
/// The file extensions the asset is available with.
|
||||
var fileExtensions: [AssetExtension] { get }
|
||||
|
||||
@@ -18,6 +22,11 @@ public protocol Asset: Sendable {
|
||||
|
||||
public extension Asset {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The asset's files live in each extension's own folder by default.
|
||||
var folder: String? { nil }
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Resolves the asset's path against the given base directory.
|
||||
@@ -50,7 +59,7 @@ public extension Asset {
|
||||
) -> String {
|
||||
let file = "\(fileName).\(fileExtension.rawValue)"
|
||||
|
||||
return fileExtension.subdirectory
|
||||
return (folder ?? fileExtension.folder)
|
||||
.map { "\($0)/\(file)" } ?? file
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user