2015-09-26 21:02:49 +00:00
|
|
|
package browse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-02-25 20:02:06 +00:00
|
|
|
"path/filepath"
|
2015-09-26 21:02:49 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-03-06 19:18:46 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/tools/commands"
|
2016-03-06 20:37:49 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/tools/server"
|
2015-09-26 21:02:49 +00:00
|
|
|
)
|
|
|
|
|
2016-02-14 10:20:47 +00:00
|
|
|
// POST handles the POST method on browse page. It's used to create new files,
|
|
|
|
// folders and upload content.
|
2016-03-12 09:52:04 +00:00
|
|
|
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-02-07 21:39:34 +00:00
|
|
|
// Remove prefix slash
|
2015-09-26 21:02:49 +00:00
|
|
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
|
|
|
|
// If it's the upload of a file
|
|
|
|
if r.Header.Get("X-Upload") == "true" {
|
2016-03-12 09:52:04 +00:00
|
|
|
return upload(w, r)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Filename not specified.",
|
|
|
|
}, 500, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := info["archetype"]; !ok {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Archtype not specified.",
|
|
|
|
}, 500, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanitize the file name path
|
|
|
|
filename := info["filename"].(string)
|
|
|
|
filename = strings.TrimPrefix(filename, "/")
|
|
|
|
filename = strings.TrimSuffix(filename, "/")
|
2016-02-25 20:02:06 +00:00
|
|
|
url := "/admin/edit/" + r.URL.Path + filename
|
2016-03-12 09:52:04 +00:00
|
|
|
filename = conf.Path + r.URL.Path + filename
|
2015-09-26 21:02:49 +00:00
|
|
|
|
2016-03-12 09:52:04 +00:00
|
|
|
if strings.HasPrefix(filename, conf.Path+"content/") &&
|
2016-02-20 12:04:12 +00:00
|
|
|
(strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) {
|
|
|
|
|
2016-03-12 09:52:04 +00:00
|
|
|
filename = strings.Replace(filename, conf.Path+"content/", "", 1)
|
2016-02-20 12:04:12 +00:00
|
|
|
args := []string{"new", filename}
|
2015-09-26 21:02:49 +00:00
|
|
|
archetype := info["archetype"].(string)
|
2016-02-20 12:04:12 +00:00
|
|
|
|
|
|
|
if archetype != "" {
|
|
|
|
args = append(args, "--kind", archetype)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 09:52:04 +00:00
|
|
|
if err := commands.Run(conf.Hugo, args, conf.Path); err != nil {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2016-02-20 12:04:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-25 20:02:06 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2016-02-20 12:04:12 +00:00
|
|
|
if err != nil {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"location": url,
|
|
|
|
"message": "File created.",
|
|
|
|
}, 200, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 09:52:04 +00:00
|
|
|
func upload(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-09-26 21:02:49 +00:00
|
|
|
// Parse the multipart form in the request
|
|
|
|
err := r.ParseMultipartForm(100000)
|
|
|
|
if err != nil {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
var outfile *os.File
|
2016-03-12 09:52:04 +00:00
|
|
|
if outfile, err = os.Create(conf.Path + r.URL.Path + hdr.Filename); nil != err {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the file content
|
|
|
|
if _, err = io.Copy(outfile, infile); nil != err {
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, map[string]string{
|
2016-03-06 15:56:53 +00:00
|
|
|
"message": "Something went wrong.",
|
|
|
|
}, 500, err)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
2015-10-17 07:10:42 +00:00
|
|
|
|
|
|
|
defer outfile.Close()
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 20:37:49 +00:00
|
|
|
return server.RespondJSON(w, nil, 200, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|