Skip to content

Commit f4f7f8e

Browse files
author
Daniel Große
committed
Adds support for mimetypes
1 parent 3c86f8e commit f4f7f8e

File tree

3 files changed

+167
-8
lines changed

3 files changed

+167
-8
lines changed

Sources/Files.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
3535
}
3636
}
3737
if let file = try? (directoryPath + String.pathSeparator + fileRelativePath.value).openForReading() {
38-
return .raw(200, "OK", [:], { writer in
38+
let mimeType = fileRelativePath.value.mimeType();
39+
40+
return .raw(200, "OK", ["Content-Type": mimeType], { writer in
3941
try? writer.write(file)
4042
file.close()
4143
})

Sources/MimeTypes.swift

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// MimeTypes.swift
3+
// Swifter
4+
//
5+
// Created by Daniel Große on 16.02.18.
6+
//
7+
8+
import Foundation
9+
10+
internal let DEFAULT_MIME_TYPE = "application/octet-stream"
11+
12+
internal let mimeTypes = [
13+
"html": "text/html",
14+
"htm": "text/html",
15+
"shtml": "text/html",
16+
"css": "text/css",
17+
"xml": "text/xml",
18+
"gif": "image/gif",
19+
"jpeg": "image/jpeg",
20+
"jpg": "image/jpeg",
21+
"js": "application/javascript",
22+
"atom": "application/atom+xml",
23+
"rss": "application/rss+xml",
24+
"mml": "text/mathml",
25+
"txt": "text/plain",
26+
"jad": "text/vnd.sun.j2me.app-descriptor",
27+
"wml": "text/vnd.wap.wml",
28+
"htc": "text/x-component",
29+
"png": "image/png",
30+
"tif": "image/tiff",
31+
"tiff": "image/tiff",
32+
"wbmp": "image/vnd.wap.wbmp",
33+
"ico": "image/x-icon",
34+
"jng": "image/x-jng",
35+
"bmp": "image/x-ms-bmp",
36+
"svg": "image/svg+xml",
37+
"svgz": "image/svg+xml",
38+
"webp": "image/webp",
39+
"woff": "application/font-woff",
40+
"jar": "application/java-archive",
41+
"war": "application/java-archive",
42+
"ear": "application/java-archive",
43+
"json": "application/json",
44+
"hqx": "application/mac-binhex40",
45+
"doc": "application/msword",
46+
"pdf": "application/pdf",
47+
"ps": "application/postscript",
48+
"eps": "application/postscript",
49+
"ai": "application/postscript",
50+
"rtf": "application/rtf",
51+
"m3u8": "application/vnd.apple.mpegurl",
52+
"xls": "application/vnd.ms-excel",
53+
"eot": "application/vnd.ms-fontobject",
54+
"ppt": "application/vnd.ms-powerpoint",
55+
"wmlc": "application/vnd.wap.wmlc",
56+
"kml": "application/vnd.google-earth.kml+xml",
57+
"kmz": "application/vnd.google-earth.kmz",
58+
"7z": "application/x-7z-compressed",
59+
"cco": "application/x-cocoa",
60+
"jardiff": "application/x-java-archive-diff",
61+
"jnlp": "application/x-java-jnlp-file",
62+
"run": "application/x-makeself",
63+
"pl": "application/x-perl",
64+
"pm": "application/x-perl",
65+
"prc": "application/x-pilot",
66+
"pdb": "application/x-pilot",
67+
"rar": "application/x-rar-compressed",
68+
"rpm": "application/x-redhat-package-manager",
69+
"sea": "application/x-sea",
70+
"swf": "application/x-shockwave-flash",
71+
"sit": "application/x-stuffit",
72+
"tcl": "application/x-tcl",
73+
"tk": "application/x-tcl",
74+
"der": "application/x-x509-ca-cert",
75+
"pem": "application/x-x509-ca-cert",
76+
"crt": "application/x-x509-ca-cert",
77+
"xpi": "application/x-xpinstall",
78+
"xhtml": "application/xhtml+xml",
79+
"xspf": "application/xspf+xml",
80+
"zip": "application/zip",
81+
"bin": "application/octet-stream",
82+
"exe": "application/octet-stream",
83+
"dll": "application/octet-stream",
84+
"deb": "application/octet-stream",
85+
"dmg": "application/octet-stream",
86+
"iso": "application/octet-stream",
87+
"img": "application/octet-stream",
88+
"msi": "application/octet-stream",
89+
"msp": "application/octet-stream",
90+
"msm": "application/octet-stream",
91+
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
92+
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
93+
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
94+
"mid": "audio/midi",
95+
"midi": "audio/midi",
96+
"kar": "audio/midi",
97+
"mp3": "audio/mpeg",
98+
"ogg": "audio/ogg",
99+
"m4a": "audio/x-m4a",
100+
"ra": "audio/x-realaudio",
101+
"3gpp": "video/3gpp",
102+
"3gp": "video/3gpp",
103+
"ts": "video/mp2t",
104+
"mp4": "video/mp4",
105+
"mpeg": "video/mpeg",
106+
"mpg": "video/mpeg",
107+
"mov": "video/quicktime",
108+
"webm": "video/webm",
109+
"flv": "video/x-flv",
110+
"m4v": "video/x-m4v",
111+
"mng": "video/x-mng",
112+
"asx": "video/x-ms-asf",
113+
"asf": "video/x-ms-asf",
114+
"wmv": "video/x-ms-wmv",
115+
"avi": "video/x-msvideo"
116+
]
117+
118+
internal func MimeType(ext: String?) -> String {
119+
if ext != nil && mimeTypes.contains(where: { $0.0 == ext!.lowercased() }) {
120+
return mimeTypes[ext!.lowercased()]!
121+
}
122+
return DEFAULT_MIME_TYPE
123+
}
124+
125+
extension NSURL {
126+
public func mimeType() -> String {
127+
return MimeType(ext: self.pathExtension)
128+
}
129+
}
130+
131+
extension NSString {
132+
public func mimeType() -> String {
133+
return MimeType(ext: self.pathExtension)
134+
}
135+
}
136+
137+
extension String {
138+
public func mimeType() -> String {
139+
return (self as NSString).mimeType()
140+
}
141+
}
142+
143+

XCode/Swifter.xcodeproj/project.pbxproj

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
269B47981D3AAAE20042D137 /* Errno.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B2A11D369C9D00D35BFB /* Errno.swift */; };
3131
269B47991D3AAAE20042D137 /* String+BASE64.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B6F61D2C44F30030FC98 /* String+BASE64.swift */; };
3232
269B47A71D3AAC4F0042D137 /* SwiftertvOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 269B47A51D3AAC4F0042D137 /* SwiftertvOS.h */; settings = {ATTRIBUTES = (Public, ); }; };
33+
6AE2FF722048013000302EC4 /* MimeTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE2FF702048011A00302EC4 /* MimeTypes.swift */; };
34+
6AE2FF732048013000302EC4 /* MimeTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE2FF702048011A00302EC4 /* MimeTypes.swift */; };
35+
6AE2FF742048013100302EC4 /* MimeTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE2FF702048011A00302EC4 /* MimeTypes.swift */; };
36+
6AE2FF752048013300302EC4 /* MimeTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE2FF702048011A00302EC4 /* MimeTypes.swift */; };
37+
6AE2FF762048013500302EC4 /* MimeTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6AE2FF702048011A00302EC4 /* MimeTypes.swift */; };
3338
7AE893EA1C05127900A29F63 /* SwifteriOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 7AE893E91C05127900A29F63 /* SwifteriOS.h */; settings = {ATTRIBUTES = (Public, ); }; };
3439
7AE893FE1C0512C400A29F63 /* SwifterMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 7AE893FD1C0512C400A29F63 /* SwifterMac.h */; settings = {ATTRIBUTES = (Public, ); }; };
3540
7AE8940D1C05151100A29F63 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7AE8940C1C05151100A29F63 /* Launch Screen.storyboard */; };
@@ -150,6 +155,7 @@
150155
269B47A11D3AAAE20042D137 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
151156
269B47A41D3AAC4F0042D137 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
152157
269B47A51D3AAC4F0042D137 /* SwiftertvOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftertvOS.h; sourceTree = "<group>"; };
158+
6AE2FF702048011A00302EC4 /* MimeTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MimeTypes.swift; sourceTree = "<group>"; };
153159
7AE893E71C05127900A29F63 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
154160
7AE893E91C05127900A29F63 /* SwifteriOS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwifteriOS.h; sourceTree = "<group>"; };
155161
7AE893EB1C05127900A29F63 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -177,7 +183,7 @@
177183
7C76B6F71D2C44F30030FC98 /* String+Misc.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Misc.swift"; sourceTree = "<group>"; };
178184
7C76B6F81D2C44F30030FC98 /* String+SHA1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+SHA1.swift"; sourceTree = "<group>"; };
179185
7C76B6F91D2C44F30030FC98 /* WebSockets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebSockets.swift; sourceTree = "<group>"; };
180-
7C839B6E19422CFF003A6950 /* SwifterSampleiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwifterSampleiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
186+
7C839B6E19422CFF003A6950 /* .app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = .app; sourceTree = BUILT_PRODUCTS_DIR; };
181187
7CA4813B19A2EA8D0030B30D /* SwifterSampleOSX */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwifterSampleOSX; sourceTree = BUILT_PRODUCTS_DIR; };
182188
7CA4813D19A2EA8D0030B30D /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
183189
7CB102DF1A17381D00CBA3B4 /* logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo.png; sourceTree = "<group>"; };
@@ -298,6 +304,7 @@
298304
7C76B6F91D2C44F30030FC98 /* WebSockets.swift */,
299305
7C76B2A11D369C9D00D35BFB /* Errno.swift */,
300306
7C377E161D964B6A009C6148 /* String+File.swift */,
307+
6AE2FF702048011A00302EC4 /* MimeTypes.swift */,
301308
);
302309
name = Sources;
303310
path = ../Sources;
@@ -323,7 +330,7 @@
323330
7C839B6F19422CFF003A6950 /* Products */ = {
324331
isa = PBXGroup;
325332
children = (
326-
7C839B6E19422CFF003A6950 /* SwifterSampleiOS.app */,
333+
7C839B6E19422CFF003A6950 /* .app */,
327334
7CA4813B19A2EA8D0030B30D /* SwifterSampleOSX */,
328335
7AE893E71C05127900A29F63 /* Swifter.framework */,
329336
7AE893FB1C0512C400A29F63 /* Swifter.framework */,
@@ -491,7 +498,7 @@
491498
);
492499
name = SwifterSampleiOS;
493500
productName = Swifter;
494-
productReference = 7C839B6E19422CFF003A6950 /* SwifterSampleiOS.app */;
501+
productReference = 7C839B6E19422CFF003A6950 /* .app */;
495502
productType = "com.apple.product-type.application";
496503
};
497504
7CA4813A19A2EA8D0030B30D /* SwifterSampleOSX */ = {
@@ -671,6 +678,7 @@
671678
269B47951D3AAAE20042D137 /* Files.swift in Sources */,
672679
2659FC1A1DADC077003F3930 /* String+File.swift in Sources */,
673680
269B47961D3AAAE20042D137 /* HttpRouter.swift in Sources */,
681+
6AE2FF742048013100302EC4 /* MimeTypes.swift in Sources */,
674682
269B47971D3AAAE20042D137 /* String+SHA1.swift in Sources */,
675683
7C458EFE1D4A7526006A68E5 /* Socket+Server.swift in Sources */,
676684
269B47981D3AAAE20042D137 /* Errno.swift in Sources */,
@@ -697,6 +705,7 @@
697705
7C76B71D1D2C45820030FC98 /* HttpServerIO.swift in Sources */,
698706
7C76B7111D2C45710030FC98 /* Files.swift in Sources */,
699707
7C76B7191D2C457C0030FC98 /* HttpRouter.swift in Sources */,
708+
6AE2FF722048013000302EC4 /* MimeTypes.swift in Sources */,
700709
7C76B7291D2C45920030FC98 /* String+SHA1.swift in Sources */,
701710
7C458EFC1D4A7526006A68E5 /* Socket+Server.swift in Sources */,
702711
7C76B2A21D369C9D00D35BFB /* Errno.swift in Sources */,
@@ -723,6 +732,7 @@
723732
7C76B71E1D2C45820030FC98 /* HttpServerIO.swift in Sources */,
724733
7C76B7121D2C45710030FC98 /* Files.swift in Sources */,
725734
7C76B71A1D2C457C0030FC98 /* HttpRouter.swift in Sources */,
735+
6AE2FF732048013000302EC4 /* MimeTypes.swift in Sources */,
726736
7C76B72A1D2C45920030FC98 /* String+SHA1.swift in Sources */,
727737
7C458EFD1D4A7526006A68E5 /* Socket+Server.swift in Sources */,
728738
7C76B2A31D369C9D00D35BFB /* Errno.swift in Sources */,
@@ -752,6 +762,7 @@
752762
buildActionMask = 2147483647;
753763
files = (
754764
7CCD87701C660B250068099B /* SwifterTestsHttpParser.swift in Sources */,
765+
6AE2FF752048013300302EC4 /* MimeTypes.swift in Sources */,
755766
0858E7F81D68BC2600491CD1 /* PingServer.swift in Sources */,
756767
0858E7F41D68BB2600491CD1 /* IOSafetyTests.swift in Sources */,
757768
7C4785E91C71D15600A9FE73 /* SwifterTestsWebSocketSession.swift in Sources */,
@@ -773,6 +784,7 @@
773784
7CEBB8741D94612D00370A6B /* HttpServer.swift in Sources */,
774785
7CEBB8751D94612D00370A6B /* HttpServerIO.swift in Sources */,
775786
7CEBB8761D94612D00370A6B /* Process.swift in Sources */,
787+
6AE2FF762048013500302EC4 /* MimeTypes.swift in Sources */,
776788
7CEBB8771D94612D00370A6B /* Scopes.swift in Sources */,
777789
7CEBB8781D94612D00370A6B /* Socket.swift in Sources */,
778790
7CEBB8791D94612D00370A6B /* Socket+File.swift in Sources */,
@@ -1110,12 +1122,13 @@
11101122
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
11111123
CLANG_ENABLE_MODULES = YES;
11121124
INFOPLIST_FILE = "$(SRCROOT)/SwifterSampleiOS/Info.plist";
1125+
IPHONEOS_DEPLOYMENT_TARGET = 10.12;
11131126
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
11141127
PRODUCT_BUNDLE_IDENTIFIER = "pl.kolakowski.${PRODUCT_NAME:rfc1034identifier}";
1115-
PRODUCT_NAME = "$(TARGET_NAME)";
1116-
SDKROOT = iphoneos;
1128+
SDKROOT = macosx10.13;
11171129
SWIFT_OBJC_BRIDGING_HEADER = "";
11181130
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
1131+
TARGETED_DEVICE_FAMILY = "1,2";
11191132
};
11201133
name = Debug;
11211134
};
@@ -1125,11 +1138,12 @@
11251138
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
11261139
CLANG_ENABLE_MODULES = YES;
11271140
INFOPLIST_FILE = "$(SRCROOT)/SwifterSampleiOS/Info.plist";
1141+
IPHONEOS_DEPLOYMENT_TARGET = 10.12;
11281142
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
11291143
PRODUCT_BUNDLE_IDENTIFIER = "pl.kolakowski.${PRODUCT_NAME:rfc1034identifier}";
1130-
PRODUCT_NAME = "$(TARGET_NAME)";
1131-
SDKROOT = iphoneos;
1144+
SDKROOT = macosx10.13;
11321145
SWIFT_OBJC_BRIDGING_HEADER = "";
1146+
TARGETED_DEVICE_FAMILY = "1,2";
11331147
};
11341148
name = Release;
11351149
};

0 commit comments

Comments
 (0)