Last active
August 29, 2023 11:44
-
-
Save ngs/918b07f448977789cf69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var text = fs.readFileSync('/usr/local/etc/nginx/mime.types').toString('utf8'); | |
var lines = text | |
.replace(/[\s\n]*types\s*{/, '') | |
.replace(/}[\s\n]*/, '') | |
.replace(/\n\s+/g, '\n') | |
.split('\n') | |
.filter(function(l) { return /^[^\s]/.test(l) }) | |
.map(function(l) { | |
var m = l.match(/^([^\s]+)\s+([^;]+);/); | |
return [m[1], m[2].split(' ')]; | |
}); | |
var map = {}; | |
lines.forEach(function(l) { | |
l[1].forEach(function(ext) { | |
map[ext] = l[0]; | |
}) | |
}); | |
console.info(JSON.stringify(map, null, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal let DEFAULT_MIME_TYPE = "application/octet-stream" | |
internal let mimeTypes = [ | |
"html": "text/html", | |
"htm": "text/html", | |
"shtml": "text/html", | |
"css": "text/css", | |
"xml": "text/xml", | |
"gif": "image/gif", | |
"jpeg": "image/jpeg", | |
"jpg": "image/jpeg", | |
"js": "application/javascript", | |
"atom": "application/atom+xml", | |
"rss": "application/rss+xml", | |
"mml": "text/mathml", | |
"txt": "text/plain", | |
"jad": "text/vnd.sun.j2me.app-descriptor", | |
"wml": "text/vnd.wap.wml", | |
"htc": "text/x-component", | |
"png": "image/png", | |
"tif": "image/tiff", | |
"tiff": "image/tiff", | |
"wbmp": "image/vnd.wap.wbmp", | |
"ico": "image/x-icon", | |
"jng": "image/x-jng", | |
"bmp": "image/x-ms-bmp", | |
"svg": "image/svg+xml", | |
"svgz": "image/svg+xml", | |
"webp": "image/webp", | |
"woff": "application/font-woff", | |
"jar": "application/java-archive", | |
"war": "application/java-archive", | |
"ear": "application/java-archive", | |
"json": "application/json", | |
"hqx": "application/mac-binhex40", | |
"doc": "application/msword", | |
"pdf": "application/pdf", | |
"ps": "application/postscript", | |
"eps": "application/postscript", | |
"ai": "application/postscript", | |
"rtf": "application/rtf", | |
"m3u8": "application/vnd.apple.mpegurl", | |
"xls": "application/vnd.ms-excel", | |
"eot": "application/vnd.ms-fontobject", | |
"ppt": "application/vnd.ms-powerpoint", | |
"wmlc": "application/vnd.wap.wmlc", | |
"kml": "application/vnd.google-earth.kml+xml", | |
"kmz": "application/vnd.google-earth.kmz", | |
"7z": "application/x-7z-compressed", | |
"cco": "application/x-cocoa", | |
"jardiff": "application/x-java-archive-diff", | |
"jnlp": "application/x-java-jnlp-file", | |
"run": "application/x-makeself", | |
"pl": "application/x-perl", | |
"pm": "application/x-perl", | |
"prc": "application/x-pilot", | |
"pdb": "application/x-pilot", | |
"rar": "application/x-rar-compressed", | |
"rpm": "application/x-redhat-package-manager", | |
"sea": "application/x-sea", | |
"swf": "application/x-shockwave-flash", | |
"sit": "application/x-stuffit", | |
"tcl": "application/x-tcl", | |
"tk": "application/x-tcl", | |
"der": "application/x-x509-ca-cert", | |
"pem": "application/x-x509-ca-cert", | |
"crt": "application/x-x509-ca-cert", | |
"xpi": "application/x-xpinstall", | |
"xhtml": "application/xhtml+xml", | |
"xspf": "application/xspf+xml", | |
"zip": "application/zip", | |
"bin": "application/octet-stream", | |
"exe": "application/octet-stream", | |
"dll": "application/octet-stream", | |
"deb": "application/octet-stream", | |
"dmg": "application/octet-stream", | |
"iso": "application/octet-stream", | |
"img": "application/octet-stream", | |
"msi": "application/octet-stream", | |
"msp": "application/octet-stream", | |
"msm": "application/octet-stream", | |
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", | |
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | |
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", | |
"mid": "audio/midi", | |
"midi": "audio/midi", | |
"kar": "audio/midi", | |
"mp3": "audio/mpeg", | |
"ogg": "audio/ogg", | |
"m4a": "audio/x-m4a", | |
"ra": "audio/x-realaudio", | |
"3gpp": "video/3gpp", | |
"3gp": "video/3gpp", | |
"ts": "video/mp2t", | |
"mp4": "video/mp4", | |
"mpeg": "video/mpeg", | |
"mpg": "video/mpeg", | |
"mov": "video/quicktime", | |
"webm": "video/webm", | |
"flv": "video/x-flv", | |
"m4v": "video/x-m4v", | |
"mng": "video/x-mng", | |
"asx": "video/x-ms-asf", | |
"asf": "video/x-ms-asf", | |
"wmv": "video/x-ms-wmv", | |
"avi": "video/x-msvideo" | |
] | |
internal func MimeType(ext: String?) -> String { | |
if ext != nil && mimeTypes.contains({ $0.0 == ext!.lowercaseString }) { | |
return mimeTypes[ext!.lowercaseString]! | |
} | |
return DEFAULT_MIME_TYPE | |
} | |
extension NSURL { | |
public func mimeType() -> String { | |
return MimeType(self.pathExtension) | |
} | |
} | |
extension NSString { | |
public func mimeType() -> String { | |
return MimeType(self.pathExtension) | |
} | |
} | |
extension String { | |
public func mimeType() -> String { | |
return (self as NSString).mimeType() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"html": "text/html", | |
"htm": "text/html", | |
"shtml": "text/html", | |
"css": "text/css", | |
"xml": "text/xml", | |
"gif": "image/gif", | |
"jpeg": "image/jpeg", | |
"jpg": "image/jpeg", | |
"js": "application/javascript", | |
"atom": "application/atom+xml", | |
"rss": "application/rss+xml", | |
"mml": "text/mathml", | |
"txt": "text/plain", | |
"jad": "text/vnd.sun.j2me.app-descriptor", | |
"wml": "text/vnd.wap.wml", | |
"htc": "text/x-component", | |
"png": "image/png", | |
"tif": "image/tiff", | |
"tiff": "image/tiff", | |
"wbmp": "image/vnd.wap.wbmp", | |
"ico": "image/x-icon", | |
"jng": "image/x-jng", | |
"bmp": "image/x-ms-bmp", | |
"svg": "image/svg+xml", | |
"svgz": "image/svg+xml", | |
"webp": "image/webp", | |
"woff": "application/font-woff", | |
"jar": "application/java-archive", | |
"war": "application/java-archive", | |
"ear": "application/java-archive", | |
"json": "application/json", | |
"hqx": "application/mac-binhex40", | |
"doc": "application/msword", | |
"pdf": "application/pdf", | |
"ps": "application/postscript", | |
"eps": "application/postscript", | |
"ai": "application/postscript", | |
"rtf": "application/rtf", | |
"m3u8": "application/vnd.apple.mpegurl", | |
"xls": "application/vnd.ms-excel", | |
"eot": "application/vnd.ms-fontobject", | |
"ppt": "application/vnd.ms-powerpoint", | |
"wmlc": "application/vnd.wap.wmlc", | |
"kml": "application/vnd.google-earth.kml+xml", | |
"kmz": "application/vnd.google-earth.kmz", | |
"7z": "application/x-7z-compressed", | |
"cco": "application/x-cocoa", | |
"jardiff": "application/x-java-archive-diff", | |
"jnlp": "application/x-java-jnlp-file", | |
"run": "application/x-makeself", | |
"pl": "application/x-perl", | |
"pm": "application/x-perl", | |
"prc": "application/x-pilot", | |
"pdb": "application/x-pilot", | |
"rar": "application/x-rar-compressed", | |
"rpm": "application/x-redhat-package-manager", | |
"sea": "application/x-sea", | |
"swf": "application/x-shockwave-flash", | |
"sit": "application/x-stuffit", | |
"tcl": "application/x-tcl", | |
"tk": "application/x-tcl", | |
"der": "application/x-x509-ca-cert", | |
"pem": "application/x-x509-ca-cert", | |
"crt": "application/x-x509-ca-cert", | |
"xpi": "application/x-xpinstall", | |
"xhtml": "application/xhtml+xml", | |
"xspf": "application/xspf+xml", | |
"zip": "application/zip", | |
"bin": "application/octet-stream", | |
"exe": "application/octet-stream", | |
"dll": "application/octet-stream", | |
"deb": "application/octet-stream", | |
"dmg": "application/octet-stream", | |
"iso": "application/octet-stream", | |
"img": "application/octet-stream", | |
"msi": "application/octet-stream", | |
"msp": "application/octet-stream", | |
"msm": "application/octet-stream", | |
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", | |
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | |
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", | |
"mid": "audio/midi", | |
"midi": "audio/midi", | |
"kar": "audio/midi", | |
"mp3": "audio/mpeg", | |
"ogg": "audio/ogg", | |
"m4a": "audio/x-m4a", | |
"ra": "audio/x-realaudio", | |
"3gpp": "video/3gpp", | |
"3gp": "video/3gpp", | |
"ts": "video/mp2t", | |
"mp4": "video/mp4", | |
"mpeg": "video/mpeg", | |
"mpg": "video/mpeg", | |
"mov": "video/quicktime", | |
"webm": "video/webm", | |
"flv": "video/x-flv", | |
"m4v": "video/x-m4v", | |
"mng": "video/x-mng", | |
"asx": "video/x-ms-asf", | |
"asf": "video/x-ms-asf", | |
"wmv": "video/x-ms-wmv", | |
"avi": "video/x-msvideo" | |
} |
@ngs Modified to work with Swift 4, and included what @mbalex99 wanted:
import Foundation
internal let DEFAULT_MIME_TYPE = "application/octet-stream"
internal let mimeTypes = [
"md": "text/markdown",
"html": "text/html",
"htm": "text/html",
"shtml": "text/html",
"css": "text/css",
"xml": "text/xml",
"gif": "image/gif",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "application/javascript",
"atom": "application/atom+xml",
"rss": "application/rss+xml",
"mml": "text/mathml",
"txt": "text/plain",
"jad": "text/vnd.sun.j2me.app-descriptor",
"wml": "text/vnd.wap.wml",
"htc": "text/x-component",
"png": "image/png",
"tif": "image/tiff",
"tiff": "image/tiff",
"wbmp": "image/vnd.wap.wbmp",
"ico": "image/x-icon",
"jng": "image/x-jng",
"bmp": "image/x-ms-bmp",
"svg": "image/svg+xml",
"svgz": "image/svg+xml",
"webp": "image/webp",
"woff": "application/font-woff",
"jar": "application/java-archive",
"war": "application/java-archive",
"ear": "application/java-archive",
"json": "application/json",
"hqx": "application/mac-binhex40",
"doc": "application/msword",
"pdf": "application/pdf",
"ps": "application/postscript",
"eps": "application/postscript",
"ai": "application/postscript",
"rtf": "application/rtf",
"m3u8": "application/vnd.apple.mpegurl",
"xls": "application/vnd.ms-excel",
"eot": "application/vnd.ms-fontobject",
"ppt": "application/vnd.ms-powerpoint",
"wmlc": "application/vnd.wap.wmlc",
"kml": "application/vnd.google-earth.kml+xml",
"kmz": "application/vnd.google-earth.kmz",
"7z": "application/x-7z-compressed",
"cco": "application/x-cocoa",
"jardiff": "application/x-java-archive-diff",
"jnlp": "application/x-java-jnlp-file",
"run": "application/x-makeself",
"pl": "application/x-perl",
"pm": "application/x-perl",
"prc": "application/x-pilot",
"pdb": "application/x-pilot",
"rar": "application/x-rar-compressed",
"rpm": "application/x-redhat-package-manager",
"sea": "application/x-sea",
"swf": "application/x-shockwave-flash",
"sit": "application/x-stuffit",
"tcl": "application/x-tcl",
"tk": "application/x-tcl",
"der": "application/x-x509-ca-cert",
"pem": "application/x-x509-ca-cert",
"crt": "application/x-x509-ca-cert",
"xpi": "application/x-xpinstall",
"xhtml": "application/xhtml+xml",
"xspf": "application/xspf+xml",
"zip": "application/zip",
"bin": "application/octet-stream",
"exe": "application/octet-stream",
"dll": "application/octet-stream",
"deb": "application/octet-stream",
"dmg": "application/octet-stream",
"iso": "application/octet-stream",
"img": "application/octet-stream",
"msi": "application/octet-stream",
"msp": "application/octet-stream",
"msm": "application/octet-stream",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"mid": "audio/midi",
"midi": "audio/midi",
"kar": "audio/midi",
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"m4a": "audio/x-m4a",
"ra": "audio/x-realaudio",
"3gpp": "video/3gpp",
"3gp": "video/3gpp",
"ts": "video/mp2t",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"mpg": "video/mpeg",
"mov": "video/quicktime",
"webm": "video/webm",
"flv": "video/x-flv",
"m4v": "video/x-m4v",
"mng": "video/x-mng",
"asx": "video/x-ms-asf",
"asf": "video/x-ms-asf",
"wmv": "video/x-ms-wmv",
"avi": "video/x-msvideo"
]
internal func MimeType(ext: String?) -> String {
let lowercase_ext: String = ext!.lowercased()
if ext != nil && mimeTypes.contains(where: { $0.0 == lowercase_ext }) {
return mimeTypes[lowercase_ext]!
}
return DEFAULT_MIME_TYPE
}
extension NSURL {
public func mimeType() -> String {
return MimeType(ext: self.pathExtension)
}
}
extension NSString {
public func mimeType() -> String {
return MimeType(ext: self.pathExtension)
}
}
extension String {
public func mimeType() -> String {
return (self as NSString).mimeType()
}
}
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream"
}
@amrangry This is broken for iWork documents. ( Keynote, Pages and Numbers).
UTTypeCopyPreferredTagWithClass return nil for com.apple.iwork.keynote.key
How can I get mime type from NSData object. I want to detect zip file. Please help ASAP.
Hey, I've made an optimization for Swift where it uses optional in a bit cleaner way. You can check it out on my fork, or just replace it with:
internal func MimeType(ext: String?) -> String {
if let ext = ext?.lowercased(), let mimeType = mimeTypes[ext] {
return mimeType
}else{
return DEFAULT_MIME_TYPE
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can we add the official markdown extension