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"}}
|
{{define "base"}}
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang='en'>
|
<html lang='en'>
|
||||||
|
|
@ -23,7 +35,9 @@
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="/notes/">All Notes</a>
|
<a class="nav-link" href="/notes/">All Notes</a>
|
||||||
</li>
|
</li>
|
||||||
|
{{template "navLinks" .}}
|
||||||
</ul>
|
</ul>
|
||||||
|
{{template "navExtra" .}}
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
{{define "title"}}Edit {{.note.Title}}{{end}}
|
{{define "title"}}Edit: {{.note.Title}}{{end}}
|
||||||
|
{{define "navExtra"}}<!-- -->{{end}}
|
||||||
|
|
||||||
{{define "main"}}
|
{{define "main"}}
|
||||||
<form action="{{.urlSave}}" method="POST">
|
<form action="{{.urlSave}}" method="POST">
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
{{define "title"}}All Notes{{end}}
|
{{define "title"}}All Notes{{end}}
|
||||||
{{define "main"}}
|
{{define "main"}}
|
||||||
<div class="list-group list-group-flush">
|
<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"
|
<a class="list-group-item list-group-item-action d-flex justify-content-between"
|
||||||
href="{{$title}}/">
|
href="{{$note.Url}}/">
|
||||||
<span>{{$title}}</span>
|
<span>{{$note.Title}}</span>
|
||||||
<img src="/static/icons/eye.svg" alt="Edit">
|
<img src="/static/icons/eye.svg" alt="Edit">
|
||||||
</a>
|
</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ package notes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -29,7 +30,7 @@ func fmtPath(path string) string {
|
||||||
|
|
||||||
// Save a note to a path derived from the title
|
// Save a note to a path derived from the title
|
||||||
func (n *Note) Save() error {
|
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)
|
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
|
// Load a note from the disk. The path is derived from the title
|
||||||
func LoadNote(title string) (*Note, error) {
|
func LoadNote(encodedTitle string) (*Note, error) {
|
||||||
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(encodedTitle))
|
||||||
body, err := os.ReadFile(filename)
|
body, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
title := DecodeTitle(encodedTitle)
|
||||||
return &Note{Title: title, Body: body}, nil
|
return &Note{Title: title, Body: body}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,3 +65,16 @@ func DeleteNote(title string) error {
|
||||||
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
||||||
return os.Remove(filename)
|
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{
|
myurls = urls.URLs{
|
||||||
Prefix: prefix,
|
Prefix: prefix,
|
||||||
URLs: map[string]urls.URL{
|
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},
|
||||||
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
||||||
"delete": {Path: "/{note}/delete/", Protocol: "GET", Handler: delete},
|
"delete": {Path: "/{note}/delete/", Protocol: "GET", Handler: delete},
|
||||||
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
||||||
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
||||||
"list": {Path: "/", Protocol: "GET", Handler: list},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return myurls.GetRouter()
|
return myurls.GetRouter()
|
||||||
|
|
@ -51,13 +52,14 @@ func view(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func edit(w http.ResponseWriter, r *http.Request) {
|
func edit(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PathValue("note")
|
encodedTitle := r.PathValue("note")
|
||||||
note, err := notes.LoadNote(title)
|
note, err := notes.LoadNote(encodedTitle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
title := notes.DecodeTitle(encodedTitle)
|
||||||
note = ¬es.Note{Title: title}
|
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)}
|
context := templ.Ctx{"note": note, "urlSave": urlSave, "text": string(note.Body)}
|
||||||
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
|
||||||
if err != nil {
|
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) {
|
func delete(w http.ResponseWriter, r *http.Request) {
|
||||||
title := r.PathValue("note")
|
encodedTitle := r.PathValue("note")
|
||||||
err := notes.DeleteNote(title)
|
err := notes.DeleteNote(encodedTitle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
urlDelete := myurls.Reverse("list", urls.Repl{})
|
urlList := myurls.Reverse("list", urls.Repl{})
|
||||||
http.Redirect(w, r, urlDelete, http.StatusFound)
|
http.Redirect(w, r, urlList, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
func save(w http.ResponseWriter, r *http.Request) {
|
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 := ¬es.Note{Title: title, Body: []byte(body)}
|
||||||
note.Save()
|
note.Save()
|
||||||
|
|
||||||
if oldTitle != title {
|
if oldTitle != note.EncodedTitle() {
|
||||||
notes.DeleteNote(oldTitle)
|
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) {
|
func list(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -103,16 +118,26 @@ func list(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
titles := make([]string, 0)
|
titlesAndUrls := make([]titleAndUrl, 0)
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if !f.IsDir() {
|
if !f.IsDir() {
|
||||||
title := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||||
titles = append(titles, title)
|
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 {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
type Ctx map[string]any
|
type Ctx map[string]any
|
||||||
|
|
||||||
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
func RenderTemplate(w http.ResponseWriter, tmpl string, context any) error {
|
||||||
|
var err error
|
||||||
files := []string{
|
files := []string{
|
||||||
filepath.Join("templates", "base.tmpl.html"),
|
filepath.Join("templates", "base.tmpl.html"),
|
||||||
filepath.Join("templates", tmpl),
|
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, err := template.ParseFS(conf.Templates, files...)
|
||||||
t.ExecuteTemplate(w, "base", context)
|
err = t.ExecuteTemplate(w, "base", context)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue