filebrowser/routes/editor/post.go

190 lines
4.5 KiB
Go
Raw Normal View History

2015-09-26 21:02:49 +00:00
package editor
import (
"bytes"
"encoding/json"
2015-10-12 21:03:26 +00:00
"errors"
2016-06-07 17:15:55 +00:00
"fmt"
2015-09-26 21:02:49 +00:00
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"time"
2016-06-16 16:01:41 +00:00
"github.com/hacdias/caddy-hugo/config"
2016-03-06 20:37:49 +00:00
"github.com/hacdias/caddy-hugo/tools/hugo"
2016-03-11 20:38:57 +00:00
"github.com/hacdias/caddy-hugo/tools/server"
2015-09-26 21:02:49 +00:00
"github.com/robfig/cron"
2016-02-08 19:14:27 +00:00
"github.com/spf13/cast"
2015-09-26 21:02:49 +00:00
"github.com/spf13/hugo/parser"
)
2016-03-12 09:25:15 +00:00
type info struct {
ContentType string
Schedule bool
Regenerate bool
Content map[string]interface{}
}
2016-03-11 20:38:57 +00:00
2016-03-12 13:03:31 +00:00
type response struct {
Message string `json:"message"`
}
2015-09-26 21:02:49 +00:00
// POST handles the POST method on editor page
2016-06-16 16:01:41 +00:00
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
2016-06-07 17:15:55 +00:00
var data info
2015-09-26 21:02:49 +00:00
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
2016-03-12 09:25:15 +00:00
err := json.Unmarshal(rawBuffer.Bytes(), &data)
2015-09-26 21:02:49 +00:00
2016-06-07 17:15:55 +00:00
fmt.Println(string(rawBuffer.Bytes()))
2016-03-12 09:25:15 +00:00
if err != nil {
2016-03-12 13:03:31 +00:00
return server.RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
2016-03-11 20:38:57 +00:00
}
2015-09-26 21:02:49 +00:00
// Initializes the file content to write
var file []byte
2016-06-16 15:52:37 +00:00
var code int
2015-09-26 21:02:49 +00:00
2016-03-12 09:25:15 +00:00
switch data.ContentType {
2015-09-26 21:02:49 +00:00
case "frontmatter-only":
2016-06-16 16:01:41 +00:00
file, code, err = parseFrontMatterOnlyFile(data, filename)
2015-09-26 21:02:49 +00:00
if err != nil {
2016-03-12 13:03:31 +00:00
return server.RespondJSON(w, &response{err.Error()}, code, err)
2015-09-26 21:02:49 +00:00
}
case "content-only":
// The main content of the file
2016-03-12 09:25:15 +00:00
mainContent := data.Content["content"].(string)
2016-02-18 18:18:50 +00:00
mainContent = strings.TrimSpace(mainContent)
2015-09-26 21:02:49 +00:00
file = []byte(mainContent)
case "complete":
2016-06-16 16:01:41 +00:00
file, code, err = parseCompleteFile(data, filename, c)
2015-09-26 21:02:49 +00:00
if err != nil {
2016-03-12 13:03:31 +00:00
return server.RespondJSON(w, &response{err.Error()}, code, err)
2015-09-26 21:02:49 +00:00
}
default:
2016-03-12 13:03:31 +00:00
return server.RespondJSON(w, &response{"Invalid content type."}, http.StatusBadRequest, nil)
2015-09-26 21:02:49 +00:00
}
// Write the file
2016-03-12 09:25:15 +00:00
err = ioutil.WriteFile(filename, file, 0666)
2015-09-26 21:02:49 +00:00
if err != nil {
2016-03-12 13:03:31 +00:00
return server.RespondJSON(w, &response{err.Error()}, http.StatusInternalServerError, err)
2016-03-11 20:38:57 +00:00
}
2016-03-12 09:25:15 +00:00
if data.Regenerate {
2016-06-16 16:01:41 +00:00
go hugo.Run(c, false)
2015-09-26 21:02:49 +00:00
}
2016-03-12 09:25:15 +00:00
return server.RespondJSON(w, nil, http.StatusOK, nil)
2015-09-26 21:02:49 +00:00
}
2016-06-16 16:01:41 +00:00
func parseFrontMatterOnlyFile(data info, filename string) ([]byte, int, error) {
2015-09-26 21:02:49 +00:00
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
var mark rune
switch frontmatter {
case "toml":
mark = rune('+')
case "json":
mark = rune('{')
case "yaml":
mark = rune('-')
default:
2015-10-16 19:03:59 +00:00
return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
2015-09-26 21:02:49 +00:00
}
2016-03-12 09:25:15 +00:00
f, err := parser.InterfaceToFrontMatter(data.Content, mark)
2015-09-26 21:02:49 +00:00
fString := string(f)
// If it's toml or yaml, strip frontmatter identifier
if frontmatter == "toml" {
fString = strings.TrimSuffix(fString, "+++\n")
fString = strings.TrimPrefix(fString, "+++\n")
}
if frontmatter == "yaml" {
fString = strings.TrimSuffix(fString, "---\n")
fString = strings.TrimPrefix(fString, "---\n")
}
f = []byte(fString)
if err != nil {
2015-09-26 21:19:22 +00:00
return []byte{}, http.StatusInternalServerError, err
2015-09-26 21:02:49 +00:00
}
2015-09-26 21:19:22 +00:00
return f, http.StatusOK, nil
2015-09-26 21:02:49 +00:00
}
2016-06-16 16:01:41 +00:00
func parseCompleteFile(data info, filename string, c *config.Config) ([]byte, int, error) {
2015-09-26 21:02:49 +00:00
// The main content of the file
2016-03-12 09:25:15 +00:00
mainContent := data.Content["content"].(string)
2015-10-09 19:47:04 +00:00
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
2015-09-26 21:02:49 +00:00
// Removes the main content from the rest of the frontmatter
2016-03-12 09:25:15 +00:00
delete(data.Content, "content")
2015-09-26 21:02:49 +00:00
2016-03-12 10:23:30 +00:00
if _, ok := data.Content["date"]; ok {
data.Content["date"] = data.Content["date"].(string) + ":00"
}
2015-09-26 21:02:49 +00:00
// Schedule the post
2016-03-12 09:25:15 +00:00
if data.Schedule {
2016-03-12 10:23:30 +00:00
t := cast.ToTime(data.Content["date"])
2015-09-26 21:02:49 +00:00
scheduler := cron.New()
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
// Set draft to false
2016-03-12 09:25:15 +00:00
data.Content["draft"] = false
2015-09-26 21:02:49 +00:00
// Converts the frontmatter in JSON
2016-03-12 09:25:15 +00:00
jsonFrontmatter, err := json.Marshal(data.Content)
2015-09-26 21:02:49 +00:00
if err != nil {
return
}
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file
f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes())
f.Write([]byte(mainContent))
file := f.Bytes()
// Write the file
2016-02-11 20:52:56 +00:00
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
2015-09-26 21:02:49 +00:00
return
}
2016-06-16 16:01:41 +00:00
go hugo.Run(c, false)
2015-09-26 21:02:49 +00:00
})
scheduler.Start()
}
// Converts the frontmatter in JSON
2016-03-12 09:25:15 +00:00
jsonFrontmatter, err := json.Marshal(data.Content)
2015-09-26 21:02:49 +00:00
if err != nil {
2015-09-26 21:19:22 +00:00
return []byte{}, http.StatusInternalServerError, err
2015-09-26 21:02:49 +00:00
}
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file
f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes())
f.Write([]byte(mainContent))
2015-09-26 21:19:22 +00:00
return f.Bytes(), http.StatusOK, nil
2015-09-26 21:02:49 +00:00
}