package main
import (
_ "embed"
"fmt"
"html"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
var (
//go:embed favicon.ico
favIcn []byte
disTag = map[bool]string{
true: "",
false: "DISABLED",
}
rorw = map[bool]string{
true: "🔑 rw",
false: "🔒 ro",
}
)
func htErr(w http.ResponseWriter, msg string, err error) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Cache-Control", *cacheCtl)
fmt.Fprintln(w, msg, ":", err)
log.Printf("error: %v : %v", msg, err)
}
func header(w http.ResponseWriter, uDir, sort, extraCSS string) {
eDir := html.EscapeString(uDir)
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", *cacheCtl)
w.Write([]byte(`
` + *siteName + ` : ` + eDir + `
\n"))
}
func redirect(w http.ResponseWriter, uUrl string) {
w.Header().Set("Location", uUrl)
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", *cacheCtl)
w.WriteHeader(302)
w.Write([]byte(`
Go here...
`))
}
func upDnDir(uDir, uBn string, wfs afero.Fs) string {
o := strings.Builder{}
o.WriteString("\n")
p := "/"
i := 0
for _, n := range strings.Split(uDir, string(os.PathSeparator))[1:] {
p = p + n + "/"
opt := ""
if p == uDir+"/" {
opt = "DISABLED"
}
i++
o.WriteString("\n")
}
d, err := afero.ReadDir(wfs, uDir)
if err != nil {
return o.String()
}
for _, n := range d {
if !n.IsDir() || strings.HasPrefix(n.Name(), ".") {
continue
}
o.WriteString("\n")
}
return o.String()
}