2016-02-06 17:18:30 +00:00
|
|
|
package browse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2016-03-06 20:37:49 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/tools/server"
|
2016-02-06 17:18:30 +00:00
|
|
|
)
|
|
|
|
|
2016-06-07 17:15:55 +00:00
|
|
|
// PUT handles the HTTP PUT request for all /{admin}/browse related requests.
|
2016-02-06 17:18:30 +00:00
|
|
|
// Renames a file and/or a folder.
|
2016-03-12 09:52:04 +00:00
|
|
|
func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-02-06 17:18:30 +00:00
|
|
|
// Remove both beginning and trailing slashes
|
|
|
|
old := r.URL.Path
|
|
|
|
old = strings.TrimPrefix(old, "/")
|
|
|
|
old = strings.TrimSuffix(old, "/")
|
2016-03-12 09:52:04 +00:00
|
|
|
old = conf.Path + old
|
2016-02-06 17:18:30 +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-12 13:03:31 +00:00
|
|
|
return server.RespondJSON(w, &response{"Filename not specified.", ""}, http.StatusBadRequest, 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, "/")
|
2016-03-12 09:52:04 +00:00
|
|
|
new = conf.Path + new
|
2016-02-06 17:18:30 +00:00
|
|
|
|
2016-02-14 10:20:47 +00:00
|
|
|
// Renames the file/folder
|
|
|
|
if err := os.Rename(old, new); err != nil {
|
2016-03-12 13:03:31 +00:00
|
|
|
return server.RespondJSON(w, &response{err.Error(), ""}, http.StatusInternalServerError, err)
|
2016-02-06 17:18:30 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:03:31 +00:00
|
|
|
return server.RespondJSON(w, &response{"File renamed.", ""}, http.StatusOK, nil)
|
2016-02-06 17:18:30 +00:00
|
|
|
}
|