Skip to content

Commit

Permalink
rename mediaTypeFileExtensionMap for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
mredig committed Jan 16, 2025
1 parent c69b929 commit 5740138
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions Sources/Hummingbird/Middleware/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where Provider.FileAttributes: FileMiddlewareFileAttributes {
let searchForIndexHtml: Bool
let urlBasePath: String?
let fileProvider: Provider
let additionalMediaTypeExtensions: [String: MediaType]
let mediaTypeFileExtensionMap: [String: MediaType]

/// Create FileMiddleware
/// - Parameters:
Expand All @@ -79,7 +79,7 @@ where Provider.FileAttributes: FileMiddlewareFileAttributes {
urlBasePath: urlBasePath,
cacheControl: cacheControl,
searchForIndexHtml: searchForIndexHtml,
additionalMediaTypeExtensions: [:]
mediaTypeFileExtensionMap: [:]
)
}

Expand All @@ -100,7 +100,7 @@ where Provider.FileAttributes: FileMiddlewareFileAttributes {
urlBasePath: urlBasePath,
cacheControl: cacheControl,
searchForIndexHtml: searchForIndexHtml,
additionalMediaTypeExtensions: [:]
mediaTypeFileExtensionMap: [:]
)
}

Expand All @@ -109,24 +109,24 @@ where Provider.FileAttributes: FileMiddlewareFileAttributes {
urlBasePath: String? = nil,
cacheControl: CacheControl = .init([]),
searchForIndexHtml: Bool = false,
additionalMediaTypeExtensions: [String: MediaType]
mediaTypeFileExtensionMap: [String: MediaType]
) {
self.cacheControl = cacheControl
self.searchForIndexHtml = searchForIndexHtml
self.urlBasePath = urlBasePath.map { String($0.dropSuffix("/")) }
self.fileProvider = fileProvider
self.additionalMediaTypeExtensions = additionalMediaTypeExtensions
self.mediaTypeFileExtensionMap = mediaTypeFileExtensionMap
}

public func withAdditionalMediaType(_ mediaType: MediaType, forFileExtension fileExtension: String) -> FileMiddleware {
var extensions = additionalMediaTypeExtensions
public func withAdditionalMediaType(_ mediaType: MediaType, mappedToFileExtension fileExtension: String) -> FileMiddleware {
var extensions = mediaTypeFileExtensionMap
extensions[fileExtension] = mediaType
return FileMiddleware(
fileProvider: fileProvider,
urlBasePath: urlBasePath,
cacheControl: cacheControl,
searchForIndexHtml: searchForIndexHtml,
additionalMediaTypeExtensions: extensions
mediaTypeFileExtensionMap: extensions
)
}

Expand Down Expand Up @@ -253,7 +253,7 @@ extension FileMiddleware {

// content-type
if let ext = self.fileExtension(for: path) {
if let contentType = additionalMediaTypeExtensions[ext] ?? MediaType.getMediaType(forExtension: ext) {
if let contentType = mediaTypeFileExtensionMap[ext] ?? MediaType.getMediaType(forExtension: ext) {
headers[.contentType] = contentType.description
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdTests/FileMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ final class FileMiddlewareTests: XCTestCase {
func testCustomMIMEType() async throws {
let hlsStream = try XCTUnwrap(MediaType(from: "application/x-mpegURL"))
let router = Router()
router.middlewares.add(FileMiddleware(".").withAdditionalMediaType(hlsStream, forFileExtension: "m3u8"))
router.middlewares.add(FileMiddleware(".").withAdditionalMediaType(hlsStream, mappedToFileExtension: "m3u8"))
let app = Application(responder: router.buildResponder())

let filename = "\(#function).m3u8"
Expand Down

0 comments on commit 5740138

Please sign in to comment.