Skip to content

Commit

Permalink
routes: add raw file view
Browse files Browse the repository at this point in the history
  • Loading branch information
icyphox committed Feb 25, 2024
1 parent bdfc973 commit a87f88a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 11 additions & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"sort"
"strconv"
"time"

"git.icyphox.sh/legit/config"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions routes/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion templates/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<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>
Expand Down

0 comments on commit a87f88a

Please sign in to comment.