Delete empty files and improve entry viewing
This commit is contained in:
parent
ed230055f4
commit
2fe88259ec
4 changed files with 46 additions and 46 deletions
2
TODO.md
2
TODO.md
|
@ -4,10 +4,8 @@ List of things to add to this project
|
||||||
* Better docs in case others want to use ths for some reason
|
* Better docs in case others want to use ths for some reason
|
||||||
* Github/Codeberg/whatever mirror for when `faye` (my server) is offline
|
* Github/Codeberg/whatever mirror for when `faye` (my server) is offline
|
||||||
* Improve config and use reflection when loading it
|
* Improve config and use reflection when loading it
|
||||||
* Do to other methods what was done to GetDays/GetNotes?
|
|
||||||
* API revamp
|
* API revamp
|
||||||
* Check export function for improvements
|
* Check export function for improvements
|
||||||
* Add note creation and deletion to frontend (*properly*)
|
|
||||||
* Customise log file
|
* Customise log file
|
||||||
* More slog.Debug and a debug flag?
|
* More slog.Debug and a debug flag?
|
||||||
* Consider less clunky auth method
|
* Consider less clunky auth method
|
||||||
|
|
10
files.go
10
files.go
|
@ -32,7 +32,15 @@ func ReadFile(filename string) ([]byte, error) {
|
||||||
|
|
||||||
// SaveFile Writes request's contents to a file
|
// SaveFile Writes request's contents to a file
|
||||||
func SaveFile(filename string, contents []byte) error {
|
func SaveFile(filename string, contents []byte) error {
|
||||||
|
contents = bytes.TrimSpace(contents)
|
||||||
filename = "data/" + filename + ".txt"
|
filename = "data/" + filename + ".txt"
|
||||||
|
if len(contents) == 0 { // Delete empty files
|
||||||
|
err := os.Remove(filename)
|
||||||
|
slog.Error("error deleting empty file",
|
||||||
|
"error", err,
|
||||||
|
"file", filename)
|
||||||
|
return err
|
||||||
|
}
|
||||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error opening/making file",
|
slog.Error("error opening/making file",
|
||||||
|
@ -40,7 +48,7 @@ func SaveFile(filename string, contents []byte) error {
|
||||||
"file", filename)
|
"file", filename)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := f.Write(bytes.TrimSpace(contents)); err != nil {
|
if _, err := f.Write(contents); err != nil {
|
||||||
slog.Error("error writing to file",
|
slog.Error("error writing to file",
|
||||||
"error", err,
|
"error", err,
|
||||||
"file", filename)
|
"file", filename)
|
||||||
|
|
78
routes.go
78
routes.go
|
@ -131,6 +131,37 @@ func GetNotes(w http.ResponseWriter, r *http.Request) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetEntry(w http.ResponseWriter, r *http.Request, title string, filename string, editable bool) {
|
||||||
|
entry, err := ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
if editable && errors.Is(err, os.ErrNotExist) {
|
||||||
|
entry = []byte("")
|
||||||
|
} else {
|
||||||
|
slog.Error("error reading entry file", "error", err, "file", filename)
|
||||||
|
InternalError(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
files := []string{"./pages/base.html"}
|
||||||
|
if editable {
|
||||||
|
files = append(files, "./pages/edit.html")
|
||||||
|
} else {
|
||||||
|
files = append(files, "./pages/entry.html")
|
||||||
|
}
|
||||||
|
ts, err := template.ParseFiles(files...)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ts.ExecuteTemplate(w, "base", Entry{Title: title, Content: string(entry)})
|
||||||
|
if err != nil {
|
||||||
|
InternalError(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetDay renders HTML page for a specific day entry
|
// GetDay renders HTML page for a specific day entry
|
||||||
func GetDay(w http.ResponseWriter, r *http.Request) {
|
func GetDay(w http.ResponseWriter, r *http.Request) {
|
||||||
dayString := chi.URLParam(r, "day")
|
dayString := chi.URLParam(r, "day")
|
||||||
|
@ -139,34 +170,18 @@ func GetDay(w http.ResponseWriter, r *http.Request) {
|
||||||
HandleWrite(w.Write([]byte("day not specified")))
|
HandleWrite(w.Write([]byte("day not specified")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if dayString == time.Now().Format(time.DateOnly) { // today can still be edited
|
if dayString == time.Now().Format(time.DateOnly) { // Today can still be edited
|
||||||
http.Redirect(w, r, "/", 302)
|
http.Redirect(w, r, "/", 302)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
day, err := ReadFile("day/" + dayString)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("error reading day's file", "error", err, "day", dayString)
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
files := []string{"./pages/base.html", "./pages/entry.html"}
|
|
||||||
ts, err := template.ParseFiles(files...)
|
|
||||||
if err != nil {
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
|
title := dayString
|
||||||
t, err := time.Parse(time.DateOnly, dayString)
|
t, err := time.Parse(time.DateOnly, dayString)
|
||||||
if err == nil { // This is low priority so silently fail
|
if err == nil { // This is low priority so silently fail
|
||||||
dayString = t.Format("02 Jan 2006")
|
title = t.Format("02 Jan 2006")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", Entry{Content: string(day), Title: dayString})
|
GetEntry(w, r, title, "day/"+dayString, false)
|
||||||
if err != nil {
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNote renders HTML page for a note
|
// GetNote renders HTML page for a note
|
||||||
|
@ -177,29 +192,8 @@ func GetNote(w http.ResponseWriter, r *http.Request) {
|
||||||
HandleWrite(w.Write([]byte("note not specified")))
|
HandleWrite(w.Write([]byte("note not specified")))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
note, err := ReadFile("notes/" + noteString)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
note = []byte("")
|
|
||||||
} else {
|
|
||||||
slog.Error("error reading note's file", "error", err)
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
files := []string{"./pages/base.html", "./pages/edit.html"}
|
GetEntry(w, r, noteString, "notes/"+noteString, true)
|
||||||
ts, err := template.ParseFiles(files...)
|
|
||||||
if err != nil {
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ts.ExecuteTemplate(w, "base", Entry{Title: noteString, Content: string(note)})
|
|
||||||
if err != nil {
|
|
||||||
InternalError(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostNote saves a note form and redirects back to GET
|
// PostNote saves a note form and redirects back to GET
|
||||||
|
|
Loading…
Reference in a new issue