From a87f88a57c68627a1f63f1551fcef2c1f1ec7036 Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sun, 25 Feb 2024 23:15:21 +0200 Subject: routes: add raw file view --- routes/routes.go | 12 +++++++++++- routes/template.go | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'routes') diff --git a/routes/routes.go b/routes/routes.go index 0499a9e..2989752 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "sort" + "strconv" "time" "git.icyphox.sh/legit/config" @@ -199,6 +200,11 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { } func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { + var raw bool + if rawParam, err := strconv.ParseBool(r.URL.Query().Get("raw")); err == nil { + raw = rawParam + } + name := flow.Param(r.Context(), "name") if d.isIgnored(name) { d.Write404(w) @@ -222,7 +228,11 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { data["desc"] = getDescription(path) data["path"] = treePath - d.showFile(contents, data, w) + if raw { + d.showRaw(contents, w) + } else { + d.showFile(contents, data, w) + } return } diff --git a/routes/template.go b/routes/template.go index 20d678b..0b03015 100644 --- a/routes/template.go +++ b/routes/template.go @@ -95,3 +95,10 @@ func (d *deps) showFile(content string, data map[string]any, w http.ResponseWrit return } } + +func (d *deps) showRaw(content string, w http.ResponseWriter) { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(content)) + return +} -- cgit v1.2.3