diff --git a/assets.go b/assets.go index 77d7f3a3..81498d59 100644 --- a/assets.go +++ b/assets.go @@ -14,7 +14,7 @@ const assetsURL = "/_internal" // Serve provides the needed assets for the front-end func serveAssets(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, error) { // gets the filename to be used with Assets function - filename := strings.Replace(r.URL.Path, ctx.FileManager.BaseURL+assetsURL, "", 1) + filename := strings.TrimPrefix(r.URL.Path, assetsURL) var file []byte var err error diff --git a/cmd/filemanager/main.go b/cmd/filemanager/main.go index 6136d72e..042707cb 100644 --- a/cmd/filemanager/main.go +++ b/cmd/filemanager/main.go @@ -18,6 +18,7 @@ func handler(w http.ResponseWriter, r *http.Request) { func main() { m = filemanager.New("D:\\TEST") + m.SetBaseURL("/vaca") http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) } diff --git a/file.go b/file.go index fd2e612d..32ce0b55 100644 --- a/file.go +++ b/file.go @@ -41,7 +41,7 @@ type fileInfo struct { func getInfo(url *url.URL, c *FileManager, u *User) (*fileInfo, error) { var err error - i := &fileInfo{URL: c.PrefixURL + url.Path} + i := &fileInfo{URL: c.PrefixURL + c.BaseURL + url.Path} i.VirtualPath = strings.Replace(url.Path, c.BaseURL, "", 1) i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/") i.VirtualPath = "/" + i.VirtualPath diff --git a/filemanager.go b/filemanager.go index 7ceb7d5e..b2fee777 100644 --- a/filemanager.go +++ b/filemanager.go @@ -2,6 +2,7 @@ package filemanager import ( "errors" + "fmt" "net/http" "regexp" "strings" @@ -21,6 +22,8 @@ type FileManager struct { *User assets *assets + // TODO: transform BaseURL and PrefixURL in only one. But HOW? + // BaseURL is the path where the GUI will be accessible. It musn't end with // a trailing slash and mustn't contain PrefixURL, if set. Despite being // exported, it should only be manipulated using SetBaseURL function. @@ -136,7 +139,7 @@ func (m FileManager) AbsoluteURL() string { // AbsoluteWebDavURL returns the actual URL // where WebDAV can be accessed. func (m FileManager) AbsoluteWebDavURL() string { - return m.PrefixURL + m.WebDavURL + return m.PrefixURL + m.BaseURL + m.WebDavURL } // SetBaseURL updates the BaseURL of a File Manager @@ -146,6 +149,7 @@ func (m *FileManager) SetBaseURL(url string) { url = strings.TrimSuffix(url, "/") url = "/" + url m.BaseURL = strings.TrimSuffix(url, "/") + m.SetWebDavURL(m.WebDavURL) } // SetWebDavURL updates the WebDavURL of a File Manager @@ -154,7 +158,7 @@ func (m *FileManager) SetWebDavURL(url string) { url = strings.TrimPrefix(url, "/") url = strings.TrimSuffix(url, "/") - m.WebDavURL = m.BaseURL + "/" + url + m.WebDavURL = "/" + url // update base user webdav handler m.handler = &webdav.Handler{ @@ -234,9 +238,15 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er err error ) + // Remove the base URL from the URL + r.URL.Path = strings.TrimPrefix(r.URL.Path, m.BaseURL) + + // TODO: remove this + fmt.Printf("Raw: %s\tParsed: %s\n", m.PrefixURL+m.BaseURL+r.URL.Path, r.URL.Path) + // Checks if the URL matches the Assets URL. Returns the asset if the // method is GET and Status Forbidden otherwise. - if matchURL(r.URL.Path, m.BaseURL+assetsURL) { + if matchURL(r.URL.Path, assetsURL) { if r.Method == http.MethodGet { return serveAssets(ctx, w, r) } @@ -251,7 +261,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er ctx.User = m.User } - // Checks if the request URL is for the WebDav server + // Checks if the request URL is for the WebDav server. if matchURL(r.URL.Path, m.WebDavURL) { return serveWebDAV(ctx, w, r) } @@ -261,7 +271,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er w.Header().Set("x-xss-protection", "1; mode=block") // Checks if the User is allowed to access this file - if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, m.BaseURL)) { + if !ctx.User.Allowed(r.URL.Path) { if r.Method == http.MethodGet { return htmlError( w, http.StatusForbidden, @@ -294,10 +304,12 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er return code, err } + ctx.Info = f + // If it's a dir and the path doesn't end with a trailing slash, // redirect the user. if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") { - http.Redirect(w, r, m.PrefixURL+r.URL.Path+"/", http.StatusTemporaryRedirect) + http.Redirect(w, r, m.PrefixURL+m.BaseURL+r.URL.Path+"/", http.StatusTemporaryRedirect) return 0, nil } diff --git a/http_listing.go b/http_listing.go index c6e2074c..0d744b56 100644 --- a/http_listing.go +++ b/http_listing.go @@ -14,7 +14,7 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) ( var err error // Loads the content of the directory - listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.PrefixURL+r.URL.Path) + listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.PrefixURL+ctx.FileManager.BaseURL+r.URL.Path) if err != nil { return errorToHTTP(err, true), err }