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 15:39:03 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
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
|
|
|
|
2016-03-06 15:39:03 +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)
|
2016-03-06 15:39:03 +00:00
|
|
|
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 15:39:03 +00:00
|
|
|
return utils.RespondJSON(w, "Something went wrong.", 500, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-06 15:39:03 +00:00
|
|
|
return utils.RespondJSON(w, "File not found.", 404, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:39:03 +00:00
|
|
|
return utils.RespondJSON(w, message, 200, nil)
|
2015-09-26 21:02:49 +00:00
|
|
|
}
|