2016-06-10 13:36:43 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2016-06-10 17:27:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2016-06-10 13:36:43 +00:00
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterPlugin("filemanager", caddy.Plugin{
|
|
|
|
ServerType: "http",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// setup configures a new Browse middleware instance.
|
2016-06-10 13:36:43 +00:00
|
|
|
func setup(c *caddy.Controller) error {
|
2016-06-10 17:27:27 +00:00
|
|
|
configs, err := fileManagerParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-10 13:36:43 +00:00
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
f := FileManager{
|
|
|
|
Configs: configs,
|
|
|
|
IgnoreIndexes: false,
|
|
|
|
}
|
2016-06-10 13:36:43 +00:00
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
httpserver.GetConfig(c.Key).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
|
|
|
f.Next = next
|
|
|
|
return f
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileManagerParse(c *caddy.Controller) ([]Config, error) {
|
|
|
|
var configs []Config
|
|
|
|
|
2016-06-10 19:54:19 +00:00
|
|
|
appendCfg := func(fmc Config) error {
|
2016-06-10 17:27:27 +00:00
|
|
|
for _, c := range configs {
|
2016-06-10 19:54:19 +00:00
|
|
|
if c.PathScope == fmc.PathScope {
|
|
|
|
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-06-10 13:36:43 +00:00
|
|
|
}
|
2016-06-10 19:54:19 +00:00
|
|
|
configs = append(configs, fmc)
|
2016-06-10 17:27:27 +00:00
|
|
|
return nil
|
2016-06-10 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
for c.Next() {
|
2016-06-10 19:54:19 +00:00
|
|
|
var fmc = Config{
|
|
|
|
PathScope: ".",
|
2016-06-10 21:44:33 +00:00
|
|
|
BaseURL: "",
|
2016-06-10 19:54:19 +00:00
|
|
|
StyleSheet: "",
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-06-10 19:54:19 +00:00
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "show":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
fmc.PathScope = c.Val()
|
|
|
|
case "on":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
fmc.BaseURL = c.Val()
|
|
|
|
case "styles":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-06-10 21:18:44 +00:00
|
|
|
tplBytes, err := ioutil.ReadFile(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
fmc.StyleSheet = string(tplBytes)
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 19:54:19 +00:00
|
|
|
fmc.Root = http.Dir(fmc.PathScope)
|
2016-06-10 17:27:27 +00:00
|
|
|
|
|
|
|
// Save configuration
|
2016-06-10 19:54:19 +00:00
|
|
|
err := appendCfg(fmc)
|
2016-06-10 17:27:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
2016-06-10 13:36:43 +00:00
|
|
|
}
|