mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
67dbf88eb6
Former-commit-id: c463c6e5708b2cd10e7de37285cddf0c4898b59b [formerly 615fbb71576801762e831e00489c30bff189c7d2] [formerly cdd9f708fac1163bb79e619368ddd05e4b581be3 [formerly e4d345b7e5
]]
Former-commit-id: cfb19f435c5d08cbb38e50ba970fc2d9474ffb0c [formerly 6e1aac15e1da1c06e41d87dafa262332b018d701]
Former-commit-id: 78cebd321e5a840388e6d4eca09e2357469ec546
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// Package filemanager provides middleware for managing files in a directory
|
|
// when directory path is requested instead of a specific file. Based on browse
|
|
// middleware.
|
|
package filemanager
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/hacdias/filemanager"
|
|
"github.com/hacdias/filemanager/caddy/parser"
|
|
h "github.com/hacdias/filemanager/http"
|
|
"github.com/mholt/caddy"
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterPlugin("filemanager", caddy.Plugin{
|
|
ServerType: "http",
|
|
Action: setup,
|
|
})
|
|
}
|
|
|
|
type plugin struct {
|
|
Next httpserver.Handler
|
|
Configs []*filemanager.FileManager
|
|
}
|
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
|
func (f plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
for i := range f.Configs {
|
|
// Checks if this Path should be handled by File Manager.
|
|
if !httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
|
continue
|
|
}
|
|
|
|
h.Handler(f.Configs[i]).ServeHTTP(w, r)
|
|
return 0, nil
|
|
}
|
|
|
|
return f.Next.ServeHTTP(w, r)
|
|
}
|
|
|
|
// setup configures a new FileManager middleware instance.
|
|
func setup(c *caddy.Controller) error {
|
|
configs, err := parser.Parse(c, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
|
return plugin{Configs: configs, Next: next}
|
|
})
|
|
|
|
return nil
|
|
}
|