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
|
package filemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"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 {
|
type FileInfo struct {
|
||||||
IsDir bool
|
IsDir bool
|
||||||
Name string
|
Name string
|
||||||
@ -15,6 +19,36 @@ type FileInfo struct {
|
|||||||
URL string
|
URL string
|
||||||
ModTime time.Time
|
ModTime time.Time
|
||||||
Mode os.FileMode
|
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
|
// 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 {
|
func (fi FileInfo) HumanModTime(format string) string {
|
||||||
return fi.ModTime.Format(format)
|
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
|
||||||
|
}
|
||||||
|
123
filemanager.go
123
filemanager.go
@ -9,57 +9,46 @@
|
|||||||
package filemanager
|
package filemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const assetsURL = "/_filemanagerinternal"
|
||||||
|
|
||||||
// FileManager is an http.Handler that can show a file listing when
|
// FileManager is an http.Handler that can show a file listing when
|
||||||
// directories in the given paths are specified.
|
// directories in the given paths are specified.
|
||||||
type FileManager struct {
|
type FileManager struct {
|
||||||
Next httpserver.Handler
|
Next httpserver.Handler
|
||||||
Configs []Config
|
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.
|
// 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) {
|
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
var c *Config
|
var (
|
||||||
var file *InfoRequest
|
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 {
|
for i := range f.Configs {
|
||||||
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
||||||
c = &f.Configs[i]
|
c = &f.Configs[i]
|
||||||
|
assets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assetsURL)
|
||||||
|
|
||||||
// Serve assets
|
if r.Method != http.MethodPost && !assets {
|
||||||
if httpserver.Path(r.URL.Path).Matches(c.BaseURL + "/_filemanagerinternal") {
|
fi, code, err = GetFileInfo(r.URL, c)
|
||||||
return ServeAssets(w, r, 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)
|
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@ -69,17 +58,21 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
// Read and show directory or file
|
// Read and show directory or file
|
||||||
if file.Info.IsDir() {
|
if assets {
|
||||||
return f.ServeListing(w, r, file.File, c)
|
return ServeAssets(w, r, c)
|
||||||
}
|
}
|
||||||
return f.ServeSingleFile(w, r, file, c)
|
|
||||||
|
/* if file.Info.IsDir() {
|
||||||
|
return f.ServeListing(w, r, file.File, c)
|
||||||
|
}
|
||||||
|
return f.ServeSingleFile(w, r, file, c) */
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
// Create new file or directory
|
// Create new file or directory
|
||||||
|
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
// Delete a file or a directory
|
// Delete a file or a directory
|
||||||
return Delete(filepath, file.Info)
|
return fi.Delete()
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
// Update/Modify a directory or file
|
// 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)
|
return f.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InfoRequest is the information given by a GetFileInfo function
|
// ErrorToHTTPCode gets the respective HTTP code for an error
|
||||||
type InfoRequest struct {
|
func ErrorToHTTPCode(err error) int {
|
||||||
Info os.FileInfo
|
switch {
|
||||||
File http.File
|
case os.IsPermission(err):
|
||||||
Path string
|
return http.StatusForbidden
|
||||||
Code int
|
case os.IsExist(err):
|
||||||
Err error
|
return http.StatusGone
|
||||||
|
default:
|
||||||
|
return http.StatusInternalServerError
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFileInfo gets the file information and, in case of error, returns the
|
// ServeAssets provides the needed assets for the front-end
|
||||||
// respective HTTP error code
|
func ServeAssets(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
||||||
func GetFileInfo(path string, c *Config) *InfoRequest {
|
// gets the filename to be used with Assets function
|
||||||
request := &InfoRequest{Path: path}
|
filename := strings.Replace(r.URL.Path, c.BaseURL+assetsURL, "public", 1)
|
||||||
request.File, request.Err = c.Root.Open(path)
|
file, err := Asset(filename)
|
||||||
if request.Err != nil {
|
if err != nil {
|
||||||
switch {
|
return http.StatusNotFound, nil
|
||||||
case os.IsPermission(request.Err):
|
|
||||||
request.Code = http.StatusForbidden
|
|
||||||
case os.IsExist(request.Err):
|
|
||||||
request.Code = http.StatusNotFound
|
|
||||||
default:
|
|
||||||
request.Code = http.StatusInternalServerError
|
|
||||||
}
|
|
||||||
|
|
||||||
return request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
request.Info, request.Err = request.File.Stat()
|
// Get the file extension and its mimetype
|
||||||
|
extension := filepath.Ext(filename)
|
||||||
|
mediatype := mime.TypeByExtension(extension)
|
||||||
|
|
||||||
if request.Err != nil {
|
// Write the header with the Content-Type and write the file
|
||||||
switch {
|
// content to the buffer
|
||||||
case os.IsPermission(request.Err):
|
w.Header().Set("Content-Type", mediatype)
|
||||||
request.Code = http.StatusForbidden
|
w.Write(file)
|
||||||
case os.IsExist(request.Err):
|
return 200, nil
|
||||||
request.Code = http.StatusGone
|
|
||||||
default:
|
|
||||||
request.Code = http.StatusInternalServerError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return request
|
|
||||||
}
|
}
|
||||||
|
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 {
|
func setup(c *caddy.Controller) error {
|
||||||
configs, err := fileManagerParse(c)
|
configs, err := parseConfiguration(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f := FileManager{
|
|
||||||
Configs: configs,
|
|
||||||
IgnoreIndexes: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
httpserver.GetConfig(c.Key).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
httpserver.GetConfig(c.Key).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||||
f.Next = next
|
return FileManager{Configs: configs, Next: next}
|
||||||
return f
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
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
|
var configs []Config
|
||||||
|
|
||||||
appendCfg := func(fmc Config) error {
|
appendConfig := func(cfg Config) error {
|
||||||
for _, c := range configs {
|
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)
|
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
configs = append(configs, fmc)
|
configs = append(configs, cfg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
var fmc = Config{
|
var cfg = Config{PathScope: "."}
|
||||||
PathScope: ".",
|
|
||||||
BaseURL: "",
|
|
||||||
StyleSheet: "",
|
|
||||||
}
|
|
||||||
|
|
||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
case "show":
|
case "show":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
}
|
}
|
||||||
fmc.PathScope = c.Val()
|
cfg.PathScope = c.Val()
|
||||||
case "on":
|
case "on":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
}
|
}
|
||||||
fmc.BaseURL = c.Val()
|
cfg.BaseURL = c.Val()
|
||||||
case "styles":
|
case "styles":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
@ -76,15 +75,12 @@ func fileManagerParse(c *caddy.Controller) ([]Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return configs, err
|
return configs, err
|
||||||
}
|
}
|
||||||
fmc.StyleSheet = string(tplBytes)
|
cfg.StyleSheet = string(tplBytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmc.Root = http.Dir(fmc.PathScope)
|
cfg.Root = http.Dir(cfg.PathScope)
|
||||||
|
if err := appendConfig(cfg); err != nil {
|
||||||
// Save configuration
|
|
||||||
err := appendCfg(fmc)
|
|
||||||
if err != nil {
|
|
||||||
return configs, err
|
return configs, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user