I've written the following extension to determine the MIME type of a base64 string. It's worked in my local tests, but can anyone point out issues or alternate methods?
public static AttachmentType GetMimeType(this string value)
{
if(String.IsNullOrEmpty(value))
return new AttachmentType
{
FriendlyName = "Unknown",
MimeType = "application/octet-stream",
Extension = ""
};
var data = value.Substring(0,5);
switch (data.ToUpper())
{
case "IVBOR":
case "/9J/4":
return new AttachmentType
{
FriendlyName = "Photo",
MimeType = "image/png",
Extension = ".png"
};
case "AAAAF":
return new AttachmentType
{
FriendlyName = "Video",
MimeType = "video/mp4",
Extension = ".mp4"
};
case "JVBER":
return new AttachmentType
{
FriendlyName = "Document",
MimeType = "application/pdf",
Extension = ".pdf"
};
default:
return new AttachmentType
{
FriendlyName = "Unknown",
MimeType = string.Empty,
Extension = ""
};
}
}
public class AttachmentType
{
public string MimeType { get; set; }
public string FriendlyName { get; set; }
public string Extension { get; set; }
}