Base64 encode note titles
This commit is contained in:
parent
eb0c264ad7
commit
6f8796c83f
6 changed files with 78 additions and 22 deletions
|
|
@ -1,3 +1,15 @@
|
|||
{{define "navLinks"}}
|
||||
{{end}}
|
||||
{{define "navExtra"}}
|
||||
<form class="row row-cols-lg-auto align-items-center" method="GET" action="{{ .urlNew }}">
|
||||
<div class="col-12">
|
||||
<input class="form-control mr-sm-2" type="text" placeholder="Title" aria-label="Title" name="title">
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<button class="btn btn-success my-2 my-sm-0" type="submit">New Note</button>
|
||||
</div>
|
||||
</form>
|
||||
{{end}}
|
||||
{{define "base"}}
|
||||
<!doctype html>
|
||||
<html lang='en'>
|
||||
|
|
@ -23,7 +35,9 @@
|
|||
<li class="nav-item">
|
||||
<a class="nav-link" href="/notes/">All Notes</a>
|
||||
</li>
|
||||
{{template "navLinks" .}}
|
||||
</ul>
|
||||
{{template "navExtra" .}}
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
{{define "title"}}Edit {{.note.Title}}{{end}}
|
||||
{{define "title"}}Edit: {{.note.Title}}{{end}}
|
||||
{{define "navExtra"}}<!-- -->{{end}}
|
||||
|
||||
{{define "main"}}
|
||||
<form action="{{.urlSave}}" method="POST">
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
{{define "title"}}All Notes{{end}}
|
||||
{{define "main"}}
|
||||
<div class="list-group list-group-flush">
|
||||
{{range $title := .titles}}
|
||||
{{range $note := .notes}}
|
||||
<a class="list-group-item list-group-item-action d-flex justify-content-between"
|
||||
href="{{$title}}/">
|
||||
<span>{{$title}}</span>
|
||||
href="{{$note.Url}}/">
|
||||
<span>{{$note.Title}}</span>
|
||||
<img src="/static/icons/eye.svg" alt="Edit">
|
||||
</a>
|
||||
{{end}}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = ¬es.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 := ¬es.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 := ¬es.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)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
type Ctx map[string]any
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||
var err error
|
||||
files := []string{
|
||||
filepath.Join("templates", "base.tmpl.html"),
|
||||
filepath.Join("templates", tmpl),
|
||||
|
|
@ -25,6 +26,6 @@ func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
|||
}
|
||||
|
||||
t, err := template.ParseFS(conf.Templates, files...)
|
||||
t.ExecuteTemplate(w, "base", context)
|
||||
err = t.ExecuteTemplate(w, "base", context)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue