Strip path from initial request and consistency changes
Former-commit-id: ff268e5bbdbcb41bc49725e03cd5afae58882eae [formerly a28207f461e64d25d113fe71393fd69d6d421f32] [formerly 6345aaf2af5ad3ef9923f35de5e0f16dfdafbf55 [formerly 5f98fd89a8
]]
Former-commit-id: 975ee9c065d26fb99bd3a4e84bce6dceac08fd27 [formerly 6bd35709641b93c753ae109d1207933a3805b779]
Former-commit-id: fcc27613bde5397d6463fd9c2465a1e554b9407b
pull/726/head
parent
f0be416253
commit
7f28cebda6
|
@ -77,7 +77,7 @@ func command(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the path and initializes a buffer.
|
// Gets the path and initializes a buffer.
|
||||||
path := strings.Replace(r.URL.Path, ctx.FileManager.BaseURL, ctx.User.scope, 1)
|
path := ctx.User.scope + "/" + r.URL.Path
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
buff := new(bytes.Buffer)
|
buff := new(bytes.Buffer)
|
||||||
|
|
||||||
|
|
|
@ -102,9 +102,9 @@ func serveSingle(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i
|
||||||
IsDir: false,
|
IsDir: false,
|
||||||
Data: ctx.Info,
|
Data: ctx.Info,
|
||||||
User: ctx.User,
|
User: ctx.User,
|
||||||
PrefixURL: ctx.FileManager.PrefixURL,
|
PrefixURL: ctx.FileManager.prefixURL,
|
||||||
BaseURL: ctx.FileManager.AbsoluteURL(),
|
BaseURL: ctx.FileManager.RootURL(),
|
||||||
WebDavURL: ctx.FileManager.AbsoluteWebDavURL(),
|
WebDavURL: ctx.FileManager.WebDavURL(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the request accepts JSON, we send the file information.
|
// If the request accepts JSON, we send the file information.
|
||||||
|
|
4
file.go
4
file.go
|
@ -41,8 +41,8 @@ type fileInfo struct {
|
||||||
func getInfo(url *url.URL, c *FileManager, u *User) (*fileInfo, error) {
|
func getInfo(url *url.URL, c *FileManager, u *User) (*fileInfo, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
i := &fileInfo{URL: c.PrefixURL + c.BaseURL + url.Path}
|
i := &fileInfo{URL: c.RootURL() + url.Path}
|
||||||
i.VirtualPath = strings.Replace(url.Path, c.BaseURL, "", 1)
|
i.VirtualPath = url.Path
|
||||||
i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/")
|
i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/")
|
||||||
i.VirtualPath = "/" + i.VirtualPath
|
i.VirtualPath = "/" + i.VirtualPath
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package filemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -22,23 +21,19 @@ type FileManager struct {
|
||||||
*User
|
*User
|
||||||
assets *assets
|
assets *assets
|
||||||
|
|
||||||
// TODO: transform BaseURL and PrefixURL in only one. But HOW?
|
// prefixURL is a part of the URL that is already trimmed from the request URL before it
|
||||||
|
// arrives to our handlers. It may be useful when using File Manager as a middleware
|
||||||
|
// such as in caddy-filemanager plugin. It is only useful in certain situations.
|
||||||
|
prefixURL string
|
||||||
|
|
||||||
// BaseURL is the path where the GUI will be accessible. It musn't end with
|
// 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
|
// a trailing slash and mustn't contain prefixURL, if set.
|
||||||
// exported, it should only be manipulated using SetBaseURL function.
|
baseURL string
|
||||||
BaseURL string
|
|
||||||
|
|
||||||
// WebDavURL is the path where the WebDAV will be accessible. It can be set to ""
|
// webDavURL is the path where the WebDAV will be accessible. It can be set to ""
|
||||||
// in order to override the GUI and only use the WebDAV. It musn't end with
|
// in order to override the GUI and only use the WebDAV. It musn't end with
|
||||||
// a trailing slash. Despite being exported, it should only be manipulated
|
// a trailing slash.
|
||||||
// using SetWebDavURL function.
|
webDavURL string
|
||||||
WebDavURL string
|
|
||||||
|
|
||||||
// PrefixURL is a part of the URL that is trimmed from the http.Request.URL before
|
|
||||||
// it arrives to our handlers. It may be useful when using FileManager as a middleware
|
|
||||||
// such as in caddy-filemanager plugin. It musn't end with a trailing slash.
|
|
||||||
PrefixURL string
|
|
||||||
|
|
||||||
// Users is a map with the different configurations for each user.
|
// Users is a map with the different configurations for each user.
|
||||||
Users map[string]*User
|
Users map[string]*User
|
||||||
|
@ -130,39 +125,47 @@ func New(scope string) *FileManager {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// AbsoluteURL returns the actual URL where
|
// RootURL returns the actual URL where
|
||||||
// File Manager interface can be accessed.
|
// File Manager interface can be accessed.
|
||||||
func (m FileManager) AbsoluteURL() string {
|
func (m FileManager) RootURL() string {
|
||||||
return m.PrefixURL + m.BaseURL
|
return m.prefixURL + m.baseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
// AbsoluteWebDavURL returns the actual URL
|
// WebDavURL returns the actual URL
|
||||||
// where WebDAV can be accessed.
|
// where WebDAV can be accessed.
|
||||||
func (m FileManager) AbsoluteWebDavURL() string {
|
func (m FileManager) WebDavURL() string {
|
||||||
return m.PrefixURL + m.BaseURL + m.WebDavURL
|
return m.prefixURL + m.baseURL + m.webDavURL
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBaseURL updates the BaseURL of a File Manager
|
// SetPrefixURL updates the prefixURL of a File
|
||||||
|
// Manager object.
|
||||||
|
func (m FileManager) SetPrefixURL(url string) {
|
||||||
|
url = strings.TrimPrefix(url, "/")
|
||||||
|
url = strings.TrimSuffix(url, "/")
|
||||||
|
url = "/" + url
|
||||||
|
m.prefixURL = strings.TrimSuffix(url, "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBaseURL updates the baseURL of a File Manager
|
||||||
// object.
|
// object.
|
||||||
func (m *FileManager) SetBaseURL(url string) {
|
func (m *FileManager) SetBaseURL(url string) {
|
||||||
url = strings.TrimPrefix(url, "/")
|
url = strings.TrimPrefix(url, "/")
|
||||||
url = strings.TrimSuffix(url, "/")
|
url = strings.TrimSuffix(url, "/")
|
||||||
url = "/" + url
|
url = "/" + url
|
||||||
m.BaseURL = strings.TrimSuffix(url, "/")
|
m.baseURL = strings.TrimSuffix(url, "/")
|
||||||
m.SetWebDavURL(m.WebDavURL)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetWebDavURL updates the WebDavURL of a File Manager
|
// SetWebDavURL updates the webDavURL of a File Manager
|
||||||
// object and updates it's main handler.
|
// object and updates it's main handler.
|
||||||
func (m *FileManager) SetWebDavURL(url string) {
|
func (m *FileManager) SetWebDavURL(url string) {
|
||||||
url = strings.TrimPrefix(url, "/")
|
url = strings.TrimPrefix(url, "/")
|
||||||
url = strings.TrimSuffix(url, "/")
|
url = strings.TrimSuffix(url, "/")
|
||||||
|
|
||||||
m.WebDavURL = "/" + url
|
m.webDavURL = "/" + url
|
||||||
|
|
||||||
// update base user webdav handler
|
// update base user webdav handler
|
||||||
m.handler = &webdav.Handler{
|
m.handler = &webdav.Handler{
|
||||||
Prefix: m.WebDavURL,
|
Prefix: m.webDavURL,
|
||||||
FileSystem: m.fileSystem,
|
FileSystem: m.fileSystem,
|
||||||
LockSystem: webdav.NewMemLS(),
|
LockSystem: webdav.NewMemLS(),
|
||||||
}
|
}
|
||||||
|
@ -171,7 +174,7 @@ func (m *FileManager) SetWebDavURL(url string) {
|
||||||
// the new URL
|
// the new URL
|
||||||
for _, u := range m.Users {
|
for _, u := range m.Users {
|
||||||
u.handler = &webdav.Handler{
|
u.handler = &webdav.Handler{
|
||||||
Prefix: m.WebDavURL,
|
Prefix: m.webDavURL,
|
||||||
FileSystem: u.fileSystem,
|
FileSystem: u.fileSystem,
|
||||||
LockSystem: webdav.NewMemLS(),
|
LockSystem: webdav.NewMemLS(),
|
||||||
}
|
}
|
||||||
|
@ -197,7 +200,7 @@ func (m *FileManager) SetScope(scope string, username string) error {
|
||||||
u.fileSystem = webdav.Dir(u.scope)
|
u.fileSystem = webdav.Dir(u.scope)
|
||||||
|
|
||||||
u.handler = &webdav.Handler{
|
u.handler = &webdav.Handler{
|
||||||
Prefix: m.WebDavURL,
|
Prefix: m.webDavURL,
|
||||||
FileSystem: u.fileSystem,
|
FileSystem: u.fileSystem,
|
||||||
LockSystem: webdav.NewMemLS(),
|
LockSystem: webdav.NewMemLS(),
|
||||||
}
|
}
|
||||||
|
@ -238,11 +241,8 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
// Remove the base URL from the URL
|
// Strip the baseURL from the request URL.
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, m.BaseURL)
|
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
|
// Checks if the URL matches the Assets URL. Returns the asset if the
|
||||||
// method is GET and Status Forbidden otherwise.
|
// method is GET and Status Forbidden otherwise.
|
||||||
|
@ -262,7 +262,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
if matchURL(r.URL.Path, m.webDavURL) {
|
||||||
return serveWebDAV(ctx, w, r)
|
return serveWebDAV(ctx, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,7 +309,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
|
||||||
// If it's a dir and the path doesn't end with a trailing slash,
|
// If it's a dir and the path doesn't end with a trailing slash,
|
||||||
// redirect the user.
|
// redirect the user.
|
||||||
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
||||||
http.Redirect(w, r, m.PrefixURL+m.BaseURL+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, m.RootURL()+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) (
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Loads the content of the directory
|
// Loads the content of the directory
|
||||||
listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.PrefixURL+ctx.FileManager.BaseURL+r.URL.Path)
|
listing, err := getListing(ctx.User, ctx.Info.VirtualPath, ctx.FileManager.RootURL()+r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errorToHTTP(err, true), err
|
return errorToHTTP(err, true), err
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) (
|
||||||
URL: r.URL,
|
URL: r.URL,
|
||||||
}
|
}
|
||||||
|
|
||||||
cookieScope := ctx.FileManager.BaseURL
|
cookieScope := ctx.FileManager.RootURL()
|
||||||
if cookieScope == "" {
|
if cookieScope == "" {
|
||||||
cookieScope = "/"
|
cookieScope = "/"
|
||||||
}
|
}
|
||||||
|
@ -83,9 +83,9 @@ func serveListing(ctx *requestContext, w http.ResponseWriter, r *http.Request) (
|
||||||
Path: ctx.Info.VirtualPath,
|
Path: ctx.Info.VirtualPath,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
User: ctx.User,
|
User: ctx.User,
|
||||||
PrefixURL: ctx.FileManager.PrefixURL,
|
PrefixURL: ctx.FileManager.prefixURL,
|
||||||
BaseURL: ctx.FileManager.AbsoluteURL(),
|
BaseURL: ctx.FileManager.RootURL(),
|
||||||
WebDavURL: ctx.FileManager.AbsoluteWebDavURL(),
|
WebDavURL: ctx.FileManager.WebDavURL(),
|
||||||
Display: displayMode,
|
Display: displayMode,
|
||||||
Data: listing,
|
Data: listing,
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,8 +71,7 @@ func search(ctx *requestContext, w http.ResponseWriter, r *http.Request) (int, e
|
||||||
}
|
}
|
||||||
|
|
||||||
search = parseSearch(value)
|
search = parseSearch(value)
|
||||||
scope := strings.Replace(r.URL.Path, ctx.FileManager.BaseURL, "", 1)
|
scope := strings.TrimPrefix(r.URL.Path, "/")
|
||||||
scope = strings.TrimPrefix(scope, "/")
|
|
||||||
scope = "/" + scope
|
scope = "/" + scope
|
||||||
scope = ctx.User.scope + scope
|
scope = ctx.User.scope + scope
|
||||||
scope = strings.Replace(scope, "\\", "/", -1)
|
scope = strings.Replace(scope, "\\", "/", -1)
|
||||||
|
|
|
@ -12,7 +12,7 @@ func serveWebDAV(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Checks for user permissions relatively to this path.
|
// Checks for user permissions relatively to this path.
|
||||||
if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, ctx.FileManager.WebDavURL)) {
|
if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, ctx.FileManager.webDavURL)) {
|
||||||
return http.StatusForbidden, nil
|
return http.StatusForbidden, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ func serveWebDAV(ctx *requestContext, w http.ResponseWriter, r *http.Request) (i
|
||||||
//
|
//
|
||||||
// It was decided on https://github.com/hacdias/caddy-filemanager/issues/85
|
// It was decided on https://github.com/hacdias/caddy-filemanager/issues/85
|
||||||
// that GET, for collections, will return the same as PROPFIND method.
|
// that GET, for collections, will return the same as PROPFIND method.
|
||||||
path := strings.Replace(r.URL.Path, ctx.FileManager.WebDavURL, "", 1)
|
path := strings.Replace(r.URL.Path, ctx.FileManager.webDavURL, "", 1)
|
||||||
path = ctx.User.scope + "/" + path
|
path = ctx.User.scope + "/" + path
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue