2016-06-25 20:57:10 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-08-20 20:48:04 +00:00
|
|
|
"regexp"
|
2016-08-20 19:34:08 +00:00
|
|
|
"strconv"
|
2016-06-25 20:57:10 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-07-01 14:46:06 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2016-06-25 20:57:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is a configuration for browsing in a particualr path.
|
|
|
|
type Config struct {
|
2016-08-20 20:48:04 +00:00
|
|
|
*UserConfig
|
2016-06-25 20:57:10 +00:00
|
|
|
BaseURL string
|
2016-07-01 14:46:06 +00:00
|
|
|
AbsoluteURL string
|
2016-07-01 14:51:02 +00:00
|
|
|
AddrPath string
|
2016-07-05 16:46:45 +00:00
|
|
|
Token string // Anti CSRF token
|
2016-06-29 09:16:10 +00:00
|
|
|
FrontMatter string // Default frontmatter to save files in
|
2016-06-26 17:30:08 +00:00
|
|
|
HugoEnabled bool // Enables the Hugo plugin for File Manager
|
2016-08-20 20:48:04 +00:00
|
|
|
}
|
2016-08-20 19:34:08 +00:00
|
|
|
|
2016-08-20 20:48:04 +00:00
|
|
|
// UserConfig contains the configuration for each user
|
|
|
|
type UserConfig struct {
|
|
|
|
PathScope string
|
|
|
|
Root http.FileSystem
|
|
|
|
StyleSheet string // Costum stylesheet
|
|
|
|
FrontMatter string // Default frontmatter to save files in
|
|
|
|
AllowNew bool // Can create files and folders
|
|
|
|
AllowEdit bool // Can edit/rename files
|
|
|
|
|
|
|
|
Allow []string // Allowed browse directories/files
|
|
|
|
AllowRegex []*regexp.Regexp // Regex of the previous
|
|
|
|
Block []string // Blocked browse directories/files
|
|
|
|
BlockRegex []*regexp.Regexp // Regex of the previous
|
|
|
|
|
|
|
|
AllowCommands bool // Can execute commands
|
|
|
|
AllowedCommands []string // Allowed commands
|
|
|
|
BlockedCommands []string // Blocked Commands
|
2016-06-25 20:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the configuration set by the user so it can
|
|
|
|
// be used by the middleware
|
|
|
|
func Parse(c *caddy.Controller) ([]Config, error) {
|
|
|
|
var configs []Config
|
|
|
|
|
|
|
|
appendConfig := func(cfg Config) error {
|
|
|
|
for _, c := range configs {
|
|
|
|
if c.PathScope == cfg.PathScope {
|
|
|
|
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configs = append(configs, cfg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-20 19:34:08 +00:00
|
|
|
var err error
|
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
for c.Next() {
|
2016-08-20 20:48:04 +00:00
|
|
|
var cfg = Config{}
|
|
|
|
cfg.PathScope = "."
|
|
|
|
cfg.BaseURL = ""
|
|
|
|
cfg.FrontMatter = "yaml"
|
|
|
|
cfg.HugoEnabled = false
|
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "show":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.PathScope = c.Val()
|
|
|
|
cfg.PathScope = strings.TrimSuffix(cfg.PathScope, "/")
|
2016-06-29 09:43:13 +00:00
|
|
|
case "frontmatter":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.FrontMatter = c.Val()
|
|
|
|
if cfg.FrontMatter != "yaml" && cfg.FrontMatter != "json" && cfg.FrontMatter != "toml" {
|
|
|
|
return configs, c.Err("frontmatter type not supported")
|
|
|
|
}
|
2016-06-25 20:57:10 +00:00
|
|
|
case "on":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.BaseURL = c.Val()
|
|
|
|
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
|
|
|
|
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
|
|
|
|
cfg.BaseURL = "/" + cfg.BaseURL
|
|
|
|
case "styles":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 19:34:08 +00:00
|
|
|
var tplBytes []byte
|
|
|
|
tplBytes, err = ioutil.ReadFile(c.Val())
|
2016-06-25 20:57:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
cfg.StyleSheet = string(tplBytes)
|
2016-08-20 19:34:08 +00:00
|
|
|
case "allow_new":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.AllowNew, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
case "allow_edit":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.AllowEdit, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
case "allow_comands":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
cfg.AllowCommands, err = strconv.ParseBool(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
2016-06-25 20:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:46:06 +00:00
|
|
|
caddyConf := httpserver.GetConfig(c)
|
2016-07-03 18:45:46 +00:00
|
|
|
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
|
2016-07-01 14:46:06 +00:00
|
|
|
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)
|
2016-07-03 18:45:46 +00:00
|
|
|
cfg.AbsoluteURL = strings.TrimSuffix(cfg.AbsoluteURL, "/")
|
2016-07-01 14:51:02 +00:00
|
|
|
cfg.AddrPath = strings.TrimSuffix(caddyConf.Addr.Path, "/")
|
2016-06-25 20:57:10 +00:00
|
|
|
cfg.Root = http.Dir(cfg.PathScope)
|
|
|
|
if err := appendConfig(cfg); err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|