notes-server/server/submit.go

40 lines
940 B
Go
Executable File

package server
import (
"bytes"
"fmt"
"html"
"gitea.inhome.blapointe.com/local/notes-server/filetree"
"net/http"
"path"
"strings"
)
func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
content := r.FormValue("content")
content = html.UnescapeString(content)
content = strings.ReplaceAll(content, "\r", "")
content = trimLines(content)
err := s.Notes.Submit(r.URL.Path, content)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := *r.URL
url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}
func trimLines(s string) string {
buff := bytes.NewBuffer(nil)
for _, line := range strings.Split(s, "\n") {
fmt.Fprintln(buff, strings.TrimRight(line, "\n \r\t"))
}
fixed := string(buff.Bytes())
return fixed[:len(fixed)-1]
}