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)
}