code.gitea.io/gitea@v1.19.3/modules/public/mime_types.go (about)

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