mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
Start refactoring to make code cleaner
This commit is contained in:
parent
d2b7b106ef
commit
381cac79b2
39
assets.go
39
assets.go
@ -1,39 +0,0 @@
|
||||
package filemanager
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ServeAssets redirects the request for the respective method
|
||||
func ServeAssets(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
return serveAssetsGET(w, r, c)
|
||||
default:
|
||||
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
|
||||
}
|
||||
}
|
||||
|
||||
// serveAssetsGET provides the method for GET request on Assets page
|
||||
func serveAssetsGET(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
// gets the filename to be used with Assets function
|
||||
filename := strings.Replace(r.URL.Path, c.BaseURL+"/_filemanagerinternal", "public", 1)
|
||||
file, err := Asset(filename)
|
||||
if err != nil {
|
||||
return 404, nil
|
||||
}
|
||||
|
||||
// Get the file extension ant its mime type
|
||||
extension := filepath.Ext(filename)
|
||||
mediatype := mime.TypeByExtension(extension)
|
||||
|
||||
// Write the header with the Content-Type and write the file
|
||||
// content to the buffer
|
||||
w.Header().Set("Content-Type", mediatype)
|
||||
w.Write(file)
|
||||
return 200, nil
|
||||
}
|
31
delete.go
31
delete.go
@ -1,31 +0,0 @@
|
||||
package filemanager
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Delete handles the delete requests
|
||||
func Delete(path string, info os.FileInfo) (int, error) {
|
||||
var err error
|
||||
// If it's dir, remove all of the content inside
|
||||
if info.IsDir() {
|
||||
err = os.RemoveAll(path)
|
||||
} else {
|
||||
err = os.Remove(path)
|
||||
}
|
||||
|
||||
// Check for errors
|
||||
if err != nil {
|
||||
switch {
|
||||
case os.IsPermission(err):
|
||||
return http.StatusForbidden, err
|
||||
case os.IsExist(err):
|
||||
return http.StatusGone, err
|
||||
default:
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
54
fileinfo.go
54
fileinfo.go
@ -1,13 +1,17 @@
|
||||
package filemanager
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
// FileInfo is the info about a particular file or directory
|
||||
// FileInfo is the information about a particular file or directory
|
||||
type FileInfo struct {
|
||||
IsDir bool
|
||||
Name string
|
||||
@ -15,6 +19,36 @@ type FileInfo struct {
|
||||
URL string
|
||||
ModTime time.Time
|
||||
Mode os.FileMode
|
||||
Path string
|
||||
}
|
||||
|
||||
// GetFileInfo gets the file information and, in case of error, returns the
|
||||
// respective HTTP error code
|
||||
func GetFileInfo(url *url.URL, c *Config) (*FileInfo, int, error) {
|
||||
var err error
|
||||
|
||||
path := strings.Replace(url.Path, c.BaseURL, c.PathScope, 1)
|
||||
path = filepath.Clean(path)
|
||||
|
||||
file := &FileInfo{Path: path}
|
||||
f, err := c.Root.Open(path)
|
||||
if err != nil {
|
||||
return file, ErrorToHTTPCode(err), err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return file, ErrorToHTTPCode(err), err
|
||||
}
|
||||
|
||||
file.IsDir = info.IsDir()
|
||||
file.ModTime = info.ModTime()
|
||||
file.Name = info.Name()
|
||||
file.Size = info.Size()
|
||||
file.URL = url.Path
|
||||
|
||||
return file, 0, nil
|
||||
}
|
||||
|
||||
// HumanSize returns the size of the file as a human-readable string
|
||||
@ -27,3 +61,21 @@ func (fi FileInfo) HumanSize() string {
|
||||
func (fi FileInfo) HumanModTime(format string) string {
|
||||
return fi.ModTime.Format(format)
|
||||
}
|
||||
|
||||
// Delete handles the delete requests
|
||||
func (fi FileInfo) Delete() (int, error) {
|
||||
var err error
|
||||
|
||||
// If it's a directory remove all the contents inside
|
||||
if fi.IsDir {
|
||||
err = os.RemoveAll(fi.Path)
|
||||
} else {
|
||||
err = os.Remove(fi.Path)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return ErrorToHTTPCode(err), err
|
||||
}
|
||||
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
117
filemanager.go
117
filemanager.go
@ -9,57 +9,46 @@
|
||||
package filemanager
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
const assetsURL = "/_filemanagerinternal"
|
||||
|
||||
// FileManager is an http.Handler that can show a file listing when
|
||||
// directories in the given paths are specified.
|
||||
type FileManager struct {
|
||||
Next httpserver.Handler
|
||||
Configs []Config
|
||||
IgnoreIndexes bool
|
||||
}
|
||||
|
||||
// Config is a configuration for browsing in a particular path.
|
||||
type Config struct {
|
||||
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) {
|
||||
var c *Config
|
||||
var file *InfoRequest
|
||||
var (
|
||||
c *Config
|
||||
fi *FileInfo
|
||||
code int
|
||||
err error
|
||||
assets bool
|
||||
)
|
||||
|
||||
// Check if there is a FileManager configuration to match the path
|
||||
for i := range f.Configs {
|
||||
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
||||
c = &f.Configs[i]
|
||||
assets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assetsURL)
|
||||
|
||||
// Serve assets
|
||||
if httpserver.Path(r.URL.Path).Matches(c.BaseURL + "/_filemanagerinternal") {
|
||||
return ServeAssets(w, r, c)
|
||||
if r.Method != http.MethodPost && !assets {
|
||||
fi, code, err = GetFileInfo(r.URL, c)
|
||||
if err != nil {
|
||||
return code, err
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
if file.Info.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
|
||||
if fi.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
||||
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||
return 0, nil
|
||||
}
|
||||
@ -69,17 +58,21 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// Read and show directory or file
|
||||
if file.Info.IsDir() {
|
||||
if assets {
|
||||
return ServeAssets(w, r, c)
|
||||
}
|
||||
|
||||
/* if file.Info.IsDir() {
|
||||
return f.ServeListing(w, r, file.File, c)
|
||||
}
|
||||
return f.ServeSingleFile(w, r, file, c)
|
||||
return f.ServeSingleFile(w, r, file, c) */
|
||||
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)
|
||||
return fi.Delete()
|
||||
case http.MethodPut:
|
||||
// Update/Modify a directory or file
|
||||
|
||||
@ -93,48 +86,38 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return f.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// InfoRequest is the information given by a GetFileInfo function
|
||||
type InfoRequest struct {
|
||||
Info os.FileInfo
|
||||
File http.File
|
||||
Path string
|
||||
Code int
|
||||
Err error
|
||||
// ErrorToHTTPCode gets the respective HTTP code for an error
|
||||
func ErrorToHTTPCode(err error) int {
|
||||
switch {
|
||||
case os.IsPermission(err):
|
||||
return http.StatusForbidden
|
||||
case os.IsExist(err):
|
||||
return http.StatusGone
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileInfo gets the file information and, in case of error, returns the
|
||||
// respective HTTP error code
|
||||
func GetFileInfo(path string, c *Config) *InfoRequest {
|
||||
request := &InfoRequest{Path: path}
|
||||
request.File, request.Err = c.Root.Open(path)
|
||||
if request.Err != nil {
|
||||
switch {
|
||||
case os.IsPermission(request.Err):
|
||||
request.Code = http.StatusForbidden
|
||||
case os.IsExist(request.Err):
|
||||
request.Code = http.StatusNotFound
|
||||
default:
|
||||
request.Code = http.StatusInternalServerError
|
||||
// ServeAssets provides the needed assets for the front-end
|
||||
func ServeAssets(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||
// gets the filename to be used with Assets function
|
||||
filename := strings.Replace(r.URL.Path, c.BaseURL+assetsURL, "public", 1)
|
||||
file, err := Asset(filename)
|
||||
if err != nil {
|
||||
return http.StatusNotFound, nil
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
// Get the file extension and its mimetype
|
||||
extension := filepath.Ext(filename)
|
||||
mediatype := mime.TypeByExtension(extension)
|
||||
|
||||
request.Info, request.Err = request.File.Stat()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
return request
|
||||
// Write the header with the Content-Type and write the file
|
||||
// content to the buffer
|
||||
w.Header().Set("Content-Type", mediatype)
|
||||
w.Write(file)
|
||||
return 200, nil
|
||||
}
|
||||
|
50
setup.go
50
setup.go
@ -16,58 +16,57 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// setup configures a new Browse middleware instance.
|
||||
// setup configures a new FileManager middleware instance.
|
||||
func setup(c *caddy.Controller) error {
|
||||
configs, err := fileManagerParse(c)
|
||||
configs, err := parseConfiguration(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f := FileManager{
|
||||
Configs: configs,
|
||||
IgnoreIndexes: false,
|
||||
}
|
||||
|
||||
httpserver.GetConfig(c.Key).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
f.Next = next
|
||||
return f
|
||||
return FileManager{Configs: configs, Next: next}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func fileManagerParse(c *caddy.Controller) ([]Config, error) {
|
||||
// Config is a configuration for browsing in a particualr path.
|
||||
type Config struct {
|
||||
PathScope string
|
||||
Root http.FileSystem
|
||||
BaseURL string
|
||||
StyleSheet string
|
||||
}
|
||||
|
||||
// parseConfiguration parses the configuration set by the user so it can
|
||||
// be used by the middleware
|
||||
func parseConfiguration(c *caddy.Controller) ([]Config, error) {
|
||||
var configs []Config
|
||||
|
||||
appendCfg := func(fmc Config) error {
|
||||
appendConfig := func(cfg Config) error {
|
||||
for _, c := range configs {
|
||||
if c.PathScope == fmc.PathScope {
|
||||
if c.PathScope == cfg.PathScope {
|
||||
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
|
||||
}
|
||||
}
|
||||
configs = append(configs, fmc)
|
||||
configs = append(configs, cfg)
|
||||
return nil
|
||||
}
|
||||
|
||||
for c.Next() {
|
||||
var fmc = Config{
|
||||
PathScope: ".",
|
||||
BaseURL: "",
|
||||
StyleSheet: "",
|
||||
}
|
||||
|
||||
var cfg = Config{PathScope: "."}
|
||||
for c.NextBlock() {
|
||||
switch c.Val() {
|
||||
case "show":
|
||||
if !c.NextArg() {
|
||||
return configs, c.ArgErr()
|
||||
}
|
||||
fmc.PathScope = c.Val()
|
||||
cfg.PathScope = c.Val()
|
||||
case "on":
|
||||
if !c.NextArg() {
|
||||
return configs, c.ArgErr()
|
||||
}
|
||||
fmc.BaseURL = c.Val()
|
||||
cfg.BaseURL = c.Val()
|
||||
case "styles":
|
||||
if !c.NextArg() {
|
||||
return configs, c.ArgErr()
|
||||
@ -76,15 +75,12 @@ func fileManagerParse(c *caddy.Controller) ([]Config, error) {
|
||||
if err != nil {
|
||||
return configs, err
|
||||
}
|
||||
fmc.StyleSheet = string(tplBytes)
|
||||
cfg.StyleSheet = string(tplBytes)
|
||||
}
|
||||
}
|
||||
|
||||
fmc.Root = http.Dir(fmc.PathScope)
|
||||
|
||||
// Save configuration
|
||||
err := appendCfg(fmc)
|
||||
if err != nil {
|
||||
cfg.Root = http.Dir(cfg.PathScope)
|
||||
if err := appendConfig(cfg); err != nil {
|
||||
return configs, err
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user