This commit is contained in:
parent
2b1c2a3c46
commit
857eee205c
2 changed files with 17 additions and 5 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
forgejo-pages
|
||||||
21
lib/web.go
21
lib/web.go
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"mime"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Forgejo struct {
|
type Forgejo struct {
|
||||||
|
|
@ -34,7 +36,7 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range headers {
|
for k, v := range headers {
|
||||||
if v == "" {
|
if v == "" {
|
||||||
continue
|
continue
|
||||||
|
|
@ -50,7 +52,7 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user,
|
||||||
err = ErrNotFound
|
err = ErrNotFound
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,7 +104,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
file = strings.Join(arr[2:], "/")
|
file = strings.Join(arr[2:], "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
headers := map[string]string{}
|
headers := map[string]string{}
|
||||||
headers["If-None-Match"] = r.Header.Get("If-None-Match")
|
headers["If-None-Match"] = r.Header.Get("If-None-Match")
|
||||||
headers["If-Modified-Since"] = r.Header.Get("If-Modified-Since")
|
headers["If-Modified-Since"] = r.Header.Get("If-Modified-Since")
|
||||||
|
|
@ -124,12 +126,21 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
trySet(w.Header(), "Etag", resp.Header)
|
trySet(w.Header(), "Etag", resp.Header)
|
||||||
trySet(w.Header(), "Last-Modified", resp.Header)
|
trySet(w.Header(), "Last-Modified", resp.Header)
|
||||||
trySet(w.Header(), "Content-Length", resp.Header)
|
trySet(w.Header(), "Content-Length", resp.Header)
|
||||||
trySet(w.Header(), "Content-Range", 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)
|
w.WriteHeader(resp.StatusCode)
|
||||||
io.Copy(w, resp.Body)
|
io.Copy(w, resp.Body)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue