Base64 encode note titles
This commit is contained in:
parent
eb0c264ad7
commit
6f8796c83f
6 changed files with 78 additions and 22 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue