update file structure [ci skip]

This commit is contained in:
Henrique Dias
2016-03-06 19:18:46 +00:00
parent ddcaccda0c
commit 3f36200862
23 changed files with 180 additions and 165 deletions

30
routes/browse/browse.go Normal file
View File

@@ -0,0 +1,30 @@
package browse
import (
"errors"
"net/http"
"strings"
"github.com/hacdias/caddy-hugo/config"
)
// ServeHTTP is used to serve the content of Browse page using Browse middleware
// from Caddy. It handles the requests for DELETE, POST, GET and PUT related to
// /browse interface.
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Removes the page main path from the URL
r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
switch r.Method {
case "DELETE":
return DELETE(w, r, c)
case "POST":
return POST(w, r, c)
case "GET":
return GET(w, r, c)
case "PUT":
return PUT(w, r, c)
default:
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

48
routes/browse/delete.go Normal file
View File

@@ -0,0 +1,48 @@
package browse
import (
"net/http"
"os"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// DELETE handles the delete requests on browse pages
func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Remove both beginning and trailing slashes
path := r.URL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
path = c.Path + path
message := "File deleted."
// Check if the file or directory exists
if stat, err := os.Stat(path); err == nil {
var err error
// If it's dir, remove all of the content inside
if stat.IsDir() {
err = os.RemoveAll(path)
message = "Folder deleted."
} else {
err = os.Remove(path)
}
// Check for errors
if err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, nil)
}
} else {
return utils.RespondJSON(w, map[string]string{
"message": "File not found.",
}, 404, nil)
}
return utils.RespondJSON(w, map[string]string{
"message": message,
}, 200, nil)
}

44
routes/browse/get.go Normal file
View File

@@ -0,0 +1,44 @@
package browse
import (
"net/http"
"text/template"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/templates"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
// GET handles the GET method on browse page and shows the files listing Using
// the Browse Caddy middleware.
func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
functions := template.FuncMap{
"CanBeEdited": templates.CanBeEdited,
"Defined": templates.Defined,
}
tpl, err := templates.Get(r, functions, "browse")
if err != nil {
return http.StatusInternalServerError, err
}
// Using Caddy's Browse middleware
b := browse.Browse{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return 404, nil
}),
Root: c.Path,
Configs: []browse.Config{
{
PathScope: "/",
Variables: c,
Template: tpl,
},
},
IgnoreIndexes: true,
}
return b.ServeHTTP(w, r)
}

141
routes/browse/post.go Normal file
View File

@@ -0,0 +1,141 @@
package browse
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/commands"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// 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, c *config.Config) (int, error) {
// Remove prefix slash
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
// If it's the upload of a file
if r.Header.Get("X-Upload") == "true" {
return upload(w, r, c)
}
// Get the JSON information sent using a buffer
buffer := new(bytes.Buffer)
buffer.ReadFrom(r.Body)
// Creates the raw file "map" using the JSON
var info map[string]interface{}
json.Unmarshal(buffer.Bytes(), &info)
// Check if filename and archetype are specified in
// the request
if _, ok := info["filename"]; !ok {
return utils.RespondJSON(w, map[string]string{
"message": "Filename not specified.",
}, 500, nil)
}
if _, ok := info["archetype"]; !ok {
return utils.RespondJSON(w, map[string]string{
"message": "Archtype not specified.",
}, 500, nil)
}
// Sanitize the file name path
filename := info["filename"].(string)
filename = strings.TrimPrefix(filename, "/")
filename = strings.TrimSuffix(filename, "/")
url := "/admin/edit/" + r.URL.Path + filename
filename = c.Path + r.URL.Path + filename
if strings.HasPrefix(filename, c.Path+"content/") &&
(strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) {
filename = strings.Replace(filename, c.Path+"content/", "", 1)
args := []string{"new", filename}
archetype := info["archetype"].(string)
if archetype != "" {
args = append(args, "--kind", archetype)
}
if err := commands.Run(c.Hugo, args, c.Path); err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
} else {
var err error
if filepath.Ext(filename) == "" {
err = os.MkdirAll(filename, 0755)
url = strings.Replace(url, "edit", "browse", 1)
} else {
var wf *os.File
wf, err = os.Create(filename)
defer wf.Close()
}
if err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
}
return utils.RespondJSON(w, map[string]string{
"location": url,
"message": "File created.",
}, 200, nil)
}
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
// For each file header in the multipart form
for _, fheaders := range r.MultipartForm.File {
// Handle each file
for _, hdr := range fheaders {
// Open the first file
var infile multipart.File
if infile, err = hdr.Open(); nil != err {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
// Create the file
var outfile *os.File
if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
defer outfile.Close()
}
}
return utils.RespondJSON(w, nil, 200, nil)
}

55
routes/browse/put.go Normal file
View File

@@ -0,0 +1,55 @@
package browse
import (
"bytes"
"encoding/json"
"net/http"
"os"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// 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, c *config.Config) (int, error) {
// Remove both beginning and trailing slashes
old := r.URL.Path
old = strings.TrimPrefix(old, "/")
old = strings.TrimSuffix(old, "/")
old = c.Path + old
// Get the JSON information sent using a buffer
buffer := new(bytes.Buffer)
buffer.ReadFrom(r.Body)
// Creates the raw file "map" using the JSON
var info map[string]interface{}
json.Unmarshal(buffer.Bytes(), &info)
// Check if filename and archetype are specified in
// the request
if _, ok := info["filename"]; !ok {
return utils.RespondJSON(w, map[string]string{
"message": "Filename not specified.",
}, 400, nil)
}
// Sanitize the file name path
new := info["filename"].(string)
new = strings.TrimPrefix(new, "/")
new = strings.TrimSuffix(new, "/")
new = c.Path + new
// Renames the file/folder
if err := os.Rename(old, new); err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Something went wrong.",
}, 500, err)
}
return utils.RespondJSON(w, map[string]string{
"message": "File renamed.",
}, 200, nil)
}

24
routes/editor/editor.go Normal file
View File

@@ -0,0 +1,24 @@
package editor
import (
"errors"
"net/http"
"strings"
"github.com/hacdias/caddy-hugo/config"
)
// ServeHTTP serves the editor page
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
filename = c.Path + filename
switch r.Method {
case "POST":
return POST(w, r, c, filename)
case "GET":
return GET(w, r, c, filename)
default:
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

160
routes/editor/get.go Normal file
View File

@@ -0,0 +1,160 @@
package editor
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/frontmatter"
"github.com/hacdias/caddy-hugo/tools/templates"
"github.com/spf13/hugo/parser"
)
type editor struct {
Name string
Class string
IsPost bool
Mode string
Content string
FrontMatter interface{}
Config *config.Config
}
// GET handles the GET method on editor page
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
// Check if the file format is supported. If not, send a "Not Acceptable"
// header and an error
if !templates.CanBeEdited(filename) {
return http.StatusNotAcceptable, errors.New("File format not supported.")
}
// Check if the file exists.
if _, err := os.Stat(filename); os.IsNotExist(err) {
return http.StatusNotFound, nil
} else if os.IsPermission(err) {
return http.StatusForbidden, nil
} else if err != nil {
return http.StatusInternalServerError, err
}
// Open the file and check if there was some error while opening
file, err := ioutil.ReadFile(filename)
if os.IsPermission(err) {
return http.StatusForbidden, nil
} else if err != nil {
return http.StatusInternalServerError, err
}
// Create a new editor variable and set the extension
page := new(editor)
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
page.Name = strings.Replace(filename, c.Path, "", 1)
page.Config = c
page.IsPost = false
// Sanitize the extension
page.Mode = sanitizeMode(page.Mode)
// Handle the content depending on the file extension
switch page.Mode {
case "markdown", "asciidoc", "rst":
if hasFrontMatterRune(file) {
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(file)
file, err := parser.ReadFrom(buffer)
if err != nil {
return http.StatusInternalServerError, err
}
if strings.Contains(string(file.FrontMatter()), "date") {
page.IsPost = true
}
// Parses the page content and the frontmatter
page.Content = strings.TrimSpace(string(file.Content()))
page.FrontMatter, page.Name, err = frontmatter.Pretty(file.FrontMatter())
page.Class = "complete"
} else {
// The editor will handle only content
page.Class = "content-only"
page.Content = string(file)
}
case "json", "toml", "yaml":
// Defines the class and declares an error
page.Class = "frontmatter-only"
var err error
// Checks if the file already has the frontmatter rune and parses it
if hasFrontMatterRune(file) {
page.FrontMatter, _, err = frontmatter.Pretty(file)
} else {
page.FrontMatter, _, err = frontmatter.Pretty(appendFrontMatterRune(file, page.Mode))
}
// Check if there were any errors
if err != nil {
return http.StatusInternalServerError, err
}
default:
// The editor will handle only content
page.Class = "content-only"
page.Content = string(file)
}
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
functions := template.FuncMap{
"SplitCapitalize": templates.SplitCapitalize,
"Defined": templates.Defined,
}
tpl, err := templates.Get(r, functions, "editor", "frontmatter")
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, tpl.Execute(w, page)
}
func hasFrontMatterRune(file []byte) bool {
return strings.HasPrefix(string(file), "---") ||
strings.HasPrefix(string(file), "+++") ||
strings.HasPrefix(string(file), "{")
}
func appendFrontMatterRune(frontmatter []byte, language string) []byte {
switch language {
case "yaml":
return []byte("---\n" + string(frontmatter) + "\n---")
case "toml":
return []byte("+++\n" + string(frontmatter) + "\n+++")
case "json":
return frontmatter
}
return frontmatter
}
func sanitizeMode(extension string) string {
switch extension {
case "md", "markdown", "mdown", "mmark":
return "markdown"
case "asciidoc", "adoc", "ad":
return "asciidoc"
case "rst":
return "rst"
case "html", "htm":
return "html"
case "js":
return "javascript"
default:
return extension
}
}

168
routes/editor/post.go Normal file
View File

@@ -0,0 +1,168 @@
package editor
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/commands"
"github.com/robfig/cron"
"github.com/spf13/cast"
"github.com/spf13/hugo/parser"
)
// POST handles the POST method on editor page
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
// Get the JSON information sent using a buffer
rawBuffer := new(bytes.Buffer)
rawBuffer.ReadFrom(r.Body)
// Creates the raw file "map" using the JSON
var rawFile map[string]interface{}
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
// Initializes the file content to write
var file []byte
switch r.Header.Get("X-Content-Type") {
case "frontmatter-only":
f, code, err := parseFrontMatterOnlyFile(rawFile, filename)
if err != nil {
return code, err
}
file = f
case "content-only":
// The main content of the file
mainContent := rawFile["content"].(string)
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
case "complete":
f, code, err := parseCompleteFile(r, c, rawFile, filename)
if err != nil {
return code, err
}
file = f
default:
return http.StatusBadRequest, errors.New("X-Content-Type header not defined")
}
// Write the file
err := ioutil.WriteFile(filename, file, 0666)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return http.StatusOK, nil
}
func parseFrontMatterOnlyFile(rawFile map[string]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(rawFile, 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(r *http.Request, c *config.Config, rawFile map[string]interface{}, filename string) ([]byte, int, error) {
// The main content of the file
mainContent := rawFile["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
// Removes the main content from the rest of the frontmatter
delete(rawFile, "content")
// Schedule the post
if r.Header.Get("X-Schedule") == "true" {
t := cast.ToTime(rawFile["date"].(string))
scheduler := cron.New()
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
// Set draft to false
rawFile["draft"] = false
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(rawFile)
if err != nil {
return
}
// 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))
file := f.Bytes()
// Write the file
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
return
}
go commands.Run(c, false)
})
scheduler.Start()
}
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(rawFile)
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
}

18
routes/git/git.go Normal file
View File

@@ -0,0 +1,18 @@
package git
import (
"errors"
"net/http"
"github.com/hacdias/caddy-hugo/config"
)
// ServeHTTP is used to serve the content of GIT API.
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
switch r.Method {
case "POST":
return POST(w, r, c)
default:
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

64
routes/git/post.go Normal file
View File

@@ -0,0 +1,64 @@
package git
import (
"bytes"
"encoding/json"
"net/http"
"os/exec"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/utils"
)
// POST handles the POST method on GIT page which is only an API.
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Check if git is installed on the computer
if _, err := exec.LookPath("git"); err != nil {
return utils.RespondJSON(w, map[string]string{
"message": "Git is not installed on your computer.",
}, 400, nil)
}
// Get the JSON information sent using a buffer
buff := new(bytes.Buffer)
buff.ReadFrom(r.Body)
// Creates the raw file "map" using the JSON
var info map[string]interface{}
json.Unmarshal(buff.Bytes(), &info)
// Check if command was sent
if _, ok := info["command"]; !ok {
return utils.RespondJSON(w, map[string]string{
"message": "Command not specified.",
}, 400, nil)
}
command := info["command"].(string)
args := strings.Split(command, " ")
if len(args) > 0 && args[0] == "git" {
args = append(args[:0], args[1:]...)
}
if len(args) == 0 {
return utils.RespondJSON(w, map[string]string{
"message": "Command not specified.",
}, 400, nil)
}
cmd := exec.Command("git", args...)
cmd.Dir = c.Path
output, err := cmd.CombinedOutput()
if err != nil {
return utils.RespondJSON(w, map[string]string{
"message": err.Error(),
}, 500, err)
}
return utils.RespondJSON(w, map[string]string{
"message": string(output),
}, 200, nil)
}