filebrowser/routes/browse/delete.go

49 lines
1.1 KiB
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
2015-10-18 14:10:32 +00:00
"github.com/hacdias/caddy-hugo/config"
2016-03-06 20:37:49 +00:00
"github.com/hacdias/caddy-hugo/tools/server"
2015-09-26 21:02:49 +00:00
)
2016-02-14 10:20:47 +00:00
// DELETE handles the delete requests on browse pages
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
message := "File deleted."
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)
message = "Folder deleted."
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 {
2016-03-06 20:37:49 +00:00
return server.RespondJSON(w, map[string]string{
2016-03-06 15:56:53 +00:00
"message": "Something went wrong.",
}, 500, nil)
2015-09-26 21:02:49 +00:00
}
} else {
2016-03-06 20:37:49 +00:00
return server.RespondJSON(w, map[string]string{
2016-03-06 15:56:53 +00:00
"message": "File not found.",
}, 404, nil)
2015-09-26 21:02:49 +00:00
}
2016-03-06 20:37:49 +00:00
return server.RespondJSON(w, map[string]string{
2016-03-06 15:56:53 +00:00
"message": message,
}, 200, nil)
2015-09-26 21:02:49 +00:00
}