├── .gitignore ├── .swift-version ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── MimeType.swift └── Tests ├── LinuxMain.swift └── MimeTypeTests └── MimeTypeTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/swift,xcode,objective-c,osx 3 | 4 | ### Swift ### 5 | # Xcode 6 | # 7 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 8 | 9 | ## Build generated 10 | build/ 11 | DerivedData/ 12 | 13 | ## Various settings 14 | *.pbxuser 15 | !default.pbxuser 16 | *.mode1v3 17 | !default.mode1v3 18 | *.mode2v3 19 | !default.mode2v3 20 | *.perspectivev3 21 | !default.perspectivev3 22 | xcuserdata/ 23 | 24 | ## Other 25 | *.moved-aside 26 | *.xcuserstate 27 | 28 | ## Obj-C/Swift specific 29 | *.hmap 30 | *.ipa 31 | *.dSYM.zip 32 | *.dSYM 33 | 34 | ## Playgrounds 35 | timeline.xctimeline 36 | playground.xcworkspace 37 | 38 | # Swift Package Manager 39 | # 40 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 41 | # Packages/ 42 | .build/ 43 | 44 | # CocoaPods 45 | # 46 | # We recommend against adding the Pods directory to your .gitignore. However 47 | # you should judge for yourself, the pros and cons are mentioned at: 48 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 49 | # 50 | # Pods/ 51 | 52 | # Carthage 53 | # 54 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 55 | # Carthage/Checkouts 56 | 57 | Carthage/Build 58 | 59 | # fastlane 60 | # 61 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 62 | # screenshots whenever they are needed. 63 | # For more information about the recommended setup visit: 64 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 65 | 66 | fastlane/report.xml 67 | fastlane/Preview.html 68 | fastlane/screenshots 69 | fastlane/test_output 70 | 71 | 72 | ### Xcode ### 73 | # Xcode 74 | # 75 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 76 | 77 | ## Build generated 78 | 79 | ## Various settings 80 | 81 | ## Other 82 | *.xccheckout 83 | *.xcscmblueprint 84 | 85 | 86 | ### Objective-C ### 87 | # Xcode 88 | # 89 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 90 | 91 | ## Build generated 92 | 93 | ## Various settings 94 | 95 | ## Other 96 | 97 | ## Obj-C/Swift specific 98 | 99 | # CocoaPods 100 | # 101 | # We recommend against adding the Pods directory to your .gitignore. However 102 | # you should judge for yourself, the pros and cons are mentioned at: 103 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 104 | # 105 | # Pods/ 106 | 107 | # Carthage 108 | # 109 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 110 | # Carthage/Checkouts 111 | 112 | 113 | # fastlane 114 | # 115 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 116 | # screenshots whenever they are needed. 117 | # For more information about the recommended setup visit: 118 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 119 | 120 | 121 | # Code Injection 122 | # 123 | # After new code Injection tools there's a generated folder /iOSInjectionProject 124 | # https://github.com/johnno1962/injectionforxcode 125 | 126 | iOSInjectionProject/ 127 | 128 | ### Objective-C Patch ### 129 | 130 | 131 | ### OSX ### 132 | *.DS_Store 133 | .AppleDouble 134 | .LSOverride 135 | 136 | # Icon must end with two \r 137 | Icon 138 | # Thumbnails 139 | ._* 140 | # Files that might appear in the root of a volume 141 | .DocumentRevisions-V100 142 | .fseventsd 143 | .Spotlight-V100 144 | .TemporaryItems 145 | .Trashes 146 | .VolumeIcon.icns 147 | .com.apple.timemachine.donotpresent 148 | # Directories potentially created on remote AFP share 149 | .AppleDB 150 | .AppleDesktop 151 | Network Trash Folder 152 | Temporary Items 153 | .apdisk 154 | 155 | # End of https://www.gitignore.io/api/swift,xcode,objective-c,osx 156 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 3.0.2 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Wei Wang 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | import PackageDescription 2 | 3 | let package = Package( 4 | name: "MimeType" 5 | ) 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MimeType 2 | 3 | Get mime type from a file path or url extension. Like "text/html" for "/var/www/public/index.html" or "image/png" for "/home/user/cat.png". 4 | 5 | ## Installation 6 | 7 | Add the url of this repo to your `Package.swift`: 8 | 9 | ```swift 10 | import PackageDescription 11 | 12 | let package = Package( 13 | name: "YourAwesomeSoftware", 14 | dependencies: [ 15 | .Package(url: "https://github.com/onevcat/MimeType.git", 16 | majorVersion: 1) 17 | ] 18 | ) 19 | ``` 20 | 21 | Then run `swift build` whenever you get prepared. 22 | 23 | You could know more information on how to use Swift Package Manager in Apple's [official page](https://swift.org/package-manager/). 24 | 25 | ## Usage 26 | 27 | ```swift 28 | let path = "/var/www/public/index.html" 29 | let mime = MimeType(path: path).value 30 | // "text/html" 31 | 32 | let url = URL(fileURLWithPath: "/home/user/cat.png") 33 | let anotherMime = MimeType(url: url).value 34 | // "image/png" 35 | ``` 36 | 37 | ## License 38 | 39 | MIT. See the LICENSE file. 40 | -------------------------------------------------------------------------------- /Sources/MimeType.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let DEFAULT_MIME_TYPE = "application/octet-stream" 4 | let mimeTypes = [ 5 | "html": "text/html", 6 | "htm": "text/html", 7 | "shtml": "text/html", 8 | "css": "text/css", 9 | "xml": "text/xml", 10 | "gif": "image/gif", 11 | "jpeg": "image/jpeg", 12 | "jpg": "image/jpeg", 13 | "js": "application/javascript", 14 | "atom": "application/atom+xml", 15 | "rss": "application/rss+xml", 16 | "mml": "text/mathml", 17 | "txt": "text/plain", 18 | "jad": "text/vnd.sun.j2me.app-descriptor", 19 | "wml": "text/vnd.wap.wml", 20 | "htc": "text/x-component", 21 | "png": "image/png", 22 | "tif": "image/tiff", 23 | "tiff": "image/tiff", 24 | "wbmp": "image/vnd.wap.wbmp", 25 | "ico": "image/x-icon", 26 | "jng": "image/x-jng", 27 | "bmp": "image/x-ms-bmp", 28 | "svg": "image/svg+xml", 29 | "svgz": "image/svg+xml", 30 | "webp": "image/webp", 31 | "woff": "application/font-woff", 32 | "jar": "application/java-archive", 33 | "war": "application/java-archive", 34 | "ear": "application/java-archive", 35 | "json": "application/json", 36 | "hqx": "application/mac-binhex40", 37 | "doc": "application/msword", 38 | "pdf": "application/pdf", 39 | "ps": "application/postscript", 40 | "eps": "application/postscript", 41 | "ai": "application/postscript", 42 | "rtf": "application/rtf", 43 | "m3u8": "application/vnd.apple.mpegurl", 44 | "xls": "application/vnd.ms-excel", 45 | "eot": "application/vnd.ms-fontobject", 46 | "ppt": "application/vnd.ms-powerpoint", 47 | "wmlc": "application/vnd.wap.wmlc", 48 | "kml": "application/vnd.google-earth.kml+xml", 49 | "kmz": "application/vnd.google-earth.kmz", 50 | "7z": "application/x-7z-compressed", 51 | "cco": "application/x-cocoa", 52 | "jardiff": "application/x-java-archive-diff", 53 | "jnlp": "application/x-java-jnlp-file", 54 | "run": "application/x-makeself", 55 | "pl": "application/x-perl", 56 | "pm": "application/x-perl", 57 | "prc": "application/x-pilot", 58 | "pdb": "application/x-pilot", 59 | "rar": "application/x-rar-compressed", 60 | "rpm": "application/x-redhat-package-manager", 61 | "sea": "application/x-sea", 62 | "swf": "application/x-shockwave-flash", 63 | "sit": "application/x-stuffit", 64 | "tcl": "application/x-tcl", 65 | "tk": "application/x-tcl", 66 | "der": "application/x-x509-ca-cert", 67 | "pem": "application/x-x509-ca-cert", 68 | "crt": "application/x-x509-ca-cert", 69 | "xpi": "application/x-xpinstall", 70 | "xhtml": "application/xhtml+xml", 71 | "xspf": "application/xspf+xml", 72 | "zip": "application/zip", 73 | "bin": "application/octet-stream", 74 | "exe": "application/octet-stream", 75 | "dll": "application/octet-stream", 76 | "deb": "application/octet-stream", 77 | "dmg": "application/octet-stream", 78 | "iso": "application/octet-stream", 79 | "img": "application/octet-stream", 80 | "msi": "application/octet-stream", 81 | "msp": "application/octet-stream", 82 | "msm": "application/octet-stream", 83 | "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 84 | "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 85 | "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", 86 | "mid": "audio/midi", 87 | "midi": "audio/midi", 88 | "kar": "audio/midi", 89 | "mp3": "audio/mpeg", 90 | "ogg": "audio/ogg", 91 | "m4a": "audio/x-m4a", 92 | "ra": "audio/x-realaudio", 93 | "3gpp": "video/3gpp", 94 | "3gp": "video/3gpp", 95 | "ts": "video/mp2t", 96 | "mp4": "video/mp4", 97 | "mpeg": "video/mpeg", 98 | "mpg": "video/mpeg", 99 | "mov": "video/quicktime", 100 | "webm": "video/webm", 101 | "flv": "video/x-flv", 102 | "m4v": "video/x-m4v", 103 | "mng": "video/x-mng", 104 | "asx": "video/x-ms-asf", 105 | "asf": "video/x-ms-asf", 106 | "wmv": "video/x-ms-wmv", 107 | "avi": "video/x-msvideo" 108 | ] 109 | 110 | public struct MimeType { 111 | let ext: String? 112 | public var value: String { 113 | guard let ext = ext else { 114 | return DEFAULT_MIME_TYPE 115 | } 116 | return mimeTypes[ext.lowercased()] ?? DEFAULT_MIME_TYPE 117 | } 118 | public init(path: String) { 119 | ext = NSString(string: path).pathExtension 120 | } 121 | 122 | public init(path: NSString) { 123 | ext = path.pathExtension 124 | } 125 | 126 | public init(url: URL) { 127 | ext = url.pathExtension 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import MimeTypeTests 3 | 4 | XCTMain([ 5 | testCase(MimeTypeTests.allTests), 6 | ]) 7 | -------------------------------------------------------------------------------- /Tests/MimeTypeTests/MimeTypeTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import MimeType 3 | 4 | class MimeTypeTests: XCTestCase { 5 | func testMimeTypeFromString() { 6 | let path = "/User/onevcat/hello.html" 7 | XCTAssertEqual(MimeType(path: path).value, "text/html") 8 | } 9 | 10 | func testMimeTypeFromURL() { 11 | let path = "/User/onevcat/hello.html" 12 | let url = URL(fileURLWithPath: path) 13 | XCTAssertEqual(MimeType(url: url).value, "text/html") 14 | } 15 | 16 | func testMimeTypeFromNSString() { 17 | let path: NSString = "/User/onevcat/hello.html" 18 | XCTAssertEqual(MimeType(path: path).value, "text/html") 19 | } 20 | 21 | func testMimeTypeDefaultType() { 22 | let path = "/User/onevcat/hello.xyz" 23 | XCTAssertEqual(MimeType(path: path).value, "application/octet-stream") 24 | } 25 | 26 | static var allTests : [(String, (MimeTypeTests) -> () throws -> Void)] { 27 | return [ 28 | ("testMimeTypeFromString", testMimeTypeFromString), 29 | ("testMimeTypeFromURL", testMimeTypeFromURL), 30 | ("testMimeTypeFromNSString", testMimeTypeFromNSString), 31 | ("testMimeTypeDefaultType", testMimeTypeDefaultType) 32 | ] 33 | } 34 | } 35 | --------------------------------------------------------------------------------