2015-09-26 21:02:49 +00:00
|
|
|
package editor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-10-12 21:03:26 +00:00
|
|
|
"errors"
|
2015-09-26 21:02:49 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-10-18 14:10:32 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/config"
|
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// POST handles the POST method on editor page
|
|
|
|
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
|
|
|
// Get the JSON information sent using a buffer
|
|
|
|
rawBuffer := new(bytes.Buffer)
|
|
|
|
rawBuffer.ReadFrom(r.Body)
|
|
|
|
|
|
|
|
// Creates the raw file "map" using the JSON
|
|
|
|
var rawFile map[string]interface{}
|
|
|
|
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
|
|
|
|
|
|
|
|
// Initializes the file content to write
|
|
|
|
var file []byte
|
|
|
|
|
|
|
|
switch r.Header.Get("X-Content-Type") {
|
|
|
|
case "frontmatter-only":
|
|
|
|
f, code, err := parseFrontMatterOnlyFile(rawFile, filename)
|
|
|
|
if err != nil {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file = f
|
|
|
|
case "content-only":
|
|
|
|
// The main content of the file
|
|
|
|
mainContent := rawFile["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":
|
|
|
|
f, code, err := parseCompleteFile(r, c, rawFile, filename)
|
|
|
|
if err != nil {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file = f
|
|
|
|
default:
|
2015-10-12 21:03:26 +00:00
|
|
|
return http.StatusBadRequest, errors.New("X-Content-Type header not defined")
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the file
|
|
|
|
err := ioutil.WriteFile(filename, file, 0666)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte("{}"))
|
2015-09-26 21:19:22 +00:00
|
|
|
return http.StatusOK, nil
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ([]byte, int, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
f, err := parser.InterfaceToFrontMatter(rawFile, mark)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]interface{}, filename string) ([]byte, int, error) {
|
|
|
|
// The main content of the file
|
|
|
|
mainContent := rawFile["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
|
|
|
|
delete(rawFile, "content")
|
|
|
|
|
|
|
|
// Schedule the post
|
|
|
|
if r.Header.Get("X-Schedule") == "true" {
|
2016-02-08 19:14:27 +00:00
|
|
|
t := cast.ToTime(rawFile["date"].(string))
|
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
|
|
|
|
rawFile["draft"] = false
|
|
|
|
|
|
|
|
// Converts the frontmatter in JSON
|
|
|
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
|
|
|
|
|
|
|
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-02-20 12:04:12 +00:00
|
|
|
go utils.Run(c, false)
|
2015-09-26 21:02:49 +00:00
|
|
|
})
|
|
|
|
scheduler.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the frontmatter in JSON
|
|
|
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
|
|
|
|
|
|
|
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
|
|
|
}
|