Skip to main content
Tweeted twitter.com/StackCodeReview/status/1548004487251365889
deleted 224 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I've written the following extension to determine the type mimeMIME type of a base64 string, it's. 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; }
        }

I've written the following extension to determine the type 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; }
        }

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; }
}
Source Link
Sam Jones
  • 193
  • 1
  • 1
  • 4

Checking MIME Type from a base64 string

I've written the following extension to determine the type 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; }
        }