From e1664fcbc4685906d3dabc66bf947a17bce7efc0 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 22 Jun 2024 16:30:47 +0200 Subject: feat: Add archive capability --- pkg/handler/git/handler.go | 36 ++++++++++++++++++++++++++++++++++++ pkg/handler/router.go | 1 + 2 files changed, 37 insertions(+) (limited to 'pkg/handler') diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 25505ba..aed9917 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -2,10 +2,13 @@ package git import ( "bytes" + "fmt" "io" + "log/slog" "net/http" "os" "path/filepath" + "strings" "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/pkg/service" @@ -36,6 +39,7 @@ type ( GetAbout(name string) (string, error) ListTags(name string) ([]*plumbing.Reference, error) ListBranches(name string) ([]*plumbing.Reference, error) + WriteTarGZip(w io.Writer, name, ref, filename string) error } configurationRepository interface { @@ -84,6 +88,38 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) error { return nil } +func (g *GitHandler) Archive(w http.ResponseWriter, r *http.Request) error { + ext.SetGZip(w) + name := r.PathValue("name") + refs := r.PathValue("refs") + ref := strings.TrimSuffix(refs, ".tar.gz") + + // TODO: remove it once we can support more than gzip + if !strings.HasSuffix(refs, ".tar.gz") { + ext.NotFound(w) + return nil + } + + filenameWithExt := fmt.Sprintf("%s-%s.tar.gz", name, ref) + ext.SetFileName(w, filenameWithExt) + filename := fmt.Sprintf("%s-%s", name, ref) + + // writing to a buffer so we can run all the process before writing error + var buf bytes.Buffer + err := g.gitService.WriteTarGZip(&buf, name, ref, filename) + if err != nil { + return err + } + + // since that has write to w it cannot return a error. + _, err = io.Copy(w, &buf) + if err != nil { + slog.Error("Error copying buffer", "error", err) + } + + return nil +} + func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") diff --git a/pkg/handler/router.go b/pkg/handler/router.go index c8f8984..2293ab6 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -41,6 +41,7 @@ func MountHandler( mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree) mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob) mux.HandleFunc("/{name}/log/{ref}/", gitHandler.Log) + mux.HandleFunc("/{name}/archive/{refs...}", gitHandler.Archive) mux.HandleFunc("/config", configHandler) mux.HandleFunc("/about", aboutHandler.About) mux.HandleFunc("/", gitHandler.List) -- cgit v1.2.3