mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
|
|
"github.com/hanzoai/git/models/renderhelper"
|
|
"github.com/hanzoai/git/modules/git"
|
|
"github.com/hanzoai/git/modules/log"
|
|
"github.com/hanzoai/git/modules/markup"
|
|
"github.com/hanzoai/git/services/context"
|
|
)
|
|
|
|
// RenderFile renders a file by repos path
|
|
func RenderFile(ctx *context.Context) {
|
|
var blob *git.Blob
|
|
var err error
|
|
if ctx.Repo.TreePath != "" {
|
|
blob, err = ctx.Repo.Commit.GetBlobByPath(ctx, ctx.Repo.GitRepo, ctx.Repo.TreePath)
|
|
} else {
|
|
blob, err = ctx.Repo.GitRepo.GetBlob(ctx.PathParam("sha"))
|
|
}
|
|
if err != nil {
|
|
if git.IsErrNotExist(err) {
|
|
ctx.NotFound(err)
|
|
} else {
|
|
ctx.ServerError("GetBlobByPath", err)
|
|
}
|
|
return
|
|
}
|
|
|
|
blobReader, err := blob.DataAsync()
|
|
if err != nil {
|
|
ctx.ServerError("DataAsync", err)
|
|
return
|
|
}
|
|
defer blobReader.Close()
|
|
|
|
rctx := renderhelper.NewRenderContextRepoFile(ctx, ctx.Repo.Repository, renderhelper.RepoFileOptions{
|
|
CurrentRefSubURL: ctx.Repo.RefTypeNameSubURL(),
|
|
CurrentTreePath: path.Dir(ctx.Repo.TreePath),
|
|
}).WithRelativePath(ctx.Repo.TreePath).WithStandalonePage(markup.StandalonePageOptions{
|
|
CurrentWebTheme: ctx.TemplateContext.CurrentWebTheme(),
|
|
RenderQueryString: ctx.Req.URL.RawQuery,
|
|
})
|
|
renderer, rendererInput, err := rctx.DetectMarkupRendererByReader(blobReader)
|
|
if err != nil {
|
|
http.Error(ctx.Resp, "Unable to find renderer", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
extRenderer, ok := renderer.(markup.ExternalRenderer)
|
|
if !ok {
|
|
http.Error(ctx.Resp, "Unable to get external renderer", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// To render PDF in iframe, the sandbox must NOT be used (iframe & CSP header).
|
|
// Chrome blocks the PDF rendering when sandboxed, even if all "allow-*" are set.
|
|
// HINT: PDF-RENDER-SANDBOX: PDF won't render in sandboxed context
|
|
extRendererOpts := extRenderer.GetExternalRendererOptions()
|
|
if extRendererOpts.ContentSandbox != "" {
|
|
ctx.Resp.Header().Add("Content-Security-Policy", "sandbox "+extRendererOpts.ContentSandbox)
|
|
}
|
|
|
|
err = markup.RenderWithRenderer(rctx, renderer, rendererInput, ctx.Resp)
|
|
if err != nil {
|
|
log.Error("Failed to render file %q: %v", ctx.Repo.TreePath, err)
|
|
http.Error(ctx.Resp, "Failed to render file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|