filebrowser/handlers/put.go

144 lines
3.0 KiB
Go
Raw Normal View History

2016-10-22 11:00:45 +00:00
package handlers
2016-06-27 12:22:07 +00:00
import (
2016-06-27 17:57:54 +00:00
"bytes"
"encoding/json"
"errors"
"io/ioutil"
2016-06-27 12:22:07 +00:00
"net/http"
2016-06-27 17:57:54 +00:00
"path/filepath"
2017-01-18 18:20:56 +00:00
"strconv"
2016-06-27 17:57:54 +00:00
"strings"
2016-06-27 12:22:07 +00:00
2016-06-28 09:24:02 +00:00
"github.com/hacdias/caddy-filemanager/config"
2017-01-16 20:03:44 +00:00
"github.com/hacdias/caddy-filemanager/frontmatter"
2016-06-27 12:22:07 +00:00
)
2016-10-22 11:00:45 +00:00
// PreProccessPUT is used to update a file that was edited
func PreProccessPUT(
2016-10-22 10:47:49 +00:00
w http.ResponseWriter,
r *http.Request,
c *config.Config,
u *config.User,
2016-10-22 11:00:45 +00:00
) (err error) {
2016-10-18 16:56:35 +00:00
var (
2016-11-14 20:09:30 +00:00
data = map[string]interface{}{}
2017-01-03 17:14:28 +00:00
file []byte
2016-10-18 16:56:35 +00:00
kind string
rawBuffer = new(bytes.Buffer)
)
kind = r.Header.Get("kind")
2016-06-27 17:57:54 +00:00
rawBuffer.ReadFrom(r.Body)
2016-10-18 16:56:35 +00:00
if kind != "" {
err = json.Unmarshal(rawBuffer.Bytes(), &data)
2016-06-27 17:57:54 +00:00
2016-10-18 15:17:01 +00:00
if err != nil {
2016-10-22 11:00:45 +00:00
return
2016-06-27 17:57:54 +00:00
}
2016-10-18 16:56:35 +00:00
}
2016-10-18 15:17:01 +00:00
2016-10-18 16:56:35 +00:00
switch kind {
case "frontmatter-only":
2016-11-15 13:38:55 +00:00
if file, err = ParseFrontMatterOnlyFile(data, r.URL.Path); err != nil {
2016-10-22 11:00:45 +00:00
return
2016-06-27 17:57:54 +00:00
}
2016-10-18 16:56:35 +00:00
case "content-only":
mainContent := data["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
2017-01-18 18:20:56 +00:00
var mark rune
if v := r.Header.Get("Rune"); v != "" {
n, err := strconv.Atoi(v)
if err != nil {
return err
}
mark = rune(n)
}
if file, err = ParseCompleteFile(data, r.URL.Path, mark); err != nil {
2016-10-22 11:00:45 +00:00
return
2016-10-18 16:56:35 +00:00
}
default:
file = rawBuffer.Bytes()
2016-06-27 17:57:54 +00:00
}
2016-10-18 16:56:35 +00:00
// Overwrite the request Body
2016-10-18 15:17:01 +00:00
r.Body = ioutil.NopCloser(bytes.NewReader(file))
2016-10-22 11:00:45 +00:00
return
2016-06-27 17:57:54 +00:00
}
2016-11-11 20:59:37 +00:00
// ParseFrontMatterOnlyFile parses a frontmatter only file
func ParseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, error) {
2016-06-27 17:57:54 +00:00
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
2016-11-11 20:59:37 +00:00
f, 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)
2016-10-22 11:00:45 +00:00
return f, err
2016-06-29 09:16:10 +00:00
}
2016-11-11 20:59:37 +00:00
// ParseFrontMatter is the frontmatter parser
2017-01-16 20:03:44 +00:00
func ParseFrontMatter(data interface{}, front string) ([]byte, error) {
2016-06-27 17:57:54 +00:00
var mark rune
2017-01-16 20:03:44 +00:00
switch front {
2016-06-27 17:57:54 +00:00
case "toml":
2017-01-16 20:03:44 +00:00
mark = '+'
2016-06-27 17:57:54 +00:00
case "json":
2017-01-16 20:03:44 +00:00
mark = '{'
2016-06-27 17:57:54 +00:00
case "yaml":
2017-01-16 20:03:44 +00:00
mark = '-'
2016-06-27 17:57:54 +00:00
default:
2017-01-16 20:03:44 +00:00
return nil, errors.New("Unsupported Format provided")
2016-06-27 17:57:54 +00:00
}
2017-01-16 20:03:44 +00:00
return frontmatter.Marshal(data, mark)
2016-06-27 17:57:54 +00:00
}
2016-11-11 20:59:37 +00:00
// ParseCompleteFile parses a complete file
2017-01-18 18:20:56 +00:00
func ParseCompleteFile(data map[string]interface{}, filename string, mark rune) ([]byte, 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"
}
2017-01-18 18:20:56 +00:00
front, err := frontmatter.Marshal(data, mark)
2016-06-27 17:57:54 +00:00
if err != nil {
2016-10-22 11:00:45 +00:00
return []byte{}, err
2016-06-27 17:57:54 +00:00
}
2017-01-18 18:20:56 +00:00
front = frontmatter.AppendRune(front, mark)
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))
2016-10-22 11:00:45 +00:00
return f.Bytes(), nil
2016-06-27 12:22:07 +00:00
}