filebrowser/filemanager.go

211 lines
5.4 KiB
Go
Raw Normal View History

2017-06-20 10:40:52 +00:00
package filemanager
2017-06-24 11:12:15 +00:00
import (
2017-06-25 10:40:23 +00:00
"errors"
2017-06-24 11:12:15 +00:00
"net/http"
"regexp"
"strings"
rice "github.com/GeertJohan/go.rice"
"golang.org/x/net/webdav"
)
2017-06-25 11:53:49 +00:00
var (
// ErrDuplicated occurs when you try to create a user that already exists.
ErrDuplicated = errors.New("Duplicated user")
)
2017-06-25 14:24:16 +00:00
// FileManager is a file manager instance. It should be creating using the
// 'New' function and not directly.
2017-06-24 11:12:15 +00:00
type FileManager struct {
2017-06-25 14:24:16 +00:00
*User
2017-06-24 11:12:15 +00:00
// 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
2017-06-27 09:31:50 +00:00
// 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
2017-06-24 11:12:15 +00:00
// Users is a map with the different configurations for each user.
2017-06-25 14:24:16 +00:00
Users map[string]*User
2017-06-24 11:12:15 +00:00
2017-06-25 14:19:23 +00:00
// BeforeSave is a function that is called before saving a file.
BeforeSave Command
// AfterSave is a function that is called before saving a file.
AfterSave Command
2017-06-28 10:45:41 +00:00
2017-07-02 16:40:52 +00:00
assets *rice.Box
2017-06-24 11:12:15 +00:00
}
2017-06-25 14:19:23 +00:00
// Command is a command function.
2017-06-25 14:24:16 +00:00
type Command func(r *http.Request, m *FileManager, u *User) error
2017-06-25 14:19:23 +00:00
2017-06-25 14:24:16 +00:00
// User contains the configuration for each user. It should be created
// using NewUser on a File Manager instance.
type User struct {
2017-07-02 16:40:52 +00:00
// Scope is the physical path the user has access to.
Scope string
// fileSystem is the virtual file system the user has access.
fileSystem webdav.FileSystem
// 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.
2017-06-28 15:05:30 +00:00
AllowNew bool `json:"allowNew"` // Create files and folders
AllowEdit bool `json:"allowEdit"` // Edit/rename files
AllowCommands bool `json:"allowCommands"` // Execute commands
// Commands is the list of commands the user can execute.
2017-06-28 15:05:30 +00:00
Commands []string `json:"commands"`
2017-06-24 11:12:15 +00:00
}
// Rule is a dissalow/allow rule.
type Rule struct {
// Regex indicates if this rule uses Regular Expressions or not.
Regex bool
// Allow indicates if this is an allow rule. Set 'false' to be a disallow rule.
Allow bool
// Path is the corresponding URL path for this rule.
Path string
// Regexp is the regular expression. Only use this when 'Regex' was set to true.
2017-06-24 11:12:15 +00:00
Regexp *regexp.Regexp
}
2017-06-25 14:19:23 +00:00
// New creates a new File Manager instance with the needed
// configuration to work.
2017-06-25 11:53:49 +00:00
func New(scope string) *FileManager {
2017-06-25 08:11:25 +00:00
m := &FileManager{
2017-06-25 14:24:16 +00:00
User: &User{
2017-06-25 08:11:25 +00:00
AllowCommands: true,
AllowEdit: true,
AllowNew: true,
2017-06-25 14:19:23 +00:00
Commands: []string{},
Rules: []*Rule{},
2017-06-25 08:11:25 +00:00
},
2017-06-25 14:24:16 +00:00
Users: map[string]*User{},
BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
AfterSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
2017-07-02 16:40:52 +00:00
assets: rice.MustFindBox("./_assets/dist"),
2017-06-25 08:11:25 +00:00
}
2017-06-25 11:53:49 +00:00
m.SetScope(scope, "")
2017-06-25 08:11:25 +00:00
m.SetBaseURL("/")
return m
}
// RootURL returns the actual URL where
// File Manager interface can be accessed.
func (m FileManager) RootURL() string {
return m.prefixURL + m.baseURL
2017-06-24 11:12:15 +00:00
}
// WebDavURL returns the actual URL
// where WebDAV can be accessed.
func (m FileManager) WebDavURL() string {
2017-07-02 16:40:52 +00:00
return m.prefixURL + m.baseURL + "/api/webdav"
2017-06-24 11:12:15 +00:00
}
// SetPrefixURL updates the prefixURL of a File
// Manager object.
2017-06-27 13:49:46 +00:00
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.
2017-06-25 08:11:25 +00:00
func (m *FileManager) SetBaseURL(url string) {
url = strings.TrimPrefix(url, "/")
url = strings.TrimSuffix(url, "/")
url = "/" + url
m.baseURL = strings.TrimSuffix(url, "/")
2017-06-25 08:11:25 +00:00
}
2017-06-25 10:21:42 +00:00
// SetScope updates a user scope and its virtual file system.
2017-06-25 10:40:23 +00:00
// If the user string is blank, it will change the base scope.
func (m *FileManager) SetScope(scope string, username string) error {
2017-06-25 14:24:16 +00:00
var u *User
2017-06-25 10:40:23 +00:00
if username == "" {
2017-06-25 14:24:16 +00:00
u = m.User
2017-06-25 10:40:23 +00:00
} else {
var ok bool
u, ok = m.Users[username]
if !ok {
return errors.New("Inexistent user")
}
}
2017-07-02 16:40:52 +00:00
u.Scope = strings.TrimSuffix(scope, "/")
u.fileSystem = webdav.Dir(u.Scope)
2017-06-25 10:40:23 +00:00
return nil
}
2017-06-25 11:53:49 +00:00
// NewUser creates a new user on a File Manager struct
// which inherits its configuration from the main user.
func (m *FileManager) NewUser(username string) error {
if _, ok := m.Users[username]; ok {
return ErrDuplicated
}
2017-06-25 14:24:16 +00:00
m.Users[username] = &User{
fileSystem: m.User.fileSystem,
2017-07-02 16:40:52 +00:00
Scope: m.User.Scope,
2017-06-25 14:24:16 +00:00
Rules: m.User.Rules,
AllowNew: m.User.AllowNew,
AllowEdit: m.User.AllowEdit,
AllowCommands: m.User.AllowCommands,
Commands: m.User.Commands,
2017-06-25 11:53:49 +00:00
}
2017-06-25 10:40:23 +00:00
2017-06-25 11:53:49 +00:00
return nil
2017-06-25 08:11:25 +00:00
}
2017-06-27 08:28:29 +00:00
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
2017-06-27 14:35:39 +00:00
// TODO: Handle errors here and make it compatible with http.Handler
return serveHTTP(&requestContext{
fm: m,
us: nil,
fi: nil,
}, w, r)
2017-06-27 08:28:29 +00:00
}
2017-06-24 11:12:15 +00:00
// Allowed checks if the user has permission to access a directory/file.
2017-06-25 14:24:16 +00:00
func (u User) Allowed(url string) bool {
2017-06-24 11:12:15 +00:00
var rule *Rule
i := len(u.Rules) - 1
for i >= 0 {
rule = u.Rules[i]
if rule.Regex {
if rule.Regexp.MatchString(url) {
return rule.Allow
}
} else if strings.HasPrefix(url, rule.Path) {
return rule.Allow
}
i--
}
return true
}