add user to the front end
parent
87de0ae1be
commit
076245339d
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// BaseURL is the url of the assets
|
// BaseURL is the url of the assets
|
||||||
const BaseURL = "/_filemanagerinternal"
|
const BaseURL = "_filemanagerinternal"
|
||||||
|
|
||||||
// Serve provides the needed assets for the front-end
|
// Serve provides the needed assets for the front-end
|
||||||
func Serve(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func Serve(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||||
|
|
|
@ -67,6 +67,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
var cfg = Config{UserConfig: &UserConfig{}}
|
var cfg = Config{UserConfig: &UserConfig{}}
|
||||||
cfg.PathScope = "."
|
cfg.PathScope = "."
|
||||||
|
cfg.BaseURL = ""
|
||||||
cfg.FrontMatter = "yaml"
|
cfg.FrontMatter = "yaml"
|
||||||
cfg.HugoEnabled = false
|
cfg.HugoEnabled = false
|
||||||
cfg.Users = map[string]*UserConfig{}
|
cfg.Users = map[string]*UserConfig{}
|
||||||
|
@ -99,7 +100,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
|
||||||
}
|
}
|
||||||
cCfg.PathScope = c.Val()
|
cCfg.PathScope = c.Val()
|
||||||
cCfg.PathScope = strings.TrimSuffix(cCfg.PathScope, "/")
|
cCfg.PathScope = strings.TrimSuffix(cCfg.PathScope, "/")
|
||||||
cCfg.Root = http.Dir(cCfg.PathScope)
|
|
||||||
case "styles":
|
case "styles":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
|
@ -200,6 +200,8 @@ func Parse(c *caddy.Controller) ([]Config, error) {
|
||||||
})
|
})
|
||||||
// NEW USER BLOCK?
|
// NEW USER BLOCK?
|
||||||
default:
|
default:
|
||||||
|
cCfg.Root = http.Dir(cCfg.PathScope)
|
||||||
|
|
||||||
val := c.Val()
|
val := c.Val()
|
||||||
// Checks if it's a new user
|
// Checks if it's a new user
|
||||||
if !strings.HasSuffix(val, ":") {
|
if !strings.HasSuffix(val, ":") {
|
||||||
|
@ -230,6 +232,8 @@ func Parse(c *caddy.Controller) ([]Config, error) {
|
||||||
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
|
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
|
||||||
cfg.BaseURL = "/" + cfg.BaseURL
|
cfg.BaseURL = "/" + cfg.BaseURL
|
||||||
|
|
||||||
|
cfg.Root = http.Dir(cfg.PathScope)
|
||||||
|
|
||||||
caddyConf := httpserver.GetConfig(c)
|
caddyConf := httpserver.GetConfig(c)
|
||||||
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
|
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
|
||||||
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)
|
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)
|
||||||
|
@ -238,6 +242,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
|
||||||
if err := appendConfig(cfg); err != nil {
|
if err := appendConfig(cfg); err != nil {
|
||||||
return configs, err
|
return configs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return configs, nil
|
return configs, nil
|
||||||
|
|
|
@ -142,15 +142,15 @@ func (i *Info) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeAsHTML is used to serve single file pages
|
// ServeAsHTML is used to serve single file pages
|
||||||
func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
|
||||||
if i.IsDir {
|
if i.IsDir {
|
||||||
return i.serveListing(w, r, c)
|
return i.serveListing(w, r, c, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.serveSingleFile(w, r, c)
|
return i.serveSingleFile(w, r, c, u)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
|
||||||
err := i.GetExtendedInfo()
|
err := i.GetExtendedInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.ToHTTPCode(err), err
|
return errors.ToHTTPCode(err), err
|
||||||
|
@ -166,6 +166,7 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
|
||||||
Path: i.RootPath,
|
Path: i.RootPath,
|
||||||
IsDir: false,
|
IsDir: false,
|
||||||
Data: i,
|
Data: i,
|
||||||
|
User: u,
|
||||||
Config: c,
|
Config: c,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -184,7 +185,7 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
|
||||||
return page.PrintAsHTML(w, "single")
|
return page.PrintAsHTML(w, "single")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
file, err := c.Root.Open(i.RootPath)
|
file, err := c.Root.Open(i.RootPath)
|
||||||
|
@ -245,6 +246,7 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co
|
||||||
Name: listing.Name,
|
Name: listing.Name,
|
||||||
Path: i.RootPath,
|
Path: i.RootPath,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
|
User: u,
|
||||||
Config: c,
|
Config: c,
|
||||||
Data: listing,
|
Data: listing,
|
||||||
},
|
},
|
||||||
|
|
|
@ -44,22 +44,18 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
||||||
user *config.UserConfig
|
user *config.UserConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
// Set the current User
|
|
||||||
username, _, ok := r.BasicAuth()
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
user = c.UserConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := c.Users[username]; ok {
|
|
||||||
user = c.Users[username]
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range f.Configs {
|
for i := range f.Configs {
|
||||||
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
||||||
c = &f.Configs[i]
|
c = &f.Configs[i]
|
||||||
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
|
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
|
||||||
|
|
||||||
|
// Set the current User
|
||||||
|
username, _, _ := r.BasicAuth()
|
||||||
|
|
||||||
|
if _, ok := c.Users[username]; ok {
|
||||||
|
user = c.Users[username]
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method != http.MethodPost && !serveAssets {
|
if r.Method != http.MethodPost && !serveAssets {
|
||||||
fi, code, err = directory.GetInfo(r.URL, c)
|
fi, code, err = directory.GetInfo(r.URL, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,7 +101,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
code, err := fi.ServeAsHTML(w, r, c)
|
code, err := fi.ServeAsHTML(w, r, c, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.PrintHTML(w, code, err)
|
return errors.PrintHTML(w, code, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ type Info struct {
|
||||||
Name string
|
Name string
|
||||||
Path string
|
Path string
|
||||||
IsDir bool
|
IsDir bool
|
||||||
|
User *config.UserConfig
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue