2016-06-10 17:27:27 +00:00
|
|
|
//go:generate go get github.com/jteeuwen/go-bindata
|
|
|
|
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
2016-06-28 09:24:02 +00:00
|
|
|
//go:generate go-bindata -debug -pkg assets -prefix "assets" -o assets/binary.go assets/...
|
2016-06-10 17:27:27 +00: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 13:36:43 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2016-06-28 09:24:02 +00:00
|
|
|
"io"
|
2016-06-28 20:28:39 +00:00
|
|
|
"io/ioutil"
|
2016-06-28 09:24:02 +00:00
|
|
|
"log"
|
|
|
|
"mime/multipart"
|
2016-06-10 13:36:43 +00:00
|
|
|
"net/http"
|
2016-06-28 09:24:02 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2016-06-10 17:27:27 +00:00
|
|
|
"strings"
|
2016-06-10 13:36:43 +00:00
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/assets"
|
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
|
|
|
"github.com/hacdias/caddy-filemanager/directory"
|
|
|
|
"github.com/hacdias/caddy-filemanager/page"
|
2016-06-10 13:36:43 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// 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
|
2016-06-10 17:27:27 +00: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) {
|
2016-06-11 20:20:47 +00:00
|
|
|
var (
|
2016-06-28 09:24:02 +00:00
|
|
|
c *config.Config
|
|
|
|
fi *directory.Info
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
serveAssets bool
|
2016-06-11 20:20:47 +00:00
|
|
|
)
|
2016-06-10 21:44:33 +00:00
|
|
|
|
2016-06-10 17:27:27 +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-28 09:24:02 +00:00
|
|
|
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
|
2016-06-10 19:54:19 +00:00
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
if r.Method != http.MethodPost && !serveAssets {
|
|
|
|
fi, code, err = directory.GetInfo(r.URL, c)
|
2016-06-11 20:20:47 +00:00
|
|
|
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-10 17:27:27 +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-28 09:24:02 +00:00
|
|
|
if serveAssets {
|
|
|
|
return assets.Serve(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-27 12:22:07 +00:00
|
|
|
case http.MethodPut:
|
|
|
|
if fi.IsDir {
|
|
|
|
return http.StatusNotAcceptable, nil
|
|
|
|
}
|
|
|
|
// Update a file
|
|
|
|
return fi.Update(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" {
|
2016-06-28 09:24:02 +00:00
|
|
|
return upload(w, r, c)
|
2016-06-11 20:46:28 +00:00
|
|
|
}
|
2016-06-26 15:52:15 +00:00
|
|
|
// Search and git commands
|
|
|
|
if r.Header.Get("Search") == "true" {
|
2016-06-26 17:30:08 +00:00
|
|
|
// TODO: search commands
|
|
|
|
}
|
|
|
|
// VCS commands
|
|
|
|
if r.Header.Get("Command") != "" {
|
2016-06-26 22:03:27 +00:00
|
|
|
// TODO: not implemented on frontend
|
2016-06-28 09:24:02 +00:00
|
|
|
vcsCommand(w, r, c)
|
2016-06-26 15:52:15 +00:00
|
|
|
}
|
|
|
|
// Creates a new folder
|
|
|
|
// TODO: not implemented on frontend
|
2016-06-28 09:24:02 +00:00
|
|
|
return newDirectory(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-10 17:27:27 +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-10 17:27:27 +00:00
|
|
|
}
|
2016-06-28 09:24:02 +00:00
|
|
|
|
|
|
|
// upload is used to handle the upload requests to the server
|
|
|
|
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
|
|
|
// Parse the multipart form in the request
|
|
|
|
err := r.ParseMultipartForm(100000)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each file header in the multipart form
|
|
|
|
for _, headers := range r.MultipartForm.File {
|
|
|
|
// Handle each file
|
|
|
|
for _, header := range headers {
|
|
|
|
// Open the first file
|
|
|
|
var src multipart.File
|
|
|
|
if src, err = header.Open(); nil != err {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
|
|
|
|
filename = filename + header.Filename
|
|
|
|
filename = filepath.Clean(filename)
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
var dst *os.File
|
|
|
|
if dst, err = os.Create(filename); nil != err {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return http.StatusConflict, err
|
|
|
|
}
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the file content
|
|
|
|
if _, err = io.Copy(dst, src); nil != err {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer dst.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDirectory makes a new directory
|
|
|
|
func newDirectory(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
2016-06-28 20:28:39 +00:00
|
|
|
filename := r.Header.Get("Filename")
|
|
|
|
|
|
|
|
if filename == "" {
|
|
|
|
return http.StatusBadRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1) + filename
|
2016-06-28 09:24:02 +00:00
|
|
|
path = filepath.Clean(path)
|
2016-06-28 20:28:39 +00:00
|
|
|
extension := filepath.Ext(path)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if extension == "" {
|
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
} else {
|
|
|
|
err = ioutil.WriteFile(path, []byte(""), 0755)
|
|
|
|
}
|
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// vcsCommand handles the requests for VCS related commands: git, svn and mercurial
|
|
|
|
func vcsCommand(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
|
|
|
command := strings.Split(r.Header.Get("command"), " ")
|
|
|
|
|
|
|
|
// Check if the command is for git, mercurial or svn
|
|
|
|
if command[0] != "git" && command[0] != "hg" && command[0] != "svn" {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the program is talled is installed on the computer
|
|
|
|
if _, err := exec.LookPath(command[0]); err != nil {
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(command[0], command[1:len(command)]...)
|
|
|
|
cmd.Dir = c.PathScope
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page := &page.Page{Info: &page.Info{Data: string(output)}}
|
|
|
|
return page.PrintAsJSON(w)
|
|
|
|
}
|