flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit ef9c8adee1863a8ce60471c6b8ad3705d817d303
parent 22efcc1c6e773ef692dd41d04651166ac1f57c78
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu, 18 Nov 2021 20:14:20 -0800

Fix files with spaces in the name

Diffstat:
Mgemini.go | 8+++++++-
Mhttp.go | 8+++++++-
Mtemplates/folder.gmi | 2+-
Mtemplates/gemfeed.gmi | 2+-
4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/gemini.go b/gemini.go @@ -124,7 +124,13 @@ func gmiPage(_ context.Context, w gmi.ResponseWriter, r *gmi.Request) { func runGeminiServer() { log.Println("Starting gemini server") var err error - gt, err = template.ParseGlob(path.Join(c.TemplatesDirectory, "*.gmi")) + gt = template.New("main").Funcs(template.FuncMap{ + "urlencode": func(s string) string { + // Only need to escape spaces to make gemini links not break + return strings.Replace(s, " ", "+", -1) + }, + }) + gt, err = gt.ParseGlob(path.Join(c.TemplatesDirectory, "*.gmi")) if err != nil { log.Fatal(err) } diff --git a/http.go b/http.go @@ -571,7 +571,13 @@ func userFile(w http.ResponseWriter, r *http.Request) { } else { userName = filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary } - p := filepath.Clean(r.URL.Path) + unescaped, err := url.QueryUnescape(r.URL.Path) + if err != nil { + serverError(w, err) + return + } + p := filepath.Clean(unescaped) + fmt.Println(p) var isDir bool fullPath := path.Join(c.FilesDirectory, userName, p) // TODO rename filepath stat, err := os.Stat(fullPath) diff --git a/templates/folder.gmi b/templates/folder.gmi @@ -1,4 +1,4 @@ {{$host := .Host }} # Index of {{ .Folder }}/ {{ range .Files }} -=> //{{.Creator}}.{{$host}}/{{.Name}} {{.Name}}{{ end }} +=> //{{.Creator}}.{{$host}}/{{urlencode .Name}} {{.Name}}{{ end }} diff --git a/templates/gemfeed.gmi b/templates/gemfeed.gmi @@ -1,6 +1,6 @@ {{$host := .Host }} # {{ .Title }} {{ range .FeedEntries }} -=> /{{.File}} {{.DateString}} {{.Title}} {{end}} +=> /{{urlencode .File}} {{.DateString}} {{.Title}} {{end}} => /gemlog/atom.xml Subscribe via Atom