move put to put
parent
ae33825182
commit
df888b604a
|
@ -1 +0,0 @@
|
||||||
package file
|
|
|
@ -83,8 +83,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
||||||
|
|
||||||
// Preprocess the PUT request if it's the case
|
// Preprocess the PUT request if it's the case
|
||||||
if r.Method == http.MethodPut {
|
if r.Method == http.MethodPut {
|
||||||
_, err = processPUT(w, r, c, user, fi)
|
if handlers.PreProccessPUT(w, r, c, user, fi) != nil {
|
||||||
if err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package filemanager
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -15,19 +15,17 @@ import (
|
||||||
"github.com/spf13/hugo/parser"
|
"github.com/spf13/hugo/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
// processPUT is used to update a file that was edited
|
// PreProccessPUT is used to update a file that was edited
|
||||||
func processPUT(
|
func PreProccessPUT(
|
||||||
w http.ResponseWriter,
|
w http.ResponseWriter,
|
||||||
r *http.Request,
|
r *http.Request,
|
||||||
c *config.Config,
|
c *config.Config,
|
||||||
u *config.User,
|
u *config.User,
|
||||||
i *file.Info,
|
i *file.Info,
|
||||||
) (int, error) {
|
) (err error) {
|
||||||
var (
|
var (
|
||||||
data map[string]interface{}
|
data map[string]interface{}
|
||||||
file []byte
|
file []byte
|
||||||
code int
|
|
||||||
err error
|
|
||||||
kind string
|
kind string
|
||||||
rawBuffer = new(bytes.Buffer)
|
rawBuffer = new(bytes.Buffer)
|
||||||
)
|
)
|
||||||
|
@ -39,22 +37,22 @@ func processPUT(
|
||||||
err = json.Unmarshal(rawBuffer.Bytes(), &data)
|
err = json.Unmarshal(rawBuffer.Bytes(), &data)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch kind {
|
switch kind {
|
||||||
case "frontmatter-only":
|
case "frontmatter-only":
|
||||||
if file, code, err = parseFrontMatterOnlyFile(data, i.Name()); err != nil {
|
if file, err = parseFrontMatterOnlyFile(data, i.Name()); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return
|
||||||
}
|
}
|
||||||
case "content-only":
|
case "content-only":
|
||||||
mainContent := data["content"].(string)
|
mainContent := data["content"].(string)
|
||||||
mainContent = strings.TrimSpace(mainContent)
|
mainContent = strings.TrimSpace(mainContent)
|
||||||
file = []byte(mainContent)
|
file = []byte(mainContent)
|
||||||
case "complete":
|
case "complete":
|
||||||
if file, code, err = parseCompleteFile(data, i.Name(), u.FrontMatter); err != nil {
|
if file, err = parseCompleteFile(data, i.Name(), u.FrontMatter); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
file = rawBuffer.Bytes()
|
file = rawBuffer.Bytes()
|
||||||
|
@ -62,13 +60,13 @@ func processPUT(
|
||||||
|
|
||||||
// Overwrite the request Body
|
// Overwrite the request Body
|
||||||
r.Body = ioutil.NopCloser(bytes.NewReader(file))
|
r.Body = ioutil.NopCloser(bytes.NewReader(file))
|
||||||
return code, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseFrontMatterOnlyFile parses a frontmatter only file
|
// parseFrontMatterOnlyFile parses a frontmatter only file
|
||||||
func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) {
|
func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, error) {
|
||||||
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||||
f, code, err := parseFrontMatter(data, frontmatter)
|
f, err := parseFrontMatter(data, frontmatter)
|
||||||
fString := string(f)
|
fString := string(f)
|
||||||
|
|
||||||
// If it's toml or yaml, strip frontmatter identifier
|
// If it's toml or yaml, strip frontmatter identifier
|
||||||
|
@ -83,11 +81,11 @@ func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, e
|
||||||
}
|
}
|
||||||
|
|
||||||
f = []byte(fString)
|
f = []byte(fString)
|
||||||
return f, code, err
|
return f, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseFrontMatter is the frontmatter parser
|
// parseFrontMatter is the frontmatter parser
|
||||||
func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) {
|
func parseFrontMatter(data interface{}, frontmatter string) ([]byte, error) {
|
||||||
var mark rune
|
var mark rune
|
||||||
|
|
||||||
switch frontmatter {
|
switch frontmatter {
|
||||||
|
@ -98,20 +96,20 @@ func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error)
|
||||||
case "yaml":
|
case "yaml":
|
||||||
mark = rune('-')
|
mark = rune('-')
|
||||||
default:
|
default:
|
||||||
return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.")
|
return []byte{}, errors.New("Can't define the frontmatter.")
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := parser.InterfaceToFrontMatter(data, mark)
|
f, err := parser.InterfaceToFrontMatter(data, mark)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, http.StatusInternalServerError, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return f, http.StatusOK, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseCompleteFile parses a complete file
|
// parseCompleteFile parses a complete file
|
||||||
func parseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, int, error) {
|
func parseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, error) {
|
||||||
mainContent := ""
|
mainContent := ""
|
||||||
|
|
||||||
if _, ok := data["content"]; ok {
|
if _, ok := data["content"]; ok {
|
||||||
|
@ -127,16 +125,16 @@ func parseCompleteFile(data map[string]interface{}, filename string, frontmatter
|
||||||
data["date"] = data["date"].(string) + ":00"
|
data["date"] = data["date"].(string) + ":00"
|
||||||
}
|
}
|
||||||
|
|
||||||
front, code, err := parseFrontMatter(data, frontmatter)
|
front, err := parseFrontMatter(data, frontmatter)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(frontmatter)
|
fmt.Println(frontmatter)
|
||||||
return []byte{}, code, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates the final file
|
// Generates the final file
|
||||||
f := new(bytes.Buffer)
|
f := new(bytes.Buffer)
|
||||||
f.Write(front)
|
f.Write(front)
|
||||||
f.Write([]byte(mainContent))
|
f.Write([]byte(mainContent))
|
||||||
return f.Bytes(), http.StatusOK, nil
|
return f.Bytes(), nil
|
||||||
}
|
}
|
Loading…
Reference in New Issue