filebrowser/browse/post.go

145 lines
3.9 KiB
Go
Raw Normal View History

2015-09-27 05:02:49 +08:00
package browse
import (
"bytes"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"os"
"strings"
2015-10-18 22:10:32 +08:00
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
2015-09-27 05:02:49 +08:00
)
2016-02-14 18:20:47 +08:00
// POST handles the POST method on browse page. It's used to create new files,
// folders and upload content.
2015-10-01 05:03:28 +08:00
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-02-08 05:39:34 +08:00
// Remove prefix slash
2015-09-27 05:02:49 +08: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-02-08 05:39:34 +08:00
return upload(w, r, c)
2015-09-27 05:02:49 +08: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 {
2015-09-27 05:19:22 +08:00
return http.StatusBadRequest, errors.New("Filename not specified.")
2015-09-27 05:02:49 +08:00
}
if _, ok := info["archetype"]; !ok {
2015-09-27 05:19:22 +08:00
return http.StatusBadRequest, errors.New("Archtype not specified.")
2015-09-27 05:02:49 +08:00
}
// Sanitize the file name path
filename := info["filename"].(string)
filename = strings.TrimPrefix(filename, "/")
filename = strings.TrimSuffix(filename, "/")
2015-10-01 05:03:28 +08:00
filename = c.Path + r.URL.Path + filename
2015-09-27 05:02:49 +08:00
// Check if the archetype is defined
if info["archetype"] != "" {
// Sanitize the archetype path
archetype := info["archetype"].(string)
archetype = strings.Replace(archetype, "/archetypes", "", 1)
archetype = strings.Replace(archetype, "archetypes", "", 1)
archetype = strings.TrimPrefix(archetype, "/")
archetype = strings.TrimSuffix(archetype, "/")
2016-02-08 16:03:32 +08:00
archetype = c.Path + "archetypes/" + archetype
2015-09-27 05:02:49 +08:00
// Check if the archetype ending with .markdown exists
if _, err := os.Stat(archetype + ".markdown"); err == nil {
err = utils.CopyFile(archetype+".markdown", filename)
if err != nil {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
w.Header().Set("Location", "/admin/edit/"+filename)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 201, nil
}
// Check if the archetype ending with .md exists
if _, err := os.Stat(archetype + ".md"); err == nil {
err = utils.CopyFile(archetype+".md", filename)
if err != nil {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
w.Header().Set("Location", "/admin/edit/"+filename)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 201, nil
}
}
wf, err := os.Create(filename)
if err != nil {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
defer wf.Close()
w.Header().Set("Location", "/admin/edit/"+filename)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
2015-09-27 05:19:22 +08:00
return http.StatusOK, nil
2015-09-27 05:02:49 +08:00
}
2016-02-08 05:39:34 +08:00
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2015-09-27 05:02:49 +08:00
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08: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 {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
// Create the file
var outfile *os.File
2016-02-08 05:39:34 +08:00
if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err {
2015-09-27 05:02:49 +08:00
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
// Copy the file content
if _, err = io.Copy(outfile, infile); nil != err {
w.Write([]byte(err.Error()))
2015-09-27 05:19:22 +08:00
return http.StatusInternalServerError, err
2015-09-27 05:02:49 +08:00
}
2015-10-17 15:10:42 +08:00
defer outfile.Close()
2015-09-27 05:02:49 +08:00
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
2015-09-27 05:19:22 +08:00
return http.StatusOK, nil
2015-09-27 05:02:49 +08:00
}