mimetype.go 869 B

123456789101112131415161718192021222324252627282930
  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. Tar string
  10. OctetStream string
  11. }{"text/plain", "application/tar", "application/octet-stream"}
  12. // DetectContentType returns a best guess representation of the MIME
  13. // content type for the bytes at c. The value detected by
  14. // http.DetectContentType is guaranteed not be nil, defaulting to
  15. // application/octet-stream when a better guess cannot be made. The
  16. // result of this detection is then run through mime.ParseMediaType()
  17. // which separates the actual MIME string from any parameters.
  18. func DetectContentType(c []byte) (string, map[string]string, error) {
  19. ct := http.DetectContentType(c)
  20. contentType, args, err := mime.ParseMediaType(ct)
  21. if err != nil {
  22. return "", nil, err
  23. }
  24. return contentType, args, nil
  25. }