Skip to content

Commit f1707f4

Browse files
authored
Refactor render (#30136)
1 parent 0262c66 commit f1707f4

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

routers/web/repo/render.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"code.gitea.io/gitea/modules/charset"
1313
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/markup"
1516
"code.gitea.io/gitea/modules/typesniffer"
1617
"code.gitea.io/gitea/modules/util"
@@ -44,20 +45,17 @@ func RenderFile(ctx *context.Context) {
4445
isTextFile := st.IsText()
4546

4647
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
48+
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'; sandbox allow-scripts")
4749

4850
if markupType := markup.Type(blob.Name()); markupType == "" {
4951
if isTextFile {
50-
_, err = io.Copy(ctx.Resp, rd)
51-
if err != nil {
52-
ctx.ServerError("Copy", err)
53-
}
54-
return
52+
_, _ = io.Copy(ctx.Resp, rd)
53+
} else {
54+
http.Error(ctx.Resp, "Unsupported file type render", http.StatusInternalServerError)
5555
}
56-
ctx.Error(http.StatusInternalServerError, "Unsupported file type render")
5756
return
5857
}
5958

60-
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'; sandbox allow-scripts")
6159
err = markup.Render(&markup.RenderContext{
6260
Ctx: ctx,
6361
RelativePath: ctx.Repo.TreePath,
@@ -71,7 +69,8 @@ func RenderFile(ctx *context.Context) {
7169
InStandalonePage: true,
7270
}, rd, ctx.Resp)
7371
if err != nil {
74-
ctx.ServerError("Render", err)
72+
log.Error("Failed to render file %q: %v", ctx.Repo.TreePath, err)
73+
http.Error(ctx.Resp, "Failed to render file", http.StatusInternalServerError)
7574
return
7675
}
7776
}

0 commit comments

Comments
 (0)