filebrowser/browse/delete.go

42 lines
863 B
Go
Raw Normal View History

2015-09-26 21:02:49 +00:00
package browse
import (
"net/http"
"os"
"strings"
2015-09-30 21:03:28 +00:00
"github.com/hacdias/caddy-cms/config"
2015-09-26 21:02:49 +00:00
)
// DELETE handles the DELETE method on browse page
2015-09-30 21:03:28 +00:00
func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2015-09-26 21:02:49 +00:00
// Remove both beginning and trailing slashes
2015-09-30 21:03:28 +00:00
path := r.URL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
path = c.Path + path
2015-09-26 21:02:49 +00:00
// Check if the file or directory exists
2015-09-30 21:03:28 +00:00
if stat, err := os.Stat(path); err == nil {
2015-09-26 21:02:49 +00:00
var err error
// If it's dir, remove all of the content inside
if stat.IsDir() {
2015-09-30 21:03:28 +00:00
err = os.RemoveAll(path)
2015-09-26 21:02:49 +00:00
} else {
2015-09-30 21:03:28 +00:00
err = os.Remove(path)
2015-09-26 21:02:49 +00:00
}
// Check for errors
if err != nil {
w.Write([]byte(err.Error()))
return 500, err
}
} else {
return 404, nil
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
return 200, nil
}