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" "strconv"
"strings" "strings"
"golang.org/x/net/webdav"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
@ -21,6 +23,9 @@ type Config struct {
Token string // Anti CSRF token Token string // Anti CSRF token
HugoEnabled bool // Enables the Hugo plugin for File Manager HugoEnabled bool // Enables the Hugo plugin for File Manager
Users map[string]*User Users map[string]*User
WebDav bool
WebDavURL string
WebDavHandler *webdav.Handler
CurrentUser *User CurrentUser *User
} }
@ -77,6 +82,14 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.BaseURL = args[0] 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 // Set the first user, the global user
user = cfg.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" { if user.FrontMatter != "yaml" && user.FrontMatter != "json" && user.FrontMatter != "toml" {
return configs, c.Err("frontmatter type not supported") 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": case "show":
if !c.NextArg() { if !c.NextArg() {
return configs, c.ArgErr() 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) caddyConf := httpserver.GetConfig(c)
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1) 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) { if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
c = &f.Configs[i] c = &f.Configs[i]
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL) serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
// Set the current user.
username, _, _ := r.BasicAuth() username, _, _ := r.BasicAuth()
if _, ok := c.Users[username]; ok { 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 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. // Checks if the user has permission to access the current directory.
if !user.Allowed(r.URL.Path) { if !user.Allowed(r.URL.Path) {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {