package server import ( "bytes" "fmt" "html" "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] }