fixing bugs with filenames

pull/8/head
Stephen Searles 7 years ago
parent ab6152cbfa
commit 3f8cb4f265
  1. 29
      caddyhugo.go

@ -311,25 +311,33 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
ch.mtx.Lock() ch.mtx.Lock()
defer ch.mtx.Unlock() defer ch.mtx.Unlock()
docref, ok := ch.docs[name] docref, ok := ch.docs[ch.docname(name)]
if !ok { if !ok {
return http.StatusNotFound, fmt.Errorf("draft not found") return http.StatusNotFound, fmt.Errorf("draft not found")
} }
http.StripPrefix( r.URL.Path = strings.ToLower(r.URL.Path)
"/hugo/draft/"+encoded,
http.FileServer(http.Dir(docref.tmpdir))).ServeHTTP(w, r) prefix := "/hugo/draft/" + encoded
r.URL.Path = r.URL.Path[len(prefix):]
http.FileServer(http.Dir(docref.tmpdir)).ServeHTTP(w, r)
return 200, nil return 200, nil
} }
func (ch *CaddyHugo) docname(orig string) string {
return strings.ToLower(orig)
}
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
ch.mtx.Lock() ch.mtx.Lock()
defer ch.mtx.Unlock() defer ch.mtx.Unlock()
name := r.URL.Path[len("/hugo/edit/"):] name := r.URL.Path[len("/hugo/edit/"):]
name = filepath.Join(ch.Site.Root, name) name = filepath.Join(ch.Site.Root, name)
name = strings.ToLower(name)
_, ok := ch.docs[name] _, ok := ch.docs[ch.docname(name)]
if !ok { if !ok {
fmt.Println("opening", name) fmt.Println("opening", name)
contents, err := ioutil.ReadFile(name) contents, err := ioutil.ReadFile(name)
@ -352,7 +360,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
return nil, err return nil, err
} }
ch.docs[name] = ref ch.docs[ch.docname(name)] = ref
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir)
hugoCmd.Dir = ch.Site.Root hugoCmd.Dir = ch.Site.Root
@ -397,7 +405,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
ref.doc.Close() ref.doc.Close()
os.RemoveAll(tmpdir) os.RemoveAll(tmpdir)
delete(ch.docs, name) delete(ch.docs, ch.docname(name))
ch.mtx.Unlock() ch.mtx.Unlock()
return return
} }
@ -409,7 +417,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
}() }()
} }
return ch.docs[name], nil return ch.docs[ch.docname(name)], nil
} }
func (ch *CaddyHugo) Publish() error { func (ch *CaddyHugo) Publish() error {
@ -504,11 +512,15 @@ func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, err
name += ".md" name += ".md"
} }
name = strings.ToLower(name)
filename := path.Join(ctype, name) filename := path.Join(ctype, name)
if ctype == "default" { if ctype == "default" {
filename = name filename = name
} }
_, err := os.Stat(path.Join(ch.Site.Root, "content", filename))
if os.IsNotExist(err) {
cmd := exec.Command("hugo", "new", filename) cmd := exec.Command("hugo", "new", filename)
cmd.Dir = ch.Site.Root cmd.Dir = ch.Site.Root
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
@ -516,6 +528,7 @@ func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, err
fmt.Println("error running hugo:\n", string(out)) fmt.Println("error running hugo:\n", string(out))
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
}
// serve redirect // serve redirect
http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound) http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)

Loading…
Cancel
Save