progresses on #23

This commit is contained in:
Henrique Dias
2015-09-30 22:03:28 +01:00
parent c44dba20f1
commit aa8620c52c
8 changed files with 68 additions and 32 deletions

View File

@@ -4,22 +4,26 @@ import (
"net/http"
"os"
"strings"
"github.com/hacdias/caddy-cms/config"
)
// DELETE handles the DELETE method on browse page
func DELETE(w http.ResponseWriter, r *http.Request) (int, error) {
func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Remove both beginning and trailing slashes
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
path := r.URL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
path = c.Path + path
// Check if the file or directory exists
if stat, err := os.Stat(r.URL.Path); err == nil {
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(r.URL.Path)
err = os.RemoveAll(path)
} else {
err = os.Remove(r.URL.Path)
err = os.Remove(path)
}
// Check for errors