Files
filebrowser/filemanager.go
T

33 lines
934 B
Go
Raw Normal View History

2016-06-10 18:27:27 +01: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 14:36:43 +01:00
package filemanager
import (
"net/http"
2017-06-25 20:53:31 +01:00
"github.com/hacdias/filemanager"
2016-06-10 14:36:43 +01:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
2016-06-10 18:27:27 +01:00
// FileManager is an http.Handler that can show a file listing when
// directories in the given paths are specified.
2016-06-10 14:36:43 +01:00
type FileManager struct {
2016-06-11 21:20:47 +01:00
Next httpserver.Handler
2017-06-25 20:53:31 +01:00
Configs []*filemanager.FileManager
2016-06-10 18:27:27 +01:00
}
2016-11-01 13:46:28 +00:00
2016-06-10 18:27:27 +01: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 22:00:26 +01: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 22:00:26 +01:00
}
2017-06-25 20:53:31 +01:00
return f.Configs[i].ServeHTTP(w, r)
2016-06-10 18:27:27 +01:00
}
2016-06-11 21:20:47 +01:00
2016-06-10 20:54:19 +01:00
return f.Next.ServeHTTP(w, r)
2016-06-10 18:27:27 +01:00
}