github.com/chenchun/docker@v1.3.2-0.20150629222414-20467faf132b/pkg/httputils/mimetype.go (about)

     1  package httputils
     2  
     3  import (
     4  	"mime"
     5  	"net/http"
     6  )
     7  
     8  var MimeTypes = struct {
     9  	TextPlain   string
    10  	Tar         string
    11  	OctetStream string
    12  }{"text/plain", "application/tar", "application/octet-stream"}
    13  
    14  // DetectContentType returns a best guess representation of the MIME
    15  // content type for the bytes at c.  The value detected by
    16  // http.DetectContentType is guaranteed not be nil, defaulting to
    17  // application/octet-stream when a better guess cannot be made. The
    18  // result of this detection is then run through mime.ParseMediaType()
    19  // which separates it from any parameters.
    20  // Note that calling this function does not advance the Reader at r
    21  func DetectContentType(c []byte) (string, map[string]string, error) {
    22  
    23  	ct := http.DetectContentType(c)
    24  	contentType, args, err := mime.ParseMediaType(ct)
    25  	if err != nil {
    26  		return "", nil, err
    27  	}
    28  
    29  	return contentType, args, nil
    30  }