update comments and unexport some fields

Former-commit-id: 716d6d7e7a1e3c676dbc4a56d1576eee1aabb0a3
This commit is contained in:
Henrique Dias 2017-06-25 10:52:34 +01:00
parent f3c8ea21e6
commit 5ed0527092
8 changed files with 71 additions and 38 deletions

View File

@ -14,7 +14,7 @@
<script> <script>
var user = JSON.parse('{{ Marshal .User }}'), var user = JSON.parse('{{ Marshal .User }}'),
webdavURL = "{{.Config.AbsoluteWebdavURL }}", webdavURL = "{{.Config.AbsoluteWebDavURL }}",
baseURL = "{{.Config.AbsoluteURL}}", baseURL = "{{.Config.AbsoluteURL}}",
prefixURL = "{{ .Config.PrefixURL }}"; prefixURL = "{{ .Config.PrefixURL }}";
</script> </script>

View File

@ -11,44 +11,59 @@ import (
// FileManager is a file manager instance. // FileManager is a file manager instance.
type FileManager struct { type FileManager struct {
*User `json:"-"` *User
Assets *Assets `json:"-"` Assets *assets
// PrefixURL is a part of the URL that is trimmed from the http.Request.URL before // 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 // it arrives to our handlers. It may be useful when using FileManager as a middleware
// such as in caddy-filemanager plugin. // such as in caddy-filemanager plugin. It musn't end with a trailing slash.
PrefixURL string PrefixURL string
// BaseURL is the path where the GUI will be accessible. // 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.
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. // in order to override the GUI and only use the WebDAV. It musn't end with
// a trailing slash.
WebDavURL string WebDavURL 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 `json:"-"` Users map[string]*User
// TODO: event-based? // TODO: event-based?
BeforeSave CommandFunc `json:"-"` BeforeSave CommandFunc
AfterSave CommandFunc `json:"-"` AfterSave CommandFunc
} }
// User contains the configuration for each user. // User contains the configuration for each user.
type User struct { type User struct {
Scope string `json:"-"` // Path the user have access // scope is the physical path the user has access to.
FileSystem webdav.FileSystem `json:"-"` // The virtual file system the user have access scope string
Handler *webdav.Handler `json:"-"` // The WebDav HTTP Handler
Rules []*Rule `json:"-"` // Access rules // fileSystem is the virtual file system the user has access.
StyleSheet string `json:"-"` // Costum stylesheet fileSystem webdav.FileSystem
AllowNew bool // Can create files and folders
AllowEdit bool // Can edit/rename files // handler handles incoming requests to the WebDAV backend.
AllowCommands bool // Can execute commands handler *webdav.Handler
Commands []string // Available Commands
// Rules is an array of access and deny rules.
Rules []*Rule `json:"-"`
// TODO: this MUST be done in another way
StyleSheet string `json:"-"`
// These indicate if the user can perform certain actions.
AllowNew bool // Create files and folders
AllowEdit bool // Edit/rename files
AllowCommands bool // Execute commands
// Commands is the list of commands the user can execute.
Commands []string
} }
// Assets are the static and front-end assets, such as JS, CSS and HTML templates. // assets are the static and front-end assets, such as JS, CSS and HTML templates.
type Assets struct { type assets struct {
requiredJS *rice.Box // JS that is always required to have in order to be usable. requiredJS *rice.Box // JS that is always required to have in order to be usable.
Templates *rice.Box Templates *rice.Box
CSS *rice.Box CSS *rice.Box
@ -57,9 +72,16 @@ type Assets struct {
// Rule is a dissalow/allow rule. // Rule is a dissalow/allow rule.
type Rule struct { type Rule struct {
// Regex indicates if this rule uses Regular Expressions or not.
Regex bool Regex bool
// Allow indicates if this is an allow rule. Set 'false' to be a disallow rule.
Allow bool Allow bool
// Path is the corresponding URL path for this rule.
Path string Path string
// Regexp is the regular expression. Only use this when 'Regex' was set to true.
Regexp *regexp.Regexp Regexp *regexp.Regexp
} }
@ -82,7 +104,7 @@ func New() *FileManager {
Users: map[string]*User{}, Users: map[string]*User{},
BeforeSave: func(r *http.Request, c *FileManager, u *User) error { return nil }, BeforeSave: func(r *http.Request, c *FileManager, u *User) error { return nil },
AfterSave: func(r *http.Request, c *FileManager, u *User) error { return nil }, AfterSave: func(r *http.Request, c *FileManager, u *User) error { return nil },
Assets: &Assets{ Assets: &assets{
Templates: rice.MustFindBox("./_assets/templates"), Templates: rice.MustFindBox("./_assets/templates"),
CSS: rice.MustFindBox("./_assets/css"), CSS: rice.MustFindBox("./_assets/css"),
requiredJS: rice.MustFindBox("./_assets/js"), requiredJS: rice.MustFindBox("./_assets/js"),
@ -96,14 +118,20 @@ func New() *FileManager {
return m return m
} }
// AbsoluteURL returns the actual URL where
// File Manager interface can be accessed.
func (m FileManager) AbsoluteURL() string { func (m FileManager) AbsoluteURL() string {
return m.PrefixURL + m.BaseURL return m.PrefixURL + m.BaseURL
} }
func (m FileManager) AbsoluteWebdavURL() 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.WebDavURL
} }
// SetBaseURL updates the BaseURL of a File Manager
// 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, "/")
@ -111,18 +139,23 @@ func (m *FileManager) SetBaseURL(url string) {
m.BaseURL = strings.TrimSuffix(url, "/") m.BaseURL = strings.TrimSuffix(url, "/")
} }
// SetWebDavURL updates the WebDavURL of a File Manager
// object and updates it's main handler.
func (m *FileManager) SetWebDavURL(url string) { func (m *FileManager) SetWebDavURL(url string) {
m.WebDavURL = m.BaseURL + "/" + strings.TrimPrefix(url, "/") url = strings.TrimPrefix(url, "/")
m.User.Handler = &webdav.Handler{ url = strings.TrimSuffix(url, "/")
m.WebDavURL = m.BaseURL + "/" + url
m.User.handler = &webdav.Handler{
Prefix: m.WebDavURL, Prefix: m.WebDavURL,
FileSystem: m.FileSystem, FileSystem: m.fileSystem,
LockSystem: webdav.NewMemLS(), LockSystem: webdav.NewMemLS(),
} }
} }
func (u *User) SetScope(scope string) { func (u *User) SetScope(scope string) {
u.Scope = strings.TrimSuffix(scope, "/") u.scope = strings.TrimSuffix(scope, "/")
u.FileSystem = webdav.Dir(u.Scope) u.fileSystem = webdav.Dir(u.scope)
} }
// Allowed checks if the user has permission to access a directory/file. // Allowed checks if the user has permission to access a directory/file.

View File

@ -59,7 +59,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
// 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, c.WebDavURL, "", 1) path := strings.Replace(r.URL.Path, c.WebDavURL, "", 1)
path = user.Scope + "/" + path path = user.scope + "/" + path
path = filepath.Clean(path) path = filepath.Clean(path)
var i os.FileInfo var i os.FileInfo
@ -97,7 +97,7 @@ func (c *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
} }
} }
c.Handler.ServeHTTP(w, r) c.handler.ServeHTTP(w, r)
if err = c.AfterSave(r, c, user); err != nil { if err = c.AfterSave(r, c, user); err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }

View File

@ -77,7 +77,7 @@ func command(w http.ResponseWriter, r *http.Request, c *FileManager, u *User) (i
} }
// Gets the path and initializes a buffer. // Gets the path and initializes a buffer.
path := strings.Replace(r.URL.Path, c.BaseURL, c.Scope, 1) path := strings.Replace(r.URL.Path, c.BaseURL, c.scope, 1)
path = filepath.Clean(path) path = filepath.Clean(path)
buff := new(bytes.Buffer) buff := new(bytes.Buffer)

View File

@ -20,7 +20,7 @@ func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *Use
} }
listing.Context = httpserver.Context{ listing.Context = httpserver.Context{
Root: http.Dir(u.Scope), Root: http.Dir(u.scope),
Req: r, Req: r,
URL: r.URL, URL: r.URL,
} }

View File

@ -74,7 +74,7 @@ func search(w http.ResponseWriter, r *http.Request, c *FileManager, u *User) (in
scope := strings.Replace(r.URL.Path, c.BaseURL, "", 1) scope := strings.Replace(r.URL.Path, c.BaseURL, "", 1)
scope = strings.TrimPrefix(scope, "/") scope = strings.TrimPrefix(scope, "/")
scope = "/" + scope scope = "/" + scope
scope = u.Scope + scope scope = u.scope + scope
scope = strings.Replace(scope, "\\", "/", -1) scope = strings.Replace(scope, "\\", "/", -1)
scope = filepath.Clean(scope) scope = filepath.Clean(scope)

View File

@ -41,7 +41,7 @@ func GetInfo(url *url.URL, c *FileManager, u *User) (*FileInfo, error) {
i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/") i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/")
i.VirtualPath = "/" + i.VirtualPath i.VirtualPath = "/" + i.VirtualPath
i.Path = u.Scope + i.VirtualPath i.Path = u.scope + i.VirtualPath
i.Path = filepath.Clean(i.Path) i.Path = filepath.Clean(i.Path)
info, err := os.Stat(i.Path) info, err := os.Stat(i.Path)

View File

@ -36,7 +36,7 @@ type Listing struct {
func GetListing(u *User, filePath string, baseURL string) (*Listing, error) { func GetListing(u *User, filePath string, baseURL string) (*Listing, error) {
// Gets the directory information using the Virtual File System of // Gets the directory information using the Virtual File System of
// the user configuration. // the user configuration.
file, err := u.FileSystem.OpenFile(context.TODO(), filePath, os.O_RDONLY, 0) file, err := u.fileSystem.OpenFile(context.TODO(), filePath, os.O_RDONLY, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }