filebrowser/browse/put.go

50 lines
1.2 KiB
Go
Raw Normal View History

2016-02-06 17:18:30 +00:00
package browse
import (
"bytes"
"encoding/json"
"net/http"
"os"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/utils"
2016-02-06 17:18:30 +00:00
)
// 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, "Filename not specified.", 400, nil)
2016-02-06 17:18:30 +00:00
}
// Sanitize the file name path
new := info["filename"].(string)
new = strings.TrimPrefix(new, "/")
new = strings.TrimSuffix(new, "/")
new = c.Path + new
2016-02-14 10:20:47 +00:00
// Renames the file/folder
if err := os.Rename(old, new); err != nil {
return utils.RespondJSON(w, "Something went wrong.", 500, err)
2016-02-06 17:18:30 +00:00
}
return utils.RespondJSON(w, "File renamed.", 200, nil)
2016-02-06 17:18:30 +00:00
}