close #71; fix bugs

This commit is contained in:
Henrique Dias
2016-06-07 18:15:55 +01:00
parent acc4c92b15
commit 4cd72ca58d
11 changed files with 38 additions and 32 deletions

View File

@@ -21,13 +21,13 @@ type response struct {
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
conf = c
// Removes the page main path from the URL
r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
r.URL.Path = strings.Replace(r.URL.Path, c.Admin+"/browse", "", 1)
switch r.Method {
case "DELETE":
return DELETE(w, r)
case "POST":
return POST(w, r)
return POST(w, r, c)
case "GET":
return GET(w, r)
case "PUT":

View File

@@ -10,13 +10,14 @@ import (
"path/filepath"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/commands"
s "github.com/hacdias/caddy-hugo/tools/server"
)
// POST handles the POST method on browse page. It's used to create new files,
// folders and upload content.
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Remove prefix slash
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
@@ -47,7 +48,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
filename := info["filename"].(string)
filename = strings.TrimPrefix(filename, "/")
filename = strings.TrimSuffix(filename, "/")
url := "/admin/edit/" + r.URL.Path + filename
url := c.Admin + "/edit/" + r.URL.Path + filename
filename = conf.Path + r.URL.Path + filename
if strings.HasPrefix(filename, conf.Path+"content/") &&

View File

@@ -10,7 +10,7 @@ import (
"github.com/hacdias/caddy-hugo/tools/server"
)
// PUT handles the HTTP PUT request for all /admin/browse related requests.
// PUT handles the HTTP PUT request for all /{admin}/browse related requests.
// Renames a file and/or a folder.
func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
// Remove both beginning and trailing slashes

View File

@@ -16,7 +16,7 @@ var (
// ServeHTTP serves the editor page
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
conf = c
filename = strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
filename = strings.Replace(r.URL.Path, c.Admin+"/edit/", "", 1)
filename = c.Path + filename
switch r.Method {

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
@@ -17,8 +18,6 @@ import (
"github.com/spf13/hugo/parser"
)
var data info
type info struct {
ContentType string
Schedule bool
@@ -32,11 +31,15 @@ type response struct {
// POST handles the POST method on editor page
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
var data info
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
err := json.Unmarshal(rawBuffer.Bytes(), &data)
fmt.Println(string(rawBuffer.Bytes()))
if err != nil {
return server.RespondJSON(w, &response{"Error decrypting json."}, http.StatusInternalServerError, err)
}
@@ -46,7 +49,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
switch data.ContentType {
case "frontmatter-only":
f, code, err := parseFrontMatterOnlyFile()
f, code, err := parseFrontMatterOnlyFile(data)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
@@ -59,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
file = []byte(mainContent)
case "complete":
f, code, err := parseCompleteFile()
f, code, err := parseCompleteFile(data)
if err != nil {
return server.RespondJSON(w, &response{err.Error()}, code, err)
}
@@ -83,7 +86,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
return server.RespondJSON(w, nil, http.StatusOK, nil)
}
func parseFrontMatterOnlyFile() ([]byte, int, error) {
func parseFrontMatterOnlyFile(data info) ([]byte, int, error) {
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
var mark rune
@@ -121,7 +124,7 @@ func parseFrontMatterOnlyFile() ([]byte, int, error) {
return f, http.StatusOK, nil
}
func parseCompleteFile() ([]byte, int, error) {
func parseCompleteFile(data info) ([]byte, int, error) {
// The main content of the file
mainContent := data.Content["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"