mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 19:38:23 +00:00 
			
		
		
		
	Upgrade to [v0.19.0](https://github.com/golang/tools/releases/tag/gopls%2Fv0.19.0) and fix issues. Runs with new `warning` serverity setting. This likely does less checks than before. Additionally, add `make fix` which runs modernize. This is also verified on CI. For the record, here are the issues discoverd when running with `info` severity, in case we want to fix these: ``` tests/integration/repo_test.go:95:5-14: could use tagged switch on i tests/integration/api_packages_generic_test.go:149:4-64: could use tagged switch on setting.Packages.Storage.Type services/webhook/msteams_test.go:33:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:59:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:85:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:111:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:138:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:161:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:187:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:213:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:239:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:266:4-33: could use tagged switch on fact.Name services/webhook/msteams_test.go:407:4-33: could use tagged switch on fact.Name tests/integration/api_packages_conan_test.go:350:6-33: could use tagged switch on pf.Name models/issues/tracked_time_test.go:98:3-18: could use tagged switch on user.ID tests/integration/api_token_test.go:505:5-43: could use tagged switch on minRequiredLevel services/gitdiff/gitdiff.go:220:33-46: method "getLineLegacy" is unused ``` --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
		
			
				
	
	
		
			114 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2016 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package public
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"io"
 | |
| 	"net/http"
 | |
| 	"os"
 | |
| 	"path"
 | |
| 	"strings"
 | |
| 	"time"
 | |
| 
 | |
| 	"code.gitea.io/gitea/modules/assetfs"
 | |
| 	"code.gitea.io/gitea/modules/container"
 | |
| 	"code.gitea.io/gitea/modules/httpcache"
 | |
| 	"code.gitea.io/gitea/modules/log"
 | |
| 	"code.gitea.io/gitea/modules/setting"
 | |
| 	"code.gitea.io/gitea/modules/util"
 | |
| )
 | |
| 
 | |
| func CustomAssets() *assetfs.Layer {
 | |
| 	return assetfs.Local("custom", setting.CustomPath, "public")
 | |
| }
 | |
| 
 | |
| func AssetFS() *assetfs.LayeredFS {
 | |
| 	return assetfs.Layered(CustomAssets(), BuiltinAssets())
 | |
| }
 | |
| 
 | |
| // FileHandlerFunc implements the static handler for serving files in "public" assets
 | |
| func FileHandlerFunc() http.HandlerFunc {
 | |
| 	assetFS := AssetFS()
 | |
| 	return func(resp http.ResponseWriter, req *http.Request) {
 | |
| 		if req.Method != "GET" && req.Method != "HEAD" {
 | |
| 			resp.WriteHeader(http.StatusMethodNotAllowed)
 | |
| 			return
 | |
| 		}
 | |
| 		handleRequest(resp, req, assetFS, req.URL.Path)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
 | |
| func parseAcceptEncoding(val string) container.Set[string] {
 | |
| 	parts := strings.Split(val, ";")
 | |
| 	types := make(container.Set[string])
 | |
| 	for v := range strings.SplitSeq(parts[0], ",") {
 | |
| 		types.Add(strings.TrimSpace(v))
 | |
| 	}
 | |
| 	return types
 | |
| }
 | |
| 
 | |
| // setWellKnownContentType will set the Content-Type if the file is a well-known type.
 | |
| // See the comments of detectWellKnownMimeType
 | |
| func setWellKnownContentType(w http.ResponseWriter, file string) {
 | |
| 	mimeType := detectWellKnownMimeType(path.Ext(file))
 | |
| 	if mimeType != "" {
 | |
| 		w.Header().Set("Content-Type", mimeType)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func handleRequest(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string) {
 | |
| 	// actually, fs (http.FileSystem) is designed to be a safe interface, relative paths won't bypass its parent directory, it's also fine to do a clean here
 | |
| 	f, err := fs.Open(util.PathJoinRelX(file))
 | |
| 	if err != nil {
 | |
| 		if os.IsNotExist(err) {
 | |
| 			w.WriteHeader(http.StatusNotFound)
 | |
| 			return
 | |
| 		}
 | |
| 		w.WriteHeader(http.StatusInternalServerError)
 | |
| 		log.Error("[Static] Open %q failed: %v", file, err)
 | |
| 		return
 | |
| 	}
 | |
| 	defer f.Close()
 | |
| 
 | |
| 	fi, err := f.Stat()
 | |
| 	if err != nil {
 | |
| 		w.WriteHeader(http.StatusInternalServerError)
 | |
| 		log.Error("[Static] %q exists, but fails to open: %v", file, err)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	// need to serve index file? (no at the moment)
 | |
| 	if fi.IsDir() {
 | |
| 		w.WriteHeader(http.StatusNotFound)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	servePublicAsset(w, req, fi, fi.ModTime(), f)
 | |
| }
 | |
| 
 | |
| // servePublicAsset serve http content
 | |
| func servePublicAsset(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
 | |
| 	setWellKnownContentType(w, fi.Name())
 | |
| 	httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
 | |
| 	encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
 | |
| 	fiEmbedded, _ := fi.(assetfs.EmbeddedFileInfo)
 | |
| 	if encodings.Contains("gzip") && fiEmbedded != nil {
 | |
| 		// try to provide gzip content directly from bindata
 | |
| 		if gzipBytes, ok := fiEmbedded.GetGzipContent(); ok {
 | |
| 			rdGzip := bytes.NewReader(gzipBytes)
 | |
| 			// all gzipped static files (from bindata) are managed by Gitea, so we can make sure every file has the correct ext name
 | |
| 			// then we can get the correct Content-Type, we do not need to do http.DetectContentType on the decompressed data
 | |
| 			if w.Header().Get("Content-Type") == "" {
 | |
| 				w.Header().Set("Content-Type", "application/octet-stream")
 | |
| 			}
 | |
| 			w.Header().Set("Content-Encoding", "gzip")
 | |
| 			http.ServeContent(w, req, fi.Name(), modtime, rdGzip)
 | |
| 			return
 | |
| 		}
 | |
| 	}
 | |
| 	http.ServeContent(w, req, fi.Name(), modtime, content)
 | |
| }
 |