filebrowser/browse/delete.go

42 lines
864 B
Go
Raw Normal View History

2015-09-27 05:02:49 +08:00
package browse
import (
"net/http"
"os"
"strings"
2015-10-01 05:03:28 +08:00
2015-10-18 22:10:32 +08:00
"github.com/hacdias/caddy-hugo/config"
2015-09-27 05:02:49 +08:00
)
// DELETE handles the DELETE method on browse page
2015-10-01 05:03:28 +08:00
func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2015-09-27 05:02:49 +08:00
// Remove both beginning and trailing slashes
2015-10-01 05:03:28 +08:00
path := r.URL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
path = c.Path + path
2015-09-27 05:02:49 +08:00
// Check if the file or directory exists
2015-10-01 05:03:28 +08:00
if stat, err := os.Stat(path); err == nil {
2015-09-27 05:02:49 +08:00
var err error
// If it's dir, remove all of the content inside
if stat.IsDir() {
2015-10-01 05:03:28 +08:00
err = os.RemoveAll(path)
2015-09-27 05:02:49 +08:00
} else {
2015-10-01 05:03:28 +08:00
err = os.Remove(path)
2015-09-27 05:02:49 +08: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
}