update
This commit is contained in:
203
post.go
Normal file
203
post.go
Normal file
@@ -0,0 +1,203 @@
|
||||
package hugo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/hugo/parser"
|
||||
)
|
||||
|
||||
type info struct {
|
||||
ContentType string
|
||||
Schedule bool
|
||||
Regenerate bool
|
||||
Content map[string]interface{}
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// POST handles the POST method on editor page
|
||||
func (h Hugo) POST(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
|
||||
var data info
|
||||
|
||||
// Get the JSON information sent using a buffer
|
||||
rawBuffer := new(bytes.Buffer)
|
||||
rawBuffer.ReadFrom(r.Body)
|
||||
err := json.Unmarshal(rawBuffer.Bytes(), &data)
|
||||
|
||||
fmt.Println(string(rawBuffer.Bytes()))
|
||||
|
||||
if err != nil {
|
||||
return RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
// Initializes the file content to write
|
||||
var file []byte
|
||||
var code int
|
||||
|
||||
switch data.ContentType {
|
||||
case "frontmatter-only":
|
||||
file, code, err = parseFrontMatterOnlyFile(data, filename)
|
||||
if err != nil {
|
||||
return RespondJSON(w, &response{err.Error()}, code, err)
|
||||
}
|
||||
case "content-only":
|
||||
// The main content of the file
|
||||
mainContent := data.Content["content"].(string)
|
||||
mainContent = strings.TrimSpace(mainContent)
|
||||
|
||||
file = []byte(mainContent)
|
||||
case "complete":
|
||||
file, code, err = parseCompleteFile(data, filename, h.Config)
|
||||
if err != nil {
|
||||
return RespondJSON(w, &response{err.Error()}, code, err)
|
||||
}
|
||||
default:
|
||||
return RespondJSON(w, &response{"Invalid content type."}, http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
// Write the file
|
||||
err = ioutil.WriteFile(filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
return RespondJSON(w, &response{err.Error()}, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
if data.Regenerate {
|
||||
go RunHugo(h.Config, false)
|
||||
}
|
||||
|
||||
return RespondJSON(w, nil, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
func parseFrontMatterOnlyFile(data info, 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:
|
||||
return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
|
||||
}
|
||||
|
||||
f, err := parser.InterfaceToFrontMatter(data.Content, 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 {
|
||||
return []byte{}, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return f, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func parseCompleteFile(data info, filename string, c *Config) ([]byte, int, error) {
|
||||
// The main content of the file
|
||||
mainContent := data.Content["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
|
||||
|
||||
// Removes the main content from the rest of the frontmatter
|
||||
delete(data.Content, "content")
|
||||
|
||||
if _, ok := data.Content["date"]; ok {
|
||||
data.Content["date"] = data.Content["date"].(string) + ":00"
|
||||
}
|
||||
|
||||
// Schedule the post
|
||||
if data.Schedule {
|
||||
t := cast.ToTime(data.Content["date"])
|
||||
|
||||
scheduler := cron.New()
|
||||
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
|
||||
// Set draft to false
|
||||
data.Content["draft"] = false
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(data.Content)
|
||||
|
||||
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
|
||||
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
go RunHugo(c, false)
|
||||
})
|
||||
scheduler.Start()
|
||||
}
|
||||
|
||||
// Converts the frontmatter in JSON
|
||||
jsonFrontmatter, err := json.Marshal(data.Content)
|
||||
|
||||
if err != nil {
|
||||
return []byte{}, http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// 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))
|
||||
return f.Bytes(), http.StatusOK, nil
|
||||
}
|
||||
|
||||
func RespondJSON(w http.ResponseWriter, message interface{}, code int, err error) (int, error) {
|
||||
if message == nil {
|
||||
message = map[string]string{}
|
||||
}
|
||||
|
||||
msg, msgErr := json.Marshal(message)
|
||||
|
||||
if msgErr != nil {
|
||||
return 500, msgErr
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(msg)
|
||||
return 0, err
|
||||
}
|
||||
Reference in New Issue
Block a user