Create files and dirs
parent
4729dc8c1a
commit
c871632f15
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
package server
|
||||||
|
|
@ -4,15 +4,26 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
func notesDir(path Path, w http.ResponseWriter, r *http.Request) {
|
func notesDir(p Path, w http.ResponseWriter, r *http.Request) {
|
||||||
dirs, files := lsDir(path)
|
dirs, files := lsDir(p)
|
||||||
content := dirs.List()
|
content := dirs.List()
|
||||||
|
notesDirHead(p, w)
|
||||||
block(content, w)
|
block(content, w)
|
||||||
fmt.Fprintln(w, files.List())
|
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) {
|
func lsDir(path Path) (Paths, Paths) {
|
||||||
dirs := newDirs()
|
dirs := newDirs()
|
||||||
files := newFiles()
|
files := newFiles()
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ func (s *Server) Routes() error {
|
||||||
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
||||||
handler: s.submit,
|
handler: s.submit,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
|
||||||
|
handler: s.create,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, endpoint := range endpoints {
|
for _, endpoint := range endpoints {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
|
||||||
content = html.UnescapeString(content)
|
content = html.UnescapeString(content)
|
||||||
content = strings.ReplaceAll(content, "\r", "")
|
content = strings.ReplaceAll(content, "\r", "")
|
||||||
p := NewPathFromURL(r.URL.Path)
|
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 {
|
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue