Files
filebrowser/edit/edit.go
T

54 lines
885 B
Go
Raw Normal View History

2015-09-12 20:02:26 +01:00
package edit
import (
"io/ioutil"
"net/http"
"os"
2015-09-13 21:10:54 +01:00
"strings"
2015-09-13 12:14:18 +01:00
"github.com/hacdias/caddy-hugo/page"
2015-09-13 20:37:20 +01:00
"github.com/spf13/hugo/commands"
2015-09-12 20:02:26 +01:00
)
2015-09-13 20:37:20 +01:00
type fileInfo struct {
Content string
Name string
2015-09-12 20:02:26 +01:00
}
// Execute sth
2015-09-13 21:10:54 +01:00
func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
filename := strings.Replace(r.URL.Path, "edit/", "", 1)
2015-09-12 20:02:26 +01:00
if r.Method == "POST" {
2015-09-13 20:37:20 +01:00
r.ParseForm()
err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666)
if err != nil {
return 500, err
}
commands.Execute()
2015-09-12 20:02:26 +01:00
} else {
2015-09-13 20:37:20 +01:00
if _, err := os.Stat(filename); os.IsNotExist(err) {
2015-09-12 20:02:26 +01:00
return 404, nil
}
2015-09-13 20:37:20 +01:00
file, err := ioutil.ReadFile(filename)
2015-09-13 12:14:18 +01:00
if err != nil {
return 500, err
}
2015-09-13 20:37:20 +01:00
inf := new(fileInfo)
inf.Content = string(file)
inf.Name = filename
2015-09-12 22:18:29 +01:00
page := new(page.Page)
2015-09-13 12:14:18 +01:00
page.Title = "Edit"
page.Body = inf
2015-09-13 12:14:18 +01:00
return page.Render("edit", w)
2015-09-12 20:02:26 +01:00
}
return 200, nil
}