From 72495f4538215051540eb05c14db0ed16142e06e Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 23 Jun 2024 17:04:27 +0200 Subject: 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. --- pkg/handler/git/handler.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'pkg/handler/git') 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 } -- cgit v1.2.3