diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-13 22:04:57 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-13 16:04:57 +0200 |
commit | a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c (patch) | |
tree | 84c1e5e4f3423ed952b573ab1403844de2951cb7 /routers/web/base.go | |
parent | f74501609246646a3ab62b37a1e7469d6fea63b7 (diff) | |
download | gitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.tar.gz gitea-a94a8d0ab1f6c9d63ae607dfec866c47ae3e4b5c.zip |
Use standard HTTP library to serve files (#24693)
`http.ServeFile/ServeContent` handles `If-xxx`, `Content-Length`,
`Range` and `Etag` correctly
After this PR, storage files (eg: avatar) could be responded with
correct Content-Length.
Diffstat (limited to 'routers/web/base.go')
-rw-r--r-- | routers/web/base.go | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/routers/web/base.go b/routers/web/base.go index b9b5958389..1f6c4fbfc5 100644 --- a/routers/web/base.go +++ b/routers/web/base.go @@ -6,7 +6,6 @@ package web import ( "errors" "fmt" - "io" "net/http" "os" "path" @@ -76,12 +75,6 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor } fi, err := objStore.Stat(rPath) - if err == nil && httpcache.HandleTimeCache(req, w, fi) { - return - } - - // If we have matched and access to release or issue - fr, err := objStore.Open(rPath) if err != nil { if os.IsNotExist(err) || errors.Is(err, os.ErrNotExist) { log.Warn("Unable to find %s %s", prefix, rPath) @@ -92,14 +85,15 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError) return } - defer fr.Close() - _, err = io.Copy(w, fr) + fr, err := objStore.Open(rPath) if err != nil { - log.Error("Error whilst rendering %s %s. Error: %v", prefix, rPath, err) - http.Error(w, fmt.Sprintf("Error whilst rendering %s %s", prefix, rPath), http.StatusInternalServerError) + log.Error("Error whilst opening %s %s. Error: %v", prefix, rPath, err) + http.Error(w, fmt.Sprintf("Error whilst opening %s %s", prefix, rPath), http.StatusInternalServerError) return } + defer fr.Close() + httpcache.ServeContentWithCacheControl(w, req, path.Base(rPath), fi.ModTime(), fr) }) } } |