Infer file mime type
Some checks failed
/ basic (push) Failing after 2m34s

This commit is contained in:
天クマ 2026-05-15 09:58:28 -03:00
commit 857eee205c
2 changed files with 17 additions and 5 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
forgejo-pages

View file

@ -12,6 +12,8 @@ import (
"net/http"
"net/url"
"strings"
"mime"
"path/filepath"
)
type Forgejo struct {
@ -129,6 +131,15 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
trySet(w.Header(), "Last-Modified", resp.Header)
trySet(w.Header(), "Content-Length", resp.Header)
trySet(w.Header(), "Content-Range", resp.Header)
contentType := resp.Header.Get("Content-Type")
if contentType == "" || strings.HasPrefix(contentType, "text/plain") {
if ct := mime.TypeByExtension(filepath.Ext(file)); ct != "" {
contentType = ct
}
}
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)