mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2022 The Gitea Authors. 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
|
|
}
|
|
}
|