Base64 encode note titles

This commit is contained in:
Maximilian Friedersdorff 2025-06-27 22:56:50 +01:00
parent eb0c264ad7
commit 6f8796c83f
6 changed files with 78 additions and 22 deletions

View file

@ -4,6 +4,7 @@ package notes
import (
"bytes"
"encoding/base64"
"fmt"
"html/template"
"log"
@ -29,7 +30,7 @@ func fmtPath(path string) string {
// Save a note to a path derived from the title
func (n *Note) Save() error {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(n.Title))
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(n.EncodedTitle()))
return os.WriteFile(filename, n.Body, 0600)
}
@ -50,12 +51,13 @@ func (n *Note) Render() {
}
// Load a note from the disk. The path is derived from the title
func LoadNote(title string) (*Note, error) {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
func LoadNote(encodedTitle string) (*Note, error) {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(encodedTitle))
body, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
title := DecodeTitle(encodedTitle)
return &Note{Title: title, Body: body}, nil
}
@ -63,3 +65,16 @@ func DeleteNote(title string) error {
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
return os.Remove(filename)
}
func (n *Note) EncodedTitle() string {
return base64.StdEncoding.EncodeToString([]byte(n.Title))
}
func DecodeTitle(encodedTitle string) string {
title, err := base64.StdEncoding.DecodeString(encodedTitle)
if err != nil {
log.Printf("Couldn't decode base64 string '%s': %s", encodedTitle, err)
}
return string(title)
}

View file

@ -19,12 +19,13 @@ func GetRoutes(prefix string) *http.ServeMux {
myurls = urls.URLs{
Prefix: prefix,
URLs: map[string]urls.URL{
"list": {Path: "/", Protocol: "GET", Handler: list},
"new": {Path: "/new", Protocol: "GET", Handler: new},
"view_": {Path: "/{note}", Protocol: "GET", Handler: view},
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
"delete": {Path: "/{note}/delete/", Protocol: "GET", Handler: delete},
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
"list": {Path: "/", Protocol: "GET", Handler: list},
},
}
return myurls.GetRouter()
@ -51,13 +52,14 @@ func view(w http.ResponseWriter, r *http.Request) {
}
func edit(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
note, err := notes.LoadNote(title)
encodedTitle := r.PathValue("note")
note, err := notes.LoadNote(encodedTitle)
if err != nil {
title := notes.DecodeTitle(encodedTitle)
note = &notes.Note{Title: title}
}
urlSave := myurls.Reverse("save", urls.Repl{"note": title})
urlSave := myurls.Reverse("save", urls.Repl{"note": encodedTitle})
context := templ.Ctx{"note": note, "urlSave": urlSave, "text": string(note.Body)}
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
if err != nil {
@ -67,17 +69,25 @@ func edit(w http.ResponseWriter, r *http.Request) {
}
}
func new(w http.ResponseWriter, r *http.Request) {
title := r.FormValue("title")
note := &notes.Note{Title: title}
urlEdit := myurls.Reverse("edit", urls.Repl{"note": note.EncodedTitle()})
http.Redirect(w, r, urlEdit, http.StatusFound)
}
func delete(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
err := notes.DeleteNote(title)
encodedTitle := r.PathValue("note")
err := notes.DeleteNote(encodedTitle)
if err != nil {
log.Print(err.Error())
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
return
}
urlDelete := myurls.Reverse("list", urls.Repl{})
http.Redirect(w, r, urlDelete, http.StatusFound)
urlList := myurls.Reverse("list", urls.Repl{})
http.Redirect(w, r, urlList, http.StatusFound)
}
func save(w http.ResponseWriter, r *http.Request) {
@ -88,11 +98,16 @@ func save(w http.ResponseWriter, r *http.Request) {
note := &notes.Note{Title: title, Body: []byte(body)}
note.Save()
if oldTitle != title {
if oldTitle != note.EncodedTitle() {
notes.DeleteNote(oldTitle)
}
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
}
type titleAndUrl struct {
Title string
Url string
}
func list(w http.ResponseWriter, r *http.Request) {
@ -103,16 +118,26 @@ func list(w http.ResponseWriter, r *http.Request) {
return
}
titles := make([]string, 0)
titlesAndUrls := make([]titleAndUrl, 0)
for _, f := range files {
if !f.IsDir() {
title := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
titles = append(titles, title)
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
title := notes.DecodeTitle(encodedTitle)
log.Printf("Found note %s (title '%s')", encodedTitle, title)
titlesAndUrls = append(
titlesAndUrls,
titleAndUrl{Title: title, Url: myurls.Reverse("view", urls.Repl{"note": encodedTitle})},
)
log.Print(titlesAndUrls)
}
}
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"titles": titles})
urlNew := myurls.Reverse("new", urls.Repl{})
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
if err != nil {
log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)