github.com/olljanat/moby@v1.13.1/pkg/httputils/mimetype.go (about)

     1  package httputils
     2  
     3  import (
     4  	"mime"
     5  	"net/http"
     6  )
     7  
     8  // MimeTypes stores the MIME content type.
     9  var MimeTypes = struct {
    10  	TextPlain   string
    11  	Tar         string
    12  	OctetStream string
    13  }{"text/plain", "application/tar", "application/octet-stream"}
    14  
    15  // DetectContentType returns a best guess representation of the MIME
    16  // content type for the bytes at c.  The value detected by
    17  // http.DetectContentType is guaranteed not be nil, defaulting to
    18  // application/octet-stream when a better guess cannot be made. The
    19  // result of this detection is then run through mime.ParseMediaType()
    20  // which separates the actual MIME string from any parameters.
    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  }