Add HEAD endpoint for raw and download file (#748)
Go CI / Lint (push) Has been cancelled
Go CI / Check (push) Has been cancelled
Go CI / Test (mysql, 1.26, mysql:8, ubuntu-latest, 3306:3306) (push) Has been cancelled
Go CI / Test (postgres, 1.26, postgres:16, ubuntu-latest, 5432:5432) (push) Has been cancelled
Go CI / Test (sqlite, 1.26, macOS-latest) (push) Has been cancelled
Go CI / Test (sqlite, 1.26, ubuntu-latest) (push) Has been cancelled
Go CI / Build (1.26, macOS-latest) (push) Has been cancelled
Go CI / Build (1.26, ubuntu-latest) (push) Has been cancelled
Go CI / Build (1.26, windows-latest) (push) Has been cancelled

This commit is contained in:
Thomas
2026-06-29 11:44:50 +07:00
committed by GitHub
parent cc03c1e910
commit fa4b4f6975
3 changed files with 9 additions and 1 deletions
+2 -1
View File
@@ -36,6 +36,7 @@ func RawFile(ctx *context.Context) error {
}
ctx.Response().Header().Set("Content-Disposition", "inline; filename=\""+url.PathEscape(file.Filename)+"\"")
ctx.Response().Header().Set("Content-Length", strconv.Itoa(int(file.Size)))
ctx.Response().Header().Set("X-Content-Type-Options", "nosniff")
return ctx.PlainText(200, file.Content)
}
@@ -53,7 +54,7 @@ func DownloadFile(ctx *context.Context) error {
ctx.Response().Header().Set("Content-Type", file.MimeType.Header())
ctx.Response().Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(file.Filename)+"\"")
ctx.Response().Header().Set("Content-Length", strconv.Itoa(len(file.Content)))
ctx.Response().Header().Set("Content-Length", strconv.Itoa(int(file.Size)))
ctx.Response().Header().Set("X-Content-Type-Options", "nosniff")
_, err = ctx.Response().Write([]byte(file.Content))
if err != nil {
@@ -65,6 +65,7 @@ func TestRawFile(t *testing.T) {
resp := s.Request(t, "GET", "/"+username+"/"+identifier+"/raw/HEAD/file.txt", nil, 200)
require.Equal(t, `inline; filename="file.txt"`, resp.Header.Get("Content-Disposition"))
require.Equal(t, "11", resp.Header.Get("Content-Length"))
require.Equal(t, "nosniff", resp.Header.Get("X-Content-Type-Options"))
require.Contains(t, resp.Header.Get("Content-Type"), "text/plain")
+6
View File
@@ -194,7 +194,9 @@ func (s *Server) registerRoutes() {
sC.POST("/archive", gist.ToggleArchive, logged, writePermission)
sC.POST("/delete", gist.DeleteGist, logged, writePermission)
sC.GET("/raw/:revision/:file", gist.RawFile)
sC.HEAD("/raw/:revision/:file", gist.RawFile)
sC.GET("/download/:revision/:file", gist.DownloadFile)
sC.HEAD("/download/:revision/:file", gist.DownloadFile)
sC.GET("/edit", gist.Edit, logged, writePermission, notArchived)
sC.POST("/edit", gist.ProcessCreate, logged, writePermission, notArchived)
sC.POST("/like", gist.Like, logged)
@@ -259,6 +261,10 @@ func (r *Router) GET(path string, h Handler, m ...Middleware) {
r.Group.GET(path, chain(h, m...).toEchoHandler())
}
func (r *Router) HEAD(path string, h Handler, m ...Middleware) {
r.Group.HEAD(path, chain(h, m...).toEchoHandler())
}
func (r *Router) POST(path string, h Handler, m ...Middleware) {
r.Group.POST(path, chain(h, m...).toEchoHandler())
}