2016-06-10 17:27:27 +00:00
|
|
|
// Package filemanager provides middleware for managing files in a directory
|
|
|
|
// when directory path is requested instead of a specific file. Based on browse
|
|
|
|
// middleware.
|
2016-06-10 13:36:43 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2017-06-25 19:53:31 +00:00
|
|
|
"github.com/hacdias/filemanager"
|
2016-06-10 13:36:43 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// FileManager is an http.Handler that can show a file listing when
|
|
|
|
// directories in the given paths are specified.
|
2016-06-10 13:36:43 +00:00
|
|
|
type FileManager struct {
|
2016-06-11 20:20:47 +00:00
|
|
|
Next httpserver.Handler
|
2017-06-25 19:53:31 +00:00
|
|
|
Configs []*filemanager.FileManager
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-11-01 13:46:28 +00:00
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
|
|
|
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
for i := range f.Configs {
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks if this Path should be handled by File Manager.
|
|
|
|
if !httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
2017-01-22 09:59:56 +00:00
|
|
|
continue
|
2016-10-18 21:00:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 19:53:31 +00:00
|
|
|
return f.Configs[i].ServeHTTP(w, r)
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-06-11 20:20:47 +00:00
|
|
|
|
2016-06-10 19:54:19 +00:00
|
|
|
return f.Next.ServeHTTP(w, r)
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|