filebrowser/filemanager.go

176 lines
4.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-25 20:57:10 +00:00
//go:generate go-bindata -debug -pkg assets -prefix "assets" -o internal/assets/binary.go assets/...
// 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 (
2016-06-11 20:46:28 +00:00
"io"
2016-06-23 08:15:00 +00:00
"log"
2016-06-11 20:46:28 +00:00
"mime/multipart"
2016-06-10 13:36:43 +00:00
"net/http"
"os"
2016-06-11 20:20:47 +00:00
"path/filepath"
"strings"
2016-06-10 13:36:43 +00:00
2016-06-25 20:57:10 +00:00
a "github.com/hacdias/caddy-filemanager/internal/assets"
"github.com/hacdias/caddy-filemanager/internal/config"
"github.com/hacdias/caddy-filemanager/internal/file"
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 {
2016-06-11 20:20:47 +00:00
Next httpserver.Handler
2016-06-25 20:57:10 +00:00
Configs []config.Config
}
// 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) {
2016-06-11 20:20:47 +00:00
var (
2016-06-25 20:57:10 +00:00
c *config.Config
fi *file.Info
2016-06-11 20:20:47 +00:00
code int
err error
assets bool
)
2016-06-10 21:44:33 +00:00
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-25 20:57:10 +00:00
assets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + a.BaseURL)
2016-06-10 19:54:19 +00:00
2016-06-11 20:20:47 +00:00
if r.Method != http.MethodPost && !assets {
fi, code, err = GetFileInfo(r.URL, c)
if err != nil {
return code, err
2016-06-10 19:54:19 +00:00
}
2016-06-11 09:08:33 +00:00
2016-06-11 20:20:47 +00:00
if fi.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
2016-06-11 09:08:33 +00:00
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
2016-06-11 20:20:47 +00:00
if assets {
2016-06-25 20:57:10 +00:00
return a.ServeAssets(w, r, c)
2016-06-11 09:08:33 +00:00
}
2016-06-11 20:20:47 +00:00
2016-06-11 22:01:24 +00:00
if !fi.IsDir {
query := r.URL.Query()
if val, ok := query["raw"]; ok && val[0] == "true" {
2016-06-14 19:33:59 +00:00
return fi.ServeRawFile(w, r, c)
2016-06-11 22:01:24 +00:00
}
if val, ok := query["download"]; ok && val[0] == "true" {
w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name)
2016-06-14 19:33:59 +00:00
return fi.ServeRawFile(w, r, c)
2016-06-11 22:01:24 +00:00
}
2016-06-11 21:07:55 +00:00
}
2016-06-11 21:34:00 +00:00
return fi.ServeAsHTML(w, r, c)
2016-06-11 09:08:33 +00:00
case http.MethodPost:
2016-06-11 20:46:28 +00:00
// Upload a new file
if r.Header.Get("Upload") == "true" {
return Upload(w, r, c)
}
return NewFolder(w, r, c)
2016-06-11 09:08:33 +00:00
case http.MethodDelete:
// Delete a file or a directory
2016-06-11 20:20:47 +00:00
return fi.Delete()
2016-06-11 09:08:33 +00:00
case http.MethodPatch:
// Rename a file or directory
2016-06-11 20:34:38 +00:00
return fi.Rename(w, r)
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-11 20:20:47 +00:00
2016-06-10 19:54:19 +00:00
return f.Next.ServeHTTP(w, r)
}
2016-06-11 20:20:47 +00:00
// ErrorToHTTPCode gets the respective HTTP code for an error
func ErrorToHTTPCode(err error) int {
switch {
case os.IsPermission(err):
return http.StatusForbidden
2016-06-24 12:17:55 +00:00
case os.IsNotExist(err):
return http.StatusNotFound
2016-06-11 20:20:47 +00:00
case os.IsExist(err):
return http.StatusGone
default:
return http.StatusInternalServerError
}
}
2016-06-11 20:46:28 +00:00
// Upload is used to handle the upload requests to the server
2016-06-25 20:57:10 +00:00
func Upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-06-11 20:46:28 +00:00
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
2016-06-23 08:15:00 +00:00
log.Println(err)
2016-06-11 20:46:28 +00:00
return http.StatusInternalServerError, err
}
// For each file header in the multipart form
2016-06-23 08:15:00 +00:00
for _, headers := range r.MultipartForm.File {
2016-06-11 20:46:28 +00:00
// Handle each file
2016-06-23 08:15:00 +00:00
for _, header := range headers {
2016-06-11 20:46:28 +00:00
// Open the first file
2016-06-23 08:15:00 +00:00
var src multipart.File
if src, err = header.Open(); nil != err {
2016-06-11 20:46:28 +00:00
return http.StatusInternalServerError, err
}
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
2016-06-23 08:15:00 +00:00
filename = filename + header.Filename
2016-06-11 20:46:28 +00:00
filename = filepath.Clean(filename)
// Create the file
2016-06-23 08:15:00 +00:00
var dst *os.File
if dst, err = os.Create(filename); nil != err {
2016-06-11 20:46:28 +00:00
if os.IsExist(err) {
return http.StatusConflict, err
}
return http.StatusInternalServerError, err
}
// Copy the file content
2016-06-23 08:15:00 +00:00
if _, err = io.Copy(dst, src); nil != err {
2016-06-11 20:46:28 +00:00
return http.StatusInternalServerError, err
}
2016-06-23 08:15:00 +00:00
defer dst.Close()
2016-06-11 20:46:28 +00:00
}
}
return http.StatusOK, nil
}
// NewFolder makes a new directory
2016-06-25 20:57:10 +00:00
func NewFolder(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-06-11 20:46:28 +00:00
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
path = filepath.Clean(path)
err := os.MkdirAll(path, 0755)
if err != nil {
switch {
case os.IsPermission(err):
return http.StatusForbidden, err
case os.IsExist(err):
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
}
return http.StatusCreated, nil
}