filebrowser/directory/update.go

140 lines
3.3 KiB
Go
Raw Normal View History

2016-06-28 09:24:02 +00:00
package directory
2016-06-27 12:22:07 +00:00
import (
2016-06-27 17:57:54 +00:00
"bytes"
"encoding/json"
"errors"
2016-06-29 09:16:10 +00:00
"fmt"
2016-06-27 17:57:54 +00:00
"io/ioutil"
2016-06-27 12:22:07 +00:00
"net/http"
2016-06-27 17:57:54 +00:00
"path/filepath"
"strings"
2016-06-27 12:22:07 +00:00
2016-06-28 09:24:02 +00:00
"github.com/hacdias/caddy-filemanager/config"
2016-06-27 17:57:54 +00:00
"github.com/spf13/hugo/parser"
2016-06-27 12:22:07 +00:00
)
2016-06-27 17:57:54 +00:00
// Update is used to update a file that was edited
2016-06-27 12:22:07 +00:00
func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-06-27 17:57:54 +00:00
var data map[string]interface{}
kind := r.Header.Get("kind")
2016-06-27 12:22:07 +00:00
2016-06-27 17:57:54 +00:00
if kind == "" {
return http.StatusBadRequest, nil
}
// Get the JSON information
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
if err != nil {
return http.StatusInternalServerError, err
}
var file []byte
var code int
switch kind {
case "frontmatter-only":
2016-06-30 19:12:43 +00:00
if file, code, err = ParseFrontMatterOnlyFile(data, i.Name); err != nil {
2016-06-27 17:57:54 +00:00
return http.StatusInternalServerError, err
}
case "content-only":
mainContent := data["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
2016-06-30 19:12:43 +00:00
if file, code, err = ParseCompleteFile(data, i.Name, c.FrontMatter); err != nil {
2016-06-27 17:57:54 +00:00
return http.StatusInternalServerError, err
}
default:
return http.StatusBadRequest, nil
}
// Write the file
err = ioutil.WriteFile(i.Path, file, 0666)
if err != nil {
return http.StatusInternalServerError, err
}
return code, nil
}
2016-06-30 19:12:43 +00:00
// ParseFrontMatterOnlyFile parses a frontmatter only file
func ParseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) {
2016-06-27 17:57:54 +00:00
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
2016-06-30 19:12:43 +00:00
f, code, err := ParseFrontMatter(data, frontmatter)
2016-06-29 09:40:20 +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)
return f, code, err
2016-06-29 09:16:10 +00:00
}
2016-06-30 19:12:43 +00:00
// ParseFrontMatter is the frontmatter parser
func ParseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) {
2016-06-27 17:57:54 +00:00
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, mark)
if err != nil {
return []byte{}, http.StatusInternalServerError, err
}
return f, http.StatusOK, nil
}
2016-06-30 19:12:43 +00:00
// ParseCompleteFile parses a complete file
func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, int, error) {
2016-06-29 09:16:10 +00:00
mainContent := ""
if _, ok := data["content"]; ok {
// The main content of the file
mainContent = data["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
2016-06-27 17:57:54 +00:00
2016-06-29 09:16:10 +00:00
// Removes the main content from the rest of the frontmatter
delete(data, "content")
}
2016-06-27 17:57:54 +00:00
if _, ok := data["date"]; ok {
data["date"] = data["date"].(string) + ":00"
}
2016-06-30 19:12:43 +00:00
front, code, err := ParseFrontMatter(data, frontmatter)
2016-06-27 17:57:54 +00:00
if err != nil {
2016-06-29 09:16:10 +00:00
fmt.Println(frontmatter)
return []byte{}, code, err
2016-06-27 17:57:54 +00:00
}
// Generates the final file
f := new(bytes.Buffer)
2016-06-29 09:16:10 +00:00
f.Write(front)
2016-06-27 17:57:54 +00:00
f.Write([]byte(mainContent))
return f.Bytes(), http.StatusOK, nil
2016-06-27 12:22:07 +00:00
}