Created
April 26, 2020 16:56
-
-
Save priore/2ce1a99d5c114856563fd046050f2c73 to your computer and use it in GitHub Desktop.
Retrieve the content-type from the data signature
This file contains hidden or 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
// recognized content-type | |
private let data_sign = [ | |
"7z¼¯'": "application/x-7z-compressed", | |
"AIFF": "audio/x-aiff", | |
"AVI": "video/x-msvideo", | |
"BM": "image/bmp", | |
"BZh": "application/x-bzip2", | |
"MSCF": "application/vnd.ms-cab-compressed", | |
"ITSF": "application/vnd.ms-htmlhelp", | |
"!<arch>": "application/x-debian-package", | |
"Received:": "message/rfc822", | |
"MZ": "application/x-msdownload", | |
"GIF87a": "image/gif", | |
"GIF89a": "image/gif", | |
"Êþº¾": "application/java-vm", | |
"ÿØÿÛ": "image/jpeg", | |
"ÿØÿà+JFIF": "image/jpeg", | |
"ÿØÿà+Exif": "image/jpeg", | |
"MThd": "audio/midi", | |
"%PDF-": "application/pdf", | |
"PNG": "image/png", | |
"8BPS": "image/vnd.adobe.photoshop", | |
"Rar!": "application/x-rar-compressed", | |
"{\rtf1": "application/rtf", | |
"CWS": "application/x-shockwave-flash", | |
"FWS": "application/x-shockwave-flash", | |
"ustar": "application/x-tar", | |
"II*.": "image/tiff", | |
"MM.*": "image/tiff", | |
"WAVE": "audio/x-wav", | |
"RIFF+WEBP": "image/webp", | |
"wOFF": "application/x-font-woff", | |
"wOF2": "application/x-font-woff", | |
"PK": "application/zip", | |
"<?xml": "application/xml" | |
] | |
extension Data | |
{ | |
var contentType: String { | |
if let prefix = String(data: self.prefix(30), encoding: .isoLatin1) | |
{ | |
for key in data_sign.keys { | |
let keys = key.components(separatedBy: "+") | |
if prefix.contains(keys) { | |
return data_sign[key]! | |
} | |
} | |
} | |
return "application/octet-stream" | |
} | |
} | |
extension String | |
{ | |
func contains(_ strings: [String]) -> Bool | |
{ | |
guard strings.count > 0 else { | |
return false | |
} | |
var allContained = true | |
for string in strings { | |
allContained = allContained && contains(string) | |
} | |
return allContained | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment