fixing bugs with filenames

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

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

Loading…
Cancel
Save