Skip to content

Latest commit

 

History

History
92 lines (81 loc) · 3.22 KB

EXAMPLES.md

File metadata and controls

92 lines (81 loc) · 3.22 KB

Examples

Detect MIME type Go Playground

Get the MIME type from a path to a file.

file := "testdata/pdf.pdf"
mime, err := mimetype.DetectFile(file)
fmt.Println(mime.String(), mime.Extension(), err)
// Output: application/pdf .pdf nil

Get the MIME type from a reader.

reader, _ := os.Open(file) // ignoring error for brevity's sake
mime, err := mimetype.DetectReader(reader)
fmt.Println(mime.String(), mime.Extension(), err)
// Output: application/pdf .pdf nil

Get the MIME type from a byte slice.

data, _ := ioutil.ReadFile(file) // ignoring error for brevity's sake
mime := mimetype.Detect(data)
fmt.Println(mime.String(), mime.Extension())
// Output: application/pdf .pdf

Test against a MIME type Go Playground

Test if a file has a specific MIME type. Different from the string comparison, e.g.: mime.String() == "application/zip", mime.Is("application/zip") method has the following advantages:

  • handles MIME aliases,
  • is case insensitive,
  • ignores optional MIME parameters,
  • ignores any leading and trailing whitespace.
mime, err := mimetype.DetectFile("testdata/zip.zip")
// application/x-zip is an alias of application/zip,
// therefore Is returns true both times.
fmt.Println(mime.Is("application/zip"), mime.Is("application/x-zip"), err)
// Output: true true <nil>

Upon detection, it may happen that the returned MIME type is more accurate than needed.

Suppose we have a text file containing HTML code. Detection performed on this file will retrieve the text/html MIME. If you are interested in telling if the input can be used as a text file, you can walk up the MIME hierarchy until text/plain is found.

Remember to always check for nil before using the result of the Parent() method.

           .Parent()              .Parent()
text/html ----------> text/plain ----------> application/octet-stream
detectedMIME, err := mimetype.DetectFile("testdata/html.html")

isText := false
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
    if mime.Is("text/plain") {
        isText = true
    }
}

// isText is true, even if the detected MIME was text/html.
fmt.Println(isText, detectedMIME, err)
// Output: true text/html <nil>

Binary file vs text file Go Playground

Considering the definition of a binary file as "a computer file that is not a text file", they can be differentiated by searching for the text/plain MIME in it's MIME hierarchy. This is a reiteration of the Parent example.

detectedMIME, err := mimetype.DetectFile("testdata/xml.xml")

isBinary := true
for mime := detectedMIME; mime != nil; mime = mime.Parent() {
    if mime.Is("text/plain") {
        isBinary = false
    }
}

fmt.Println(isBinary, detectedMIME, err)
// Output: false text/xml; charset=utf-8 <nil>