filebrowser/http/data.go

83 lines
1.8 KiB
Go
Raw Normal View History

package http
import (
"log"
"net/http"
"strconv"
"github.com/tomasen/realip"
2020-11-20 18:51:28 +08:00
"github.com/filebrowser/filebrowser/v2/rules"
"github.com/filebrowser/filebrowser/v2/runner"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users"
)
type handleFunc func(w http.ResponseWriter, r *http.Request, d *data) (int, error)
type data struct {
*runner.Runner
settings *settings.Settings
server *settings.Server
store *storage.Storage
user *users.User
raw interface{}
}
// Check implements rules.Checker.
func (d *data) Check(path string) bool {
2020-11-20 18:51:28 +08:00
if d.user.HideDotfiles && rules.MatchHidden(path) {
return false
}
2020-06-23 00:46:22 +08:00
allow := true
for _, rule := range d.settings.Rules {
if rule.Matches(path) {
2020-06-23 00:46:22 +08:00
allow = rule.Allow
}
}
2020-06-23 00:46:22 +08:00
for _, rule := range d.user.Rules {
if rule.Matches(path) {
2020-06-23 00:46:22 +08:00
allow = rule.Allow
}
}
2020-06-23 00:46:22 +08:00
return allow
}
2020-06-01 07:12:36 +08:00
func handle(fn handleFunc, prefix string, store *storage.Storage, server *settings.Server) http.Handler {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for k, v := range globalHeaders {
w.Header().Set(k, v)
}
2021-04-15 20:28:19 +08:00
2020-06-01 07:12:36 +08:00
settings, err := store.Settings.Get()
if err != nil {
log.Fatalf("ERROR: couldn't get settings: %v\n", err)
return
}
status, err := fn(w, r, &data{
2020-10-01 22:45:24 +08:00
Runner: &runner.Runner{Enabled: server.EnableExec, Settings: settings},
2020-06-01 07:12:36 +08:00
store: store,
settings: settings,
server: server,
})
if status >= 400 || err != nil {
clientIP := realip.FromRequest(r)
log.Printf("%s: %v %s %v", r.URL.Path, status, clientIP, err)
}
2021-12-21 06:16:35 +08:00
if status != 0 {
txt := http.StatusText(status)
http.Error(w, strconv.Itoa(status)+" "+txt, status)
return
}
})
2021-03-18 01:54:25 +08:00
return stripPrefix(prefix, handler)
}