2026-06-27 10:59:32 +00:00
|
|
|
/// A static file shipped with the website service.
|
|
|
|
|
///
|
2026-07-19 02:08:59 +00:00
|
|
|
/// Each case identifies a file name stored under the static files root (the `Resources/Static`
|
|
|
|
|
/// directory) and served by Hummingbird's `FileMiddleware` middleware. A name can be available
|
|
|
|
|
/// with more than one extension (see ``fileExtensions``), each resolving to its own file.
|
2026-06-27 10:59:32 +00:00
|
|
|
enum StaticFile: CaseIterable, Sendable {
|
2026-07-19 02:08:59 +00:00
|
|
|
/// The `apple-touch-icon.png` icon.
|
|
|
|
|
case appleTouchIcon
|
|
|
|
|
/// The `css/error.css` stylesheet and `js/error.js` script for the not-found page.
|
|
|
|
|
case error
|
2026-06-27 10:59:32 +00:00
|
|
|
/// The `favicon.ico` icon.
|
2026-07-19 02:08:59 +00:00
|
|
|
case favicon
|
2026-07-19 03:35:50 +00:00
|
|
|
/// The `icon.svg` icon.
|
2026-07-19 02:08:59 +00:00
|
|
|
case icon
|
2026-07-19 03:35:50 +00:00
|
|
|
/// The `icon-192.png` icon for the web manifest.
|
|
|
|
|
case icon192
|
|
|
|
|
/// The `icon-512.png` icon for the web manifest.
|
|
|
|
|
case icon512
|
2026-07-19 02:08:59 +00:00
|
|
|
/// The `css/index.css` stylesheet and `js/index.js` script for the landing page.
|
|
|
|
|
case index
|
2026-06-27 10:59:32 +00:00
|
|
|
/// The `robots.txt` crawler directives.
|
2026-07-19 02:08:59 +00:00
|
|
|
case robots
|
|
|
|
|
/// The `css/shared.css` stylesheet and `js/shared.js` script shared across pages.
|
|
|
|
|
case shared
|
2026-06-27 10:59:32 +00:00
|
|
|
/// The `site.webmanifest` web application manifest.
|
2026-07-19 02:08:59 +00:00
|
|
|
case site
|
|
|
|
|
/// The `sitemap.xml` crawler sitemap.
|
|
|
|
|
case sitemap
|
2026-06-27 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Enumerations
|
|
|
|
|
|
|
|
|
|
extension StaticFile {
|
|
|
|
|
/// A file extension used by a ``StaticFile``.
|
|
|
|
|
enum Extension: String, Sendable {
|
|
|
|
|
/// A Cascading Style Sheets file.
|
|
|
|
|
case css
|
|
|
|
|
/// A JavaScript file.
|
|
|
|
|
case js
|
|
|
|
|
/// A Portable Network Graphics image.
|
|
|
|
|
case png
|
|
|
|
|
/// A Windows icon image.
|
|
|
|
|
case ico
|
|
|
|
|
/// A Scalable Vector Graphics image.
|
|
|
|
|
case svg
|
|
|
|
|
/// A plain text file.
|
|
|
|
|
case txt
|
|
|
|
|
/// A web application manifest file.
|
|
|
|
|
case webmanifest
|
2026-07-19 02:08:59 +00:00
|
|
|
/// An Extensible Markup Language file.
|
|
|
|
|
case xml
|
2026-06-27 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Extensions
|
|
|
|
|
|
|
|
|
|
extension StaticFile {
|
|
|
|
|
|
|
|
|
|
// MARK: Computed
|
|
|
|
|
|
2026-07-19 02:08:59 +00:00
|
|
|
/// The file extensions the file is available with.
|
|
|
|
|
var fileExtensions: [Extension] {
|
|
|
|
|
switch self {
|
2026-07-19 03:35:50 +00:00
|
|
|
case .appleTouchIcon,
|
|
|
|
|
.icon192,
|
|
|
|
|
.icon512: [.png]
|
2026-07-19 02:08:59 +00:00
|
|
|
case .error,
|
|
|
|
|
.index,
|
|
|
|
|
.shared: [.css, .js]
|
|
|
|
|
case .favicon: [.ico]
|
2026-07-19 03:35:50 +00:00
|
|
|
case .icon: [.svg]
|
2026-07-19 02:08:59 +00:00
|
|
|
case .robots: [.txt]
|
|
|
|
|
case .site: [.webmanifest]
|
|
|
|
|
case .sitemap: [.xml]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The file's name, without extension.
|
|
|
|
|
var fileName: String {
|
|
|
|
|
switch self {
|
|
|
|
|
case .appleTouchIcon: "apple-touch-icon"
|
|
|
|
|
case .error: "error"
|
|
|
|
|
case .favicon: "favicon"
|
|
|
|
|
case .icon: "icon"
|
2026-07-19 03:35:50 +00:00
|
|
|
case .icon192: "icon-192"
|
|
|
|
|
case .icon512: "icon-512"
|
2026-07-19 02:08:59 +00:00
|
|
|
case .index: "index"
|
|
|
|
|
case .robots: "robots"
|
|
|
|
|
case .shared: "shared"
|
|
|
|
|
case .site: "site"
|
|
|
|
|
case .sitemap: "sitemap"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Methods
|
|
|
|
|
|
|
|
|
|
/// Resolves the file's path against the given base directory.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameters:
|
|
|
|
|
/// - basePath: the directory the static files are served from.
|
|
|
|
|
/// - fileExtension: the extension of the file to resolve.
|
|
|
|
|
/// - Returns: the path to the file, relative to the `basePath` path.
|
|
|
|
|
func path(
|
|
|
|
|
relativeTo basePath: String,
|
|
|
|
|
for fileExtension: Extension
|
|
|
|
|
) -> String {
|
|
|
|
|
let relativePath = relativePath(for: fileExtension)
|
|
|
|
|
|
|
|
|
|
guard !basePath.isEmpty else {
|
|
|
|
|
return relativePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "\(basePath)/\(relativePath)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Resolves the file's path relative to the static files root (e.g. `"css/shared.css"`).
|
|
|
|
|
///
|
|
|
|
|
/// This also matches the URL path the file is served at by `FileMiddleware`.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameter fileExtension: the extension of the file to resolve.
|
|
|
|
|
/// - Returns: the path to the file, relative to the static files root.
|
|
|
|
|
func relativePath(
|
|
|
|
|
for fileExtension: Extension
|
|
|
|
|
) -> String {
|
|
|
|
|
let file = "\(fileName).\(fileExtension.rawValue)"
|
|
|
|
|
|
|
|
|
|
return fileExtension.subdirectory
|
|
|
|
|
.map { "\($0)/\(file)" } ?? file
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Resolves the absolute URL path the file is served at (e.g. `"/css/shared.css"`).
|
|
|
|
|
///
|
|
|
|
|
/// - Parameter fileExtension: the extension of the file to resolve.
|
|
|
|
|
/// - Returns: the path to use in `href` and `src` attributes.
|
|
|
|
|
func urlPath(
|
|
|
|
|
for fileExtension: Extension
|
|
|
|
|
) -> String {
|
|
|
|
|
"/\(relativePath(for: fileExtension))"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension StaticFile.Extension {
|
|
|
|
|
|
|
|
|
|
// MARK: Computed
|
|
|
|
|
|
2026-06-27 10:59:32 +00:00
|
|
|
/// The file's content type.
|
|
|
|
|
var contentType: String {
|
2026-07-19 02:08:59 +00:00
|
|
|
switch self {
|
2026-06-27 10:59:32 +00:00
|
|
|
case .css: "text/css"
|
|
|
|
|
case .js: "text/javascript"
|
|
|
|
|
case .png: "image/png"
|
|
|
|
|
case .ico: "image/vnd.microsoft.icon"
|
|
|
|
|
case .svg: "image/svg+xml"
|
|
|
|
|
case .txt: "text/plain"
|
|
|
|
|
case .webmanifest: "application/manifest+json"
|
2026-07-19 02:08:59 +00:00
|
|
|
case .xml: "application/xml"
|
2026-06-27 10:59:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 02:08:59 +00:00
|
|
|
/// The sub-directory within the static root that holds files with this extension, if any.
|
2026-06-27 10:59:32 +00:00
|
|
|
var subdirectory: String? {
|
|
|
|
|
switch self {
|
2026-07-19 02:08:59 +00:00
|
|
|
case .css: "css"
|
|
|
|
|
case .js: "js"
|
2026-06-27 10:59:32 +00:00
|
|
|
default: nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|