close api response body

This commit is contained in:
Ronmi Ren 2025-01-07 21:41:32 +08:00
commit 1b5ab5659d
2 changed files with 4 additions and 2 deletions

View file

@ -25,7 +25,7 @@ type Info struct {
var ErrNotFound = errors.New("404 not found")
func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.Reader, err error) {
func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.ReadCloser, err error) {
u := f.Server
u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file
p := u.Query()
@ -55,7 +55,7 @@ func (f *Forgejo) getFile(ctx context.Context, user, repo, branch, file string)
return
}
func (f *Forgejo) GetFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.Reader, err error) {
func (f *Forgejo) GetFile(ctx context.Context, user, repo, branch, file string) (info Info, content io.ReadCloser, err error) {
info, content, err = f.getFile(ctx, user, repo, branch, file)
if err != nil {
return
@ -67,6 +67,7 @@ func (f *Forgejo) GetFile(ctx context.Context, user, repo, branch, file string)
if err != nil {
return
}
content.Close()
if bytes.HasPrefix(data, []byte("/")) {
err = ErrNotFound
return

View file

@ -49,6 +49,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
return
}
defer content.Close()
trySet(w.Header(), "Etag", info.ETag)
trySet(w.Header(), "Last-Modified", info.LastModified)