2015-09-15 20:40:46 +00:00
|
|
|
package browse
|
|
|
|
|
|
|
|
import (
|
2015-09-17 16:41:31 +00:00
|
|
|
"log"
|
2015-09-15 20:40:46 +00:00
|
|
|
"net/http"
|
2015-09-20 10:29:31 +00:00
|
|
|
"os"
|
2015-09-17 16:41:31 +00:00
|
|
|
"strings"
|
2015-09-18 12:52:10 +00:00
|
|
|
"text/template"
|
2015-09-15 20:40:46 +00:00
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/config"
|
2015-09-18 12:52:10 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
2015-09-17 16:41:31 +00:00
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/browse"
|
2015-09-15 20:40:46 +00:00
|
|
|
)
|
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
// ServeHTTP is used to serve the content of Browse page
|
|
|
|
// using Browse middleware from Caddy
|
|
|
|
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
|
|
|
// Removes the page main path from the URL
|
2015-09-17 16:41:31 +00:00
|
|
|
r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
|
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
// If the URL is blank now, replace it with a trailing slash
|
2015-09-17 16:41:31 +00:00
|
|
|
if r.URL.Path == "" {
|
|
|
|
r.URL.Path = "/"
|
|
|
|
}
|
|
|
|
|
2015-09-20 10:29:31 +00:00
|
|
|
if r.Method == "DELETE" {
|
|
|
|
// Remove both beginning and trailing slashes
|
|
|
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
|
2015-09-17 16:41:31 +00:00
|
|
|
|
2015-09-20 10:29:31 +00:00
|
|
|
// Check if the file or directory exists
|
|
|
|
if stat, err := os.Stat(r.URL.Path); err == nil {
|
|
|
|
var err error
|
|
|
|
// If it's dir, remove all of the content inside
|
|
|
|
if stat.IsDir() {
|
|
|
|
err = os.RemoveAll(r.URL.Path)
|
|
|
|
} else {
|
|
|
|
err = os.Remove(r.URL.Path)
|
|
|
|
}
|
2015-09-17 16:41:31 +00:00
|
|
|
|
2015-09-20 10:29:31 +00:00
|
|
|
// Check for errors
|
|
|
|
if err != nil {
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
} else {
|
2015-09-17 16:41:31 +00:00
|
|
|
return 404, nil
|
2015-09-20 10:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
} else {
|
|
|
|
functions := template.FuncMap{
|
|
|
|
"CanBeEdited": utils.CanBeEdited,
|
|
|
|
"Defined": utils.Defined,
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err := utils.GetTemplate(r, functions, "browse")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return 500, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := browse.Browse{
|
|
|
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 404, nil
|
|
|
|
}),
|
|
|
|
Root: "./",
|
|
|
|
Configs: []browse.Config{
|
|
|
|
browse.Config{
|
|
|
|
PathScope: "/",
|
|
|
|
Variables: c,
|
|
|
|
Template: tpl,
|
|
|
|
},
|
2015-09-17 16:41:31 +00:00
|
|
|
},
|
2015-09-20 10:29:31 +00:00
|
|
|
IgnoreIndexes: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.ServeHTTP(w, r)
|
2015-09-17 16:41:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 10:29:31 +00:00
|
|
|
return 200, nil
|
2015-09-15 20:40:46 +00:00
|
|
|
}
|