From c871632f152e2147fae8bc283acc63983e10578c Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 20 Oct 2019 01:14:00 +0000 Subject: [PATCH] Create files and dirs --- server/create.go | 23 +++++++++++++++++++++++ server/create_test.go | 1 + server/dir.go | 15 +++++++++++++-- server/routes.go | 4 ++++ server/submit.go | 1 + 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 server/create.go create mode 100644 server/create_test.go diff --git a/server/create.go b/server/create.go new file mode 100644 index 0000000..fb9898d --- /dev/null +++ b/server/create.go @@ -0,0 +1,23 @@ +package server + +import ( + "html" + "net/http" + "path" + "strings" +) + +func (s *Server) create(w http.ResponseWriter, r *http.Request) { + content := r.FormValue("base") + content = html.UnescapeString(content) + content = strings.ReplaceAll(content, "\r", "") + urlPath := path.Join(r.URL.Path, content) + p := NewPathFromURL(urlPath) + if p.IsDir() { + w.WriteHeader(http.StatusBadRequest) + return + } + url := *r.URL + url.Path = path.Join("/edit/", p.BaseHREF) + http.Redirect(w, r, url.String(), http.StatusSeeOther) +} diff --git a/server/create_test.go b/server/create_test.go new file mode 100644 index 0000000..abb4e43 --- /dev/null +++ b/server/create_test.go @@ -0,0 +1 @@ +package server diff --git a/server/dir.go b/server/dir.go index 91c6d1f..11dfd52 100755 --- a/server/dir.go +++ b/server/dir.go @@ -4,15 +4,26 @@ import ( "fmt" "io/ioutil" "net/http" + "path" ) -func notesDir(path Path, w http.ResponseWriter, r *http.Request) { - dirs, files := lsDir(path) +func notesDir(p Path, w http.ResponseWriter, r *http.Request) { + dirs, files := lsDir(p) content := dirs.List() + notesDirHead(p, w) block(content, w) fmt.Fprintln(w, files.List()) } +func notesDirHead(p Path, w http.ResponseWriter) { + fmt.Fprintf(w, ` +
+ + +
+ `, path.Join("/create/", p.BaseHREF)) +} + func lsDir(path Path) (Paths, Paths) { dirs := newDirs() files := newFiles() diff --git a/server/routes.go b/server/routes.go index dbc98a1..6fc786c 100755 --- a/server/routes.go +++ b/server/routes.go @@ -24,6 +24,10 @@ func (s *Server) Routes() error { path: fmt.Sprintf("submit/%s%s", wildcard, wildcard), handler: s.submit, }, + { + path: fmt.Sprintf("create/%s%s", wildcard, wildcard), + handler: s.create, + }, } for _, endpoint := range endpoints { diff --git a/server/submit.go b/server/submit.go index 6afde11..a7f3cff 100644 --- a/server/submit.go +++ b/server/submit.go @@ -19,6 +19,7 @@ func (s *Server) submit(w http.ResponseWriter, r *http.Request) { content = html.UnescapeString(content) content = strings.ReplaceAll(content, "\r", "") p := NewPathFromURL(r.URL.Path) + os.MkdirAll(path.Dir(p.Local), os.ModePerm) if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintln(w, err)