diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-23 15:20:47 +0200 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2024-06-29 11:16:38 +0300 |
commit | acac8d47d0dd4bab02274f750d22937044bee988 (patch) | |
tree | 439bb87c27e9982a594d66531b08180c567d521b /routes/routes.go | |
parent | git: Add function to generate tar file from repo (diff) | |
download | legitrx-acac8d47d0dd4bab02274f750d22937044bee988.tar.gz legitrx-acac8d47d0dd4bab02274f750d22937044bee988.tar.zst legitrx-acac8d47d0dd4bab02274f750d22937044bee988.zip |
routes: Add handler to generate tar gz file
Diffstat (limited to 'routes/routes.go')
-rw-r--r-- | routes/routes.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/routes/routes.go b/routes/routes.go index e22c7f9..14dbcf7 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,6 +1,7 @@ package routes import ( + "compress/gzip" "fmt" "html/template" "log" @@ -9,6 +10,7 @@ import ( "path/filepath" "sort" "strconv" + "strings" "time" "git.icyphox.sh/legit/config" @@ -235,6 +237,57 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { return } +func (d *deps) Archive(w http.ResponseWriter, r *http.Request) { + name := r.PathValue("name") + if d.isIgnored(name) { + d.Write404(w) + return + } + + file := r.PathValue("file") + + // TODO: extend this to add more files compression (e.g.: xz) + if !strings.HasSuffix(file, ".tar.gz") { + d.Write404(w) + return + } + + ref := strings.TrimSuffix(file, ".tar.gz") + + // This allows the browser to use a proper name for the file when + // downloading + filename := fmt.Sprintf("%s-%s.tar.gz", name, ref) + setContentDisposition(w, filename) + setGZipMIME(w) + + path := filepath.Join(d.c.Repo.ScanPath, name) + gr, err := git.Open(path, ref) + if err != nil { + d.Write404(w) + return + } + + gw := gzip.NewWriter(w) + defer gw.Close() + + prefix := fmt.Sprintf("%s-%s", name, ref) + err = gr.WriteTar(gw, prefix) + if err != nil { + // once we start writing to the body we can't report error anymore + // so we are only left with printing the error. + log.Println(err) + return + } + + err = gw.Flush() + if err != nil { + // once we start writing to the body we can't report error anymore + // so we are only left with printing the error. + log.Println(err) + return + } +} + func (d *deps) Log(w http.ResponseWriter, r *http.Request) { name := r.PathValue("name") if d.isIgnored(name) { |