This commit is contained in:
Henrique Dias
2016-06-27 18:57:54 +01:00
parent 1c55147faa
commit 429b67f661
13 changed files with 568 additions and 215 deletions

View File

@@ -2,6 +2,7 @@
// sources:
// assets/public/css/styles.css
// assets/public/js/application.js
// assets/public/js/form-to-json.js
// assets/templates/actions.tmpl
// assets/templates/base.tmpl
// assets/templates/editor.tmpl
@@ -70,6 +71,24 @@ func publicJsApplicationJs() (*asset, error) {
return a, err
}
// publicJsFormToJsonJs reads file data from disk. It returns an error on failure.
func publicJsFormToJsonJs() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\public\\js\\form-to-json.js"
name := "public/js/form-to-json.js"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// templatesActionsTmpl reads file data from disk. It returns an error on failure.
func templatesActionsTmpl() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\actions.tmpl"
@@ -232,6 +251,7 @@ func AssetNames() []string {
var _bindata = map[string]func() (*asset, error){
"public/css/styles.css": publicCssStylesCss,
"public/js/application.js": publicJsApplicationJs,
"public/js/form-to-json.js": publicJsFormToJsonJs,
"templates/actions.tmpl": templatesActionsTmpl,
"templates/base.tmpl": templatesBaseTmpl,
"templates/editor.tmpl": templatesEditorTmpl,
@@ -286,6 +306,7 @@ var _bintree = &bintree{nil, map[string]*bintree{
}},
"js": &bintree{nil, map[string]*bintree{
"application.js": &bintree{publicJsApplicationJs, map[string]*bintree{}},
"form-to-json.js": &bintree{publicJsFormToJsonJs, map[string]*bintree{}},
}},
}},
"templates": &bintree{nil, map[string]*bintree{

View File

@@ -78,7 +78,6 @@ func (i *Info) GetEditor() (*Editor, error) {
editor.Class = "content-only"
editor.Content = i.Content
}
return editor, nil
}

View File

@@ -146,7 +146,7 @@ func handleObjects(content interface{}, parent *Block, name string) *Block {
} else if parent.Type == arrayType {
c.Name = parent.Name + "[]"
} else {
c.Name = parent.Name + "[" + c.Title + "]"
c.Name = parent.Name + "." + c.Title
}
c.Content = rawToPretty(content, c)
@@ -162,7 +162,7 @@ func handleArrays(content interface{}, parent *Block, name string) *Block {
if parent.Name == mainName {
c.Name = name
} else {
c.Name = parent.Name + "[" + name + "]"
c.Name = parent.Name + "." + name
}
c.Content = rawToPretty(content, c)
@@ -199,7 +199,7 @@ func handleFlatValues(content interface{}, parent *Block, name string) *Block {
c.Title = content.(string)
} else if parent.Type == objectType {
c.Title = name
c.Name = parent.Name + "[" + name + "]"
c.Name = parent.Name + "." + name
if parent.Name == mainName {
c.Name = name

View File

@@ -1,68 +1,134 @@
package file
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"github.com/hacdias/caddy-filemanager/internal/config"
"github.com/spf13/hugo/parser"
)
// Update is
// Update is used to update a file that was edited
func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
var data map[string]interface{}
kind := r.Header.Get("kind")
/*
// POST handles the POST method on editor page
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
var data info
// TODO: remove
fmt.Println(i.Name)
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
if kind == "" {
return http.StatusBadRequest, nil
}
fmt.Println(string(rawBuffer.Bytes()))
// Get the JSON information
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
if err != nil {
return server.RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
}
if err != nil {
return http.StatusInternalServerError, err
}
// Initializes the file content to write
var file []byte
var code int
var file []byte
var code int
switch data.ContentType {
case "frontmatter-only":
file, code, err = parseFrontMatterOnlyFile(data, filename)
if err != nil {
return server.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)
switch kind {
case "frontmatter-only":
if file, code, err = parseFrontMatterOnlyFile(data, i.Name); err != nil {
return http.StatusInternalServerError, err
}
case "content-only":
mainContent := data["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
if file, code, err = parseCompleteFile(data, i.Name); err != nil {
return http.StatusInternalServerError, err
}
default:
return http.StatusBadRequest, nil
}
file = []byte(mainContent)
case "complete":
file, code, err = parseCompleteFile(data, filename, c)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
default:
return server.RespondJSON(w, &response{"Invalid content type."}, http.StatusBadRequest, nil)
}
// Write the file
err = ioutil.WriteFile(i.Path, file, 0666)
// Write the file
err = ioutil.WriteFile(filename, file, 0666)
if err != nil {
return http.StatusInternalServerError, err
}
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, http.StatusInternalServerError, err)
}
if data.Regenerate {
go hugo.Run(c, false)
}
return server.RespondJSON(w, nil, http.StatusOK, nil)
}
*/
return 0, nil
return code, nil
}
func parseFrontMatterOnlyFile(data interface{}, 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, 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 map[string]interface{}, filename string) ([]byte, int, error) {
// 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"
}
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(data)
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
}