Create files and dirs

master
bel 2019-10-20 01:14:00 +00:00
parent 4729dc8c1a
commit c871632f15
5 changed files with 42 additions and 2 deletions

23
server/create.go Normal file
View File

@ -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)
}

1
server/create_test.go Normal file
View File

@ -0,0 +1 @@
package server

View File

@ -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, `
<form action=%q method="get">
<input type="text" name="base"></input>
<button type="submit">Create</button>
</form>
`, path.Join("/create/", p.BaseHREF))
}
func lsDir(path Path) (Paths, Paths) {
dirs := newDirs()
files := newFiles()

View File

@ -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 {

View File

@ -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)