mimetype.go 830 B

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