routes: add raw file view
Anirudh Oppiliappan x@icyphox.sh
Sun, 25 Feb 2024 23:15:21 +0200
3 files changed,
19 insertions(+),
2 deletions(-)
M
routes/routes.go
→
routes/routes.go
@@ -8,6 +8,7 @@ "net/http"
"os" "path/filepath" "sort" + "strconv" "time" "git.icyphox.sh/legit/config"@@ -199,6 +200,11 @@ return
} 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 @@ data["ref"] = ref
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 }
M
routes/template.go
→
routes/template.go
@@ -95,3 +95,10 @@ log.Println(err)
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 +}
M
templates/file.html
→
templates/file.html
@@ -5,7 +5,7 @@ {{ template "repoheader" . }}
<body> {{ template "nav" . }} <main> - <p>{{ .path }}</p> + <p>{{ .path }} (<a style="color: gray" href="?raw=true">view raw</a>)</p> <div class="file-wrapper"> <table > <tbody><tr>