aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/git
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-06-23 17:04:27 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-06-23 17:04:27 +0200
commit72495f4538215051540eb05c14db0ed16142e06e (patch)
tree293f0f4a9ad4954b8a2390d392e0a4a3bc9a3569 /pkg/handler/git
parent6b96b76d66a929a2b428505809fda23a19005c63 (diff)
downloadcerrado-72495f4538215051540eb05c14db0ed16142e06e.tar.gz
cerrado-72495f4538215051540eb05c14db0ed16142e06e.tar.bz2
cerrado-72495f4538215051540eb05c14db0ed16142e06e.zip
ref: Move binary handling up to the handler
Now it is up to the handler to decide whether it wants to render a file or not, git only returns the content.
Diffstat (limited to 'pkg/handler/git')
-rw-r--r--pkg/handler/git/handler.go29
1 files changed, 25 insertions, 4 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 8bb4002..4c5d198 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -35,8 +35,9 @@ type (
ListCommits(name string, ref string, count int) ([]*object.Commit, error)
GetHead(name string) (*plumbing.Reference, error)
GetTree(name, ref, path string) (*object.Tree, error)
- GetFileContent(name, ref, path string) (string, error)
- GetAbout(name string) (string, error)
+ IsBinary(name, ref, path string) (bool, error)
+ GetFileContent(name, ref, path string) ([]byte, error)
+ GetAbout(name string) ([]byte, error)
ListTags(name string) ([]*plumbing.Reference, error)
ListBranches(name string) ([]*plumbing.Reference, error)
WriteTarGZip(w io.Writer, name, ref, prefix string) error
@@ -165,7 +166,7 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
- doc := p.Parse([]byte(file))
+ doc := p.Parse(file)
htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank
opts := markdownhtml.RendererOptions{Flags: htmlFlag}
@@ -244,6 +245,25 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error {
ref := r.PathValue("ref")
rest := r.PathValue("rest")
+ isBin, err := g.gitService.IsBinary(name, ref, rest)
+ if err != nil {
+ return err
+ }
+
+ // if it is binary no need to over all the chroma process
+ if isBin {
+ gitList := &templates.GitItemPage{
+ Name: name,
+ Ref: ref,
+ GitItemBase: &templates.GitItemBlobPage{
+ File: rest,
+ Content: []byte("Binary file"),
+ },
+ }
+ templates.WritePageTemplate(w, gitList)
+ return nil
+ }
+
file, err := g.gitService.GetFileContent(name, ref, rest)
if err != nil {
return err
@@ -255,7 +275,8 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error {
formatter := html.New(
html.WithLineNumbers(true),
)
- iterator, err := lexer.Tokenise(nil, file)
+
+ iterator, err := lexer.Tokenise(nil, string(file))
if err != nil {
return err
}