mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-08 19:22:57 +00:00

Former-commit-id: 6f9843613e6abfe0b19e6e43f9299afb98645477 [formerly 805fa39b073401c946b559e0e967fdc553f16295] [formerly f9e2de337abc0cbeb5ccdb8b6962871b0bd3ee0e [formerly 1d26b8e95e73a94ba92673861c4e83dfded0d92e]] Former-commit-id: 259a1ba8cdc71e7d32add58f9487e5827aa6685e [formerly 161a1a49bc1158f11f07fc9ff638506dafb6c918] Former-commit-id: 67fe1fef4602baf4ddae40b28e65ac9a2a42ca9d
139 lines
2.9 KiB
Go
139 lines
2.9 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/hacdias/filemanager/frontmatter"
|
|
)
|
|
|
|
// put is used to update a file that was edited
|
|
func put(ctx *requestContext, w http.ResponseWriter, r *http.Request) (err error) {
|
|
var (
|
|
data = map[string]interface{}{}
|
|
file []byte
|
|
kind string
|
|
rawBuffer = new(bytes.Buffer)
|
|
)
|
|
|
|
kind = r.Header.Get("kind")
|
|
rawBuffer.ReadFrom(r.Body)
|
|
|
|
if kind != "" {
|
|
err = json.Unmarshal(rawBuffer.Bytes(), &data)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
switch kind {
|
|
case "frontmatter-only":
|
|
if file, err = parseFrontMatterOnlyFile(data, r.URL.Path); err != nil {
|
|
return
|
|
}
|
|
case "content-only":
|
|
mainContent := data["content"].(string)
|
|
mainContent = strings.TrimSpace(mainContent)
|
|
file = []byte(mainContent)
|
|
case "complete":
|
|
var mark rune
|
|
|
|
if v := r.Header.Get("Rune"); v != "" {
|
|
var n int
|
|
n, err = strconv.Atoi(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
mark = rune(n)
|
|
}
|
|
|
|
if file, err = parseCompleteFile(data, r.URL.Path, mark); err != nil {
|
|
return
|
|
}
|
|
default:
|
|
file = rawBuffer.Bytes()
|
|
}
|
|
|
|
// Overwrite the request Body
|
|
r.Body = ioutil.NopCloser(bytes.NewReader(file))
|
|
return
|
|
}
|
|
|
|
// parseFrontMatterOnlyFile parses a frontmatter only file
|
|
func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, error) {
|
|
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
|
f, err := parseFrontMatter(data, frontmatter)
|
|
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, err
|
|
}
|
|
|
|
// parseFrontMatter is the frontmatter parser
|
|
func parseFrontMatter(data interface{}, front string) ([]byte, error) {
|
|
var mark rune
|
|
|
|
switch front {
|
|
case "toml":
|
|
mark = '+'
|
|
case "json":
|
|
mark = '{'
|
|
case "yaml":
|
|
mark = '-'
|
|
default:
|
|
return nil, errors.New("Unsupported Format provided")
|
|
}
|
|
|
|
return frontmatter.Marshal(data, mark)
|
|
}
|
|
|
|
// parseCompleteFile parses a complete file
|
|
func parseCompleteFile(data map[string]interface{}, filename string, mark rune) ([]byte, error) {
|
|
mainContent := ""
|
|
|
|
if _, ok := data["content"]; ok {
|
|
// The main content of the file
|
|
mainContent = data["content"].(string)
|
|
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
|
|
|
|
// Removes the main content from the rest of the frontmatter
|
|
delete(data, "content")
|
|
}
|
|
|
|
if _, ok := data["date"]; ok {
|
|
data["date"] = data["date"].(string) + ":00"
|
|
}
|
|
|
|
front, err := frontmatter.Marshal(data, mark)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
|
|
front = frontmatter.AppendRune(front, mark)
|
|
|
|
// Generates the final file
|
|
f := new(bytes.Buffer)
|
|
f.Write(front)
|
|
f.Write([]byte(mainContent))
|
|
return f.Bytes(), nil
|
|
}
|