filebrowser/filemanager.go

141 lines
3.6 KiB
Go
Raw Normal View History

//go:generate go get github.com/jteeuwen/go-bindata
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
2016-06-10 21:18:44 +00:00
//go:generate go-bindata -debug -pkg filemanager -prefix "assets" -o binary.go assets/...
2016-06-10 21:19:26 +00:00
// TODO: remove debug from the comment
// 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"
"os"
"strings"
2016-06-10 13:36:43 +00:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// 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 {
Next httpserver.Handler
Configs []Config
IgnoreIndexes bool
2016-06-10 13:36:43 +00:00
}
// Config is a configuration for browsing in a particular path.
type Config struct {
2016-06-10 19:54:19 +00:00
PathScope string
Root http.FileSystem
BaseURL string
StyleSheet string
Variables interface{}
}
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
// If so, control is handed over to ServeListing.
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
2016-06-11 09:08:33 +00:00
var c *Config
var file *InfoRequest
2016-06-10 21:44:33 +00:00
2016-06-11 09:08:33 +00:00
// Check if there is a FileManager configuration to match the path
for i := range f.Configs {
2016-06-10 19:54:19 +00:00
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
2016-06-11 09:08:33 +00:00
c = &f.Configs[i]
2016-06-10 19:54:19 +00:00
2016-06-11 09:08:33 +00:00
// Serve assets
if httpserver.Path(r.URL.Path).Matches(c.BaseURL + "/_filemanagerinternal") {
return ServeAssets(w, r, c)
2016-06-10 21:44:33 +00:00
}
2016-06-11 09:08:33 +00:00
// Gets the file path to be used within c.Root
filepath := strings.Replace(r.URL.Path, c.BaseURL, "", 1)
if r.Method != http.MethodPost {
file = GetFileInfo(filepath, c)
if file.Err != nil {
defer file.File.Close()
return file.Code, file.Err
2016-06-10 19:54:19 +00:00
}
2016-06-11 09:08:33 +00:00
if file.Info.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
return 0, nil
2016-06-10 19:54:19 +00:00
}
}
2016-06-11 09:08:33 +00:00
// Route the request depending on the HTTP Method
2016-06-10 19:54:19 +00:00
switch r.Method {
2016-06-11 09:08:33 +00:00
case http.MethodGet:
// Read and show directory or file
if file.Info.IsDir() {
return f.ServeListing(w, r, file.File, c)
}
2016-06-11 13:32:50 +00:00
return f.ServeSingleFile(w, r, file, c)
2016-06-11 09:08:33 +00:00
case http.MethodPost:
// Create new file or directory
return http.StatusOK, nil
case http.MethodDelete:
// Delete a file or a directory
return Delete(filepath, file.Info)
case http.MethodPut:
// Update/Modify a directory or file
return http.StatusOK, nil
case http.MethodPatch:
// Rename a file or directory
return http.StatusOK, nil
2016-06-10 19:54:19 +00:00
default:
2016-06-11 09:08:33 +00:00
return http.StatusNotImplemented, nil
2016-06-10 19:54:19 +00:00
}
}
}
2016-06-10 19:54:19 +00:00
return f.Next.ServeHTTP(w, r)
}
2016-06-11 09:08:33 +00:00
// InfoRequest is the information given by a GetFileInfo function
type InfoRequest struct {
Info os.FileInfo
File http.File
2016-06-11 13:32:50 +00:00
Path string
2016-06-11 09:08:33 +00:00
Code int
Err error
}
2016-06-11 09:08:33 +00:00
// GetFileInfo gets the file information and, in case of error, returns the
// respective HTTP error code
func GetFileInfo(path string, c *Config) *InfoRequest {
2016-06-11 13:32:50 +00:00
request := &InfoRequest{Path: path}
2016-06-11 09:08:33 +00:00
request.File, request.Err = c.Root.Open(path)
if request.Err != nil {
switch {
2016-06-11 09:08:33 +00:00
case os.IsPermission(request.Err):
request.Code = http.StatusForbidden
case os.IsExist(request.Err):
request.Code = http.StatusNotFound
default:
2016-06-11 09:08:33 +00:00
request.Code = http.StatusInternalServerError
}
2016-06-11 09:08:33 +00:00
return request
}
2016-06-11 09:08:33 +00:00
request.Info, request.Err = request.File.Stat()
2016-06-11 09:08:33 +00:00
if request.Err != nil {
switch {
case os.IsPermission(request.Err):
request.Code = http.StatusForbidden
case os.IsExist(request.Err):
request.Code = http.StatusGone
default:
request.Code = http.StatusInternalServerError
}
}
2016-06-11 09:08:33 +00:00
return request
}