You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mime_types.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package public
  4. import "strings"
  5. // wellKnownMimeTypesLower comes from Golang's builtin mime package: `builtinTypesLower`, see the comment of detectWellKnownMimeType
  6. var wellKnownMimeTypesLower = map[string]string{
  7. ".avif": "image/avif",
  8. ".css": "text/css; charset=utf-8",
  9. ".gif": "image/gif",
  10. ".htm": "text/html; charset=utf-8",
  11. ".html": "text/html; charset=utf-8",
  12. ".jpeg": "image/jpeg",
  13. ".jpg": "image/jpeg",
  14. ".js": "text/javascript; charset=utf-8",
  15. ".json": "application/json",
  16. ".mjs": "text/javascript; charset=utf-8",
  17. ".pdf": "application/pdf",
  18. ".png": "image/png",
  19. ".svg": "image/svg+xml",
  20. ".wasm": "application/wasm",
  21. ".webp": "image/webp",
  22. ".xml": "text/xml; charset=utf-8",
  23. // well, there are some types missing from the builtin list
  24. ".txt": "text/plain; charset=utf-8",
  25. }
  26. // detectWellKnownMimeType will return the mime-type for a well-known file ext name
  27. // The purpose of this function is to bypass the unstable behavior of Golang's mime.TypeByExtension
  28. // mime.TypeByExtension would use OS's mime-type config to overwrite the well-known types (see its document).
  29. // If the user's OS has incorrect mime-type config, it would make Gitea can not respond a correct Content-Type to browsers.
  30. // For example, if Gitea returns `text/plain` for a `.js` file, the browser couldn't run the JS due to security reasons.
  31. // detectWellKnownMimeType makes the Content-Type for well-known files stable.
  32. func detectWellKnownMimeType(ext string) string {
  33. ext = strings.ToLower(ext)
  34. return wellKnownMimeTypesLower[ext]
  35. }