Skip to content

Commit 1497ac9

Browse files
committed
Keep compatiblity with non-Apple OS
1 parent 957494a commit 1497ac9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

FlyingFox/Sources/Handlers/FileHTTPHandler.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131

3232
import FlyingSocks
3333
import Foundation
34+
#if canImport(UniformTypeIdentifiers)
3435
import UniformTypeIdentifiers
36+
#endif
3537

3638
public struct FileHTTPHandler: HTTPHandler {
3739

@@ -50,6 +52,7 @@ public struct FileHTTPHandler: HTTPHandler {
5052

5153
static func makeContentType(for filename: String) -> String {
5254
let pathExtension = (filename.lowercased() as NSString).pathExtension
55+
#if canImport(UniformTypeIdentifiers)
5356
switch pathExtension {
5457
case "wasm":
5558
return "application/wasm"
@@ -58,6 +61,46 @@ public struct FileHTTPHandler: HTTPHandler {
5861
default:
5962
return UTType(filenameExtension: pathExtension)?.preferredMIMEType ?? "application/octet-stream"
6063
}
64+
#else
65+
switch pathExtension {
66+
case "json":
67+
return "application/json"
68+
case "html", "htm":
69+
return "text/html"
70+
case "css":
71+
return "text/css"
72+
case "js", "javascript":
73+
return "text/javascript"
74+
case "png":
75+
return "image/png"
76+
case "jpeg", "jpg":
77+
return "image/jpeg"
78+
case "mp4":
79+
return "video/mp4"
80+
case "m4v":
81+
return "video/x-m4v"
82+
case "pdf":
83+
return "application/pdf"
84+
case "svg":
85+
return "image/svg+xml"
86+
case "txt":
87+
return "text/plain"
88+
case "ico":
89+
return "image/vnd.microsoft.icon"
90+
case "wasm":
91+
return "application/wasm"
92+
case "webp":
93+
return "image/webp"
94+
case "jp2":
95+
return "image/jp2"
96+
case "properties":
97+
return "text/plain"
98+
case "xml":
99+
return "application/xml"
100+
default:
101+
return "application/octet-stream"
102+
}
103+
#endif
61104
}
62105

63106
public func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse {

FlyingFox/Tests/Handlers/HTTPHandlerTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ struct HTTPHandlerTests {
179179
#expect(
180180
FileHTTPHandler.makeContentType(for: "fish.properties") == "text/plain"
181181
)
182+
#expect(
183+
FileHTTPHandler.makeContentType(for: "fish.xml") == "application/xml"
184+
)
182185
#expect(
183186
FileHTTPHandler.makeContentType(for: "fish.somefile") == "application/octet-stream"
184187
)

0 commit comments

Comments
 (0)