filebrowser/routes/browse/delete.go

42 lines
1006 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
2016-03-12 13:03:31 +00:00
s "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
2016-03-12 09:52:04 +00:00
func DELETE(w http.ResponseWriter, r *http.Request) (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, "/")
2016-03-12 09:52:04 +00:00
path = conf.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-12 13:03:31 +00:00
return s.RespondJSON(w, &response{err.Error(), ""}, http.StatusInternalServerError, err)
2015-09-26 21:02:49 +00:00
}
} else {
2016-03-12 13:03:31 +00:00
return s.RespondJSON(w, &response{"File not found.", ""}, http.StatusNotFound, nil)
2015-09-26 21:02:49 +00:00
}
2016-03-12 13:03:31 +00:00
return s.RespondJSON(w, &response{message, ""}, http.StatusOK, nil)
2015-09-26 21:02:49 +00:00
}