diff --git a/_embed/templates/base.tmpl b/_embed/templates/base.tmpl index a4acf2e8..3fb82fe5 100644 --- a/_embed/templates/base.tmpl +++ b/_embed/templates/base.tmpl @@ -113,7 +113,7 @@ - + diff --git a/config/config.go b/config/config.go index 806a6716..3bcbbc12 100644 --- a/config/config.go +++ b/config/config.go @@ -16,8 +16,8 @@ import ( // Config is a configuration for browsing in a particular path. type Config struct { *User + PrefixURL string BaseURL string - AbsoluteURL string AddrPath string Token string // Anti CSRF token HugoEnabled bool // Enables the Hugo plugin for File Manager @@ -26,8 +26,8 @@ type Config struct { CurrentUser *User } -func (c Config) FullWebDavURL() string { - return c.AbsoluteURL + c.WebDavURL +func (c Config) AbsoluteURL() string { + return c.PrefixURL + c.BaseURL } // Rule is a dissalow/allow rule @@ -235,13 +235,12 @@ func Parse(c *caddy.Controller) ([]Config, error) { } caddyConf := httpserver.GetConfig(c) - cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL - cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1) - cfg.AbsoluteURL = strings.TrimSuffix(cfg.AbsoluteURL, "/") + + cfg.PrefixURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") cfg.AddrPath = strings.TrimSuffix(caddyConf.Addr.Path, "/") cfg.WebDavURL = "/" + strings.TrimPrefix(cfg.WebDavURL, "/") cfg.Handler = &webdav.Handler{ - Prefix: cfg.WebDavURL, + Prefix: cfg.BaseURL + cfg.WebDavURL, FileSystem: cfg.FileSystem, LockSystem: webdav.NewMemLS(), } diff --git a/filemanager.go b/filemanager.go index 6336aa9b..eb10695d 100644 --- a/filemanager.go +++ b/filemanager.go @@ -10,6 +10,7 @@ package filemanager import ( e "errors" "net/http" + "net/url" "strings" "github.com/hacdias/caddy-filemanager/assets" @@ -64,7 +65,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err } // Checks if the request URL is for the WebDav server - if httpserver.Path(r.URL.Path).Matches(c.WebDavURL) { + if httpserver.Path(r.URL.Path).Matches(c.BaseURL + c.WebDavURL) { // Checks for user permissions relatively to this PATH if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.WebDavURL)) { return http.StatusForbidden, nil @@ -83,6 +84,17 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err // Preprocess the PUT request if it's the case if r.Method == http.MethodPut { + var urlp *url.URL + urlp, err = url.Parse(strings.Replace(r.URL.String(), c.WebDavURL, "", 1)) + if err != nil { + return code, err + } + + fi, code, err = file.GetInfo(urlp, c, user) + if err != nil { + return code, err + } + if handlers.PreProccessPUT(w, r, c, user, fi) != nil { return http.StatusInternalServerError, err } diff --git a/handlers/listing.go b/handlers/listing.go index 77503a69..6ee1c36b 100644 --- a/handlers/listing.go +++ b/handlers/listing.go @@ -18,7 +18,7 @@ func ServeListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *c var err error // Loads the content of the directory - listing, err := file.GetListing(u, i.VirtualPath, c.AbsoluteURL+r.URL.Path) + listing, err := file.GetListing(u, i.VirtualPath, c.PrefixURL+r.URL.Path) if err != nil { return errors.ErrorToHTTPCode(err, true), err } diff --git a/handlers/put.go b/handlers/put.go index 96cc4bcb..61352352 100644 --- a/handlers/put.go +++ b/handlers/put.go @@ -24,8 +24,8 @@ func PreProccessPUT( i *file.Info, ) (err error) { var ( - data map[string]interface{} - file []byte + data = map[string]interface{}{} + file = []byte{} kind string rawBuffer = new(bytes.Buffer) ) @@ -43,7 +43,7 @@ func PreProccessPUT( switch kind { case "frontmatter-only": - if file, err = ParseFrontMatterOnlyFile(data, i.Name()); err != nil { + if file, err = ParseFrontMatterOnlyFile(data, i.FileInfo.Name()); err != nil { return } case "content-only": diff --git a/page/page.go b/page/page.go index 3cc08fd5..b6b59e45 100644 --- a/page/page.go +++ b/page/page.go @@ -67,10 +67,10 @@ func (i Info) BreadcrumbMap() map[string]string { func (i Info) PreviousLink() string { path := strings.TrimSuffix(i.Path, "/") path = strings.TrimPrefix(path, "/") - path = i.Config.AbsoluteURL + "/" + path + path = i.Config.AbsoluteURL() + "/" + path path = path[0 : len(path)-len(i.Name)] - if len(path) < len(i.Config.AbsoluteURL+"/") { + if len(path) < len(i.Config.AbsoluteURL()+"/") { return "" }