From a140d0eade5387e184f45f99ead5299ec99ccc56 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 21 Nov 2019 13:12:30 -0700 Subject: [PATCH] Impl complete testing needed --- TODO | 17 +++++++++-------- filetree/path.go | 4 ++++ filetree/paths.go | 8 ++++++-- notes/delete.go | 15 +++++++++++++++ notes/delete_test.go | 1 + notes/search.go | 15 ++++++++------- public/A/D/G/A/b | 4 +++- public/A/abc | 1 - public/A/asdf | 3 ++- public/A/x | 12 ------------ server/delete.go | 16 ++++++++++++++++ server/notes.go | 9 +++++++++ server/routes.go | 8 ++++++++ server/search.go | 26 ++++++++++++++++++++++++++ wrapper.html | 5 ++++- 15 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 notes/delete.go create mode 100644 notes/delete_test.go delete mode 100755 public/A/abc delete mode 100755 public/A/x create mode 100755 server/delete.go create mode 100755 server/search.go diff --git a/TODO b/TODO index 89ca389..61fdca4 100644 --- a/TODO +++ b/TODO @@ -27,11 +27,12 @@ x main test - - dir->dir, - dir->file -TOC levels -delete pages -search -move auth as flag in router -. and ../** as roots cause bugs in listing and loading and linking -`create` at root is a 400, base= in URL (when `create` input is empty) -FTS - https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go +x TOC levels +x delete pages +x search + FTS + https://stackoverflow.com/questions/26709971/could-this-be-more-efficient-in-go +x move auth as flag in router +x . and ../** as roots cause bugs in listing and loading and linking +x `create` at root is a 400, base= in URL (when `create` input is empty) +delete top-level pages diff --git a/filetree/path.go b/filetree/path.go index cd4eab2..450371d 100755 --- a/filetree/path.go +++ b/filetree/path.go @@ -71,6 +71,10 @@ func (p Path) MultiLink() string { return full } +func (p Path) FullLI() string { + return fmt.Sprintf(`
  • %s
  • `, p.HREF, p.HREF) +} + func (p Path) LI() string { return fmt.Sprintf(`
  • %s
  • `, p.HREF, p.Base) } diff --git a/filetree/paths.go b/filetree/paths.go index 5a34cc7..7c3649a 100755 --- a/filetree/paths.go +++ b/filetree/paths.go @@ -2,10 +2,14 @@ package filetree type Paths []Path -func (p Paths) List() string { +func (p Paths) List(full ...bool) string { content := "\n" return content diff --git a/notes/delete.go b/notes/delete.go new file mode 100644 index 0000000..a265c63 --- /dev/null +++ b/notes/delete.go @@ -0,0 +1,15 @@ +package notes + +import ( + "errors" + "local/notes-server/filetree" + "os" +) + +func (n *Notes) Delete(urlPath string) error { + p := filetree.NewPathFromURL(urlPath) + if p.IsDir() { + return errors.New("path is dir") + } + return os.Remove(p.Local) +} diff --git a/notes/delete_test.go b/notes/delete_test.go new file mode 100644 index 0000000..6e8cef9 --- /dev/null +++ b/notes/delete_test.go @@ -0,0 +1 @@ +package notes diff --git a/notes/search.go b/notes/search.go index 7e8e4ef..ae6515b 100644 --- a/notes/search.go +++ b/notes/search.go @@ -5,31 +5,32 @@ import ( "bytes" "local/notes-server/filetree" "os" + "path" "path/filepath" ) func (n *Notes) Search(phrase string) (string, error) { files := filetree.NewFiles() err := filepath.Walk(n.root, - func(path string, info os.FileInfo, err error) error { + func(walked string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } - ok, err := grepFile(path, []byte(phrase)) - if ok { - p := filetree.NewPathFromLocal(path) + ok, err := grepFile(walked, []byte(phrase)) + if err == nil && ok { + p := filetree.NewPathFromLocal(path.Dir(walked)) files.Push(p, info) } return err }, ) - return filetree.Paths(*files).List(), err + return filetree.Paths(*files).List(true), err } -func grepFile(file string, pat []byte) (bool, error) { +func grepFile(file string, phrase []byte) (bool, error) { f, err := os.Open(file) if err != nil { return false, err @@ -37,7 +38,7 @@ func grepFile(file string, pat []byte) (bool, error) { defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { - if bytes.Contains(scanner.Bytes(), pat) { + if bytes.Contains(scanner.Bytes(), phrase) { return true, scanner.Err() } } diff --git a/public/A/D/G/A/b b/public/A/D/G/A/b index 5e40c08..91359cd 100755 --- a/public/A/D/G/A/b +++ b/public/A/D/G/A/b @@ -1 +1,3 @@ -asdf \ No newline at end of file +asdf + +this contains my search string \ No newline at end of file diff --git a/public/A/abc b/public/A/abc deleted file mode 100755 index 5e40c08..0000000 --- a/public/A/abc +++ /dev/null @@ -1 +0,0 @@ -asdf \ No newline at end of file diff --git a/public/A/asdf b/public/A/asdf index 5e40c08..ba4b0bc 100755 --- a/public/A/asdf +++ b/public/A/asdf @@ -1 +1,2 @@ -asdf \ No newline at end of file +asdf +searchString diff --git a/public/A/x b/public/A/x deleted file mode 100755 index 24dd6a1..0000000 --- a/public/A/x +++ /dev/null @@ -1,12 +0,0 @@ -# A.x - -| hello | world | -|-------|-------| -| cont | ent. | - -## A.X - -1 -2 - -3 \ No newline at end of file diff --git a/server/delete.go b/server/delete.go new file mode 100755 index 0000000..b49543c --- /dev/null +++ b/server/delete.go @@ -0,0 +1,16 @@ +package server + +import ( + "net/http" + "path" + "strings" +) + +func (s *Server) delete(w http.ResponseWriter, r *http.Request) { + if err := s.Notes.Delete(r.URL.Path); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + r.URL.Path = strings.Replace(path.Dir(r.URL.Path), "delete", "notes", 1) + http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect) +} diff --git a/server/notes.go b/server/notes.go index a76de72..8f5a914 100644 --- a/server/notes.go +++ b/server/notes.go @@ -23,6 +23,12 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) { func notesHead(w http.ResponseWriter, p filetree.Path) { fmt.Fprintln(w, h2(p.MultiLink())) + fmt.Fprintf(w, ` +
    + + +
    + `, "/search") } func (s *Server) dir(w http.ResponseWriter, r *http.Request) { @@ -59,4 +65,7 @@ func fileHead(w http.ResponseWriter, baseHREF string) { fmt.Fprintf(w, ` `, path.Join("/edit/", baseHREF)) + fmt.Fprintf(w, ` + + `, path.Join("/delete/", baseHREF)) } diff --git a/server/routes.go b/server/routes.go index 00b620b..4ff3374 100755 --- a/server/routes.go +++ b/server/routes.go @@ -24,6 +24,10 @@ func (s *Server) Routes() error { path: fmt.Sprintf("edit/%s%s", wildcard, wildcard), handler: s.authenticate(s.edit), }, + { + path: fmt.Sprintf("delete/%s%s", wildcard, wildcard), + handler: s.authenticate(s.delete), + }, { path: fmt.Sprintf("submit/%s%s", wildcard, wildcard), handler: s.authenticate(s.submit), @@ -32,6 +36,10 @@ func (s *Server) Routes() error { path: fmt.Sprintf("create/%s%s", wildcard, wildcard), handler: s.authenticate(s.create), }, + { + path: fmt.Sprintf("search"), + handler: s.authenticate(s.search), + }, } for _, endpoint := range endpoints { diff --git a/server/search.go b/server/search.go new file mode 100755 index 0000000..695820c --- /dev/null +++ b/server/search.go @@ -0,0 +1,26 @@ +package server + +import ( + "fmt" + "html" + "local/notes-server/filetree" + "net/http" +) + +func (s *Server) search(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + keywords := r.FormValue("keywords") + keywords = html.UnescapeString(keywords) + results, err := s.Notes.Search(keywords) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + head(w, r) + fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink())) + fmt.Fprintln(w, h1(keywords), results) + foot(w, r) +} diff --git a/wrapper.html b/wrapper.html index c0d5db6..fffcb2d 100644 --- a/wrapper.html +++ b/wrapper.html @@ -1,6 +1,6 @@
    - +