Start implementing webdav

pull/144/head
Henrique Dias 2016-10-17 21:05:52 +01:00
parent db4b01839a
commit 86b06b3f06
2 changed files with 45 additions and 17 deletions

View File

@ -8,6 +8,8 @@ import (
"strconv"
"strings"
"golang.org/x/net/webdav"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
@ -15,13 +17,16 @@ import (
// Config is a configuration for browsing in a particualr path.
type Config struct {
*User
BaseURL string
AbsoluteURL string
AddrPath string
Token string // Anti CSRF token
HugoEnabled bool // Enables the Hugo plugin for File Manager
Users map[string]*User
CurrentUser *User
BaseURL string
AbsoluteURL string
AddrPath string
Token string // Anti CSRF token
HugoEnabled bool // Enables the Hugo plugin for File Manager
Users map[string]*User
WebDav bool
WebDavURL string
WebDavHandler *webdav.Handler
CurrentUser *User
}
// Rule is a dissalow/allow rule
@ -77,6 +82,14 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.BaseURL = args[0]
}
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
if cfg.BaseURL == "/" {
cfg.BaseURL = ""
}
// Set the first user, the global user
user = cfg.User
@ -91,6 +104,26 @@ func Parse(c *caddy.Controller) ([]Config, error) {
if user.FrontMatter != "yaml" && user.FrontMatter != "json" && user.FrontMatter != "toml" {
return configs, c.Err("frontmatter type not supported")
}
case "webdav":
cfg.WebDav = true
prefix := "webdav"
if c.NextArg() {
prefix = c.Val()
}
prefix = strings.TrimPrefix(prefix, "/")
prefix = strings.TrimSuffix(prefix, "/")
prefix = cfg.BaseURL + "/" + prefix
cfg.WebDavURL = prefix
cfg.WebDavHandler = &webdav.Handler{
Prefix: prefix,
FileSystem: webdav.Dir(cfg.PathScope),
LockSystem: webdav.NewMemLS(),
}
// TODO
case "show":
if !c.NextArg() {
return configs, c.ArgErr()
@ -209,14 +242,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
}
}
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
if cfg.BaseURL == "/" {
cfg.BaseURL = ""
}
caddyConf := httpserver.GetConfig(c)
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)

View File

@ -49,8 +49,6 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
c = &f.Configs[i]
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
// Set the current user.
username, _, _ := r.BasicAuth()
if _, ok := c.Users[username]; ok {
@ -59,6 +57,11 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
user = c.User
}
if c.WebDav && strings.HasPrefix(r.URL.Path, c.WebDavURL) {
c.WebDavHandler.ServeHTTP(w, r)
return 0, nil
}
// Checks if the user has permission to access the current directory.
if !user.Allowed(r.URL.Path) {
if r.Method == http.MethodGet {