Skip to content

Commit b2ab0f7

Browse files
committed
SwiftDocC: implement mime type sniffing for Windows
Windows can support the mime type sniffing via URL Monikers (similar to how Darwin can use UTI). This requires a newer toolchain as it requires a modified WinSDK module.
1 parent 150013e commit b2ab0f7

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Sources/SwiftDocC/Servers/FileServer.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import SymbolKit
1313
#if canImport(FoundationNetworking)
1414
import FoundationNetworking
1515
#endif
16+
#if os(Windows)
17+
import WinSDK
18+
#endif
1619

1720
fileprivate let slashCharSet = CharacterSet(charactersIn: "/")
1821

@@ -122,7 +125,19 @@ public class FileServer {
122125
}
123126

124127
return (mimeType as NSString) as String
125-
128+
129+
#elseif os(Windows)
130+
131+
return ".\(ext)".withCString(encodedAs: UTF16.self) {
132+
var pwszMimeOut: UnsafeMutablePointer<WCHAR>?
133+
guard FindMimeFromData(nil, $0, nil, 0, nil, DWORD(FMFD_URLASFILENAME), &pwszMimeOut, 0) >= 0,
134+
let pwszMimeOut = pwszMimeOut else {
135+
return defaultMimeType
136+
}
137+
defer { CoTaskMemFree(pwszMimeOut) }
138+
return String(decodingCString: pwszMimeOut, as: UTF16.self)
139+
}
140+
126141
#else
127142

128143
let mimeTypes = [

0 commit comments

Comments
 (0)