Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Created August 20, 2018 14:10
Show Gist options
  • Save dedeexe/c79c97e68576bd0535571e110dde786e to your computer and use it in GitHub Desktop.
Save dedeexe/c79c97e68576bd0535571e110dde786e to your computer and use it in GitHub Desktop.
Data MimyType analyse extension
import Foundation
import MobileCoreServices
extension Data {
enum MimeType : Int {
case imageJPEG = 0xff
case imagePNG = 0x89
case applicationPDF = 0x25
case textPlain = 0x46
case unknown = 0x00
//
// Returns the mime type string
//
var mimeTypeString : String {
switch self {
case .imageJPEG: return "image/jpeg"
case .imagePNG: return "image/png"
case .applicationPDF: return "application/pdf"
case .textPlain: return "text/plain"
case .unknown: return ""
}
}
//
// Returns the mime type extension string
//
var mimeTypeFile : String {
switch self {
case .imageJPEG: return "jpeg"
case .imagePNG: return "png"
case .applicationPDF: return "pdf"
case .textPlain: return "txt"
case .unknown: return ""
}
}
}
var mimeType : MimeType {
var value : UInt8 = 0
copyBytes(to: &value, count: 1)
let result = MimeType(rawValue: Int(value)) ?? MimeType.unknown
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment