filebrowser/directory/file.go

336 lines
6.9 KiB
Go
Raw Normal View History

2016-06-28 09:24:02 +00:00
package directory
2016-06-10 19:54:19 +00:00
import (
2016-06-11 22:01:24 +00:00
"fmt"
2016-06-11 21:34:00 +00:00
"io/ioutil"
2016-06-11 20:20:47 +00:00
"net/http"
"net/url"
2016-06-10 19:54:19 +00:00
"os"
2016-06-11 22:01:24 +00:00
"path"
2016-06-11 20:20:47 +00:00
"path/filepath"
"strings"
2016-06-10 19:54:19 +00:00
"time"
"github.com/dustin/go-humanize"
2016-06-28 09:24:02 +00:00
"github.com/hacdias/caddy-filemanager/config"
p "github.com/hacdias/caddy-filemanager/page"
2016-06-25 20:57:10 +00:00
"github.com/hacdias/caddy-filemanager/utils/errors"
2016-06-11 22:01:24 +00:00
"github.com/mholt/caddy/caddyhttp/httpserver"
2016-06-10 19:54:19 +00:00
)
2016-06-25 20:57:10 +00:00
// Info is the information about a particular file or directory
type Info struct {
2016-06-11 21:34:00 +00:00
IsDir bool
Name string
Size int64
URL string
2016-06-14 19:33:59 +00:00
Path string // The relative Path of the file/directory relative to Caddyfile.
RootPath string // The Path of the file/directory on http.FileSystem.
2016-06-11 21:34:00 +00:00
ModTime time.Time
Mode os.FileMode
Mimetype string
Content string
2016-06-23 22:21:44 +00:00
Raw []byte
2016-06-11 21:34:00 +00:00
Type string
2016-06-11 20:20:47 +00:00
}
2016-06-25 20:57:10 +00:00
// GetInfo gets the file information and, in case of error, returns the
2016-06-11 20:20:47 +00:00
// respective HTTP error code
2016-06-25 20:57:10 +00:00
func GetInfo(url *url.URL, c *config.Config) (*Info, int, error) {
2016-06-11 20:20:47 +00:00
var err error
2016-06-14 19:33:59 +00:00
rootPath := strings.Replace(url.Path, c.BaseURL, "", 1)
rootPath = strings.TrimPrefix(rootPath, "/")
rootPath = "/" + rootPath
2016-06-25 20:57:10 +00:00
relpath := c.PathScope + rootPath
relpath = strings.Replace(relpath, "\\", "/", -1)
relpath = filepath.Clean(relpath)
2016-06-11 20:20:47 +00:00
2016-06-25 20:57:10 +00:00
file := &Info{
2016-06-14 19:33:59 +00:00
URL: url.Path,
RootPath: rootPath,
2016-06-25 20:57:10 +00:00
Path: relpath,
2016-06-14 19:33:59 +00:00
}
f, err := c.Root.Open(rootPath)
2016-06-11 20:20:47 +00:00
if err != nil {
2016-06-25 20:57:10 +00:00
return file, errors.ToHTTPCode(err), err
2016-06-11 20:20:47 +00:00
}
defer f.Close()
info, err := f.Stat()
if err != nil {
2016-06-25 20:57:10 +00:00
return file, errors.ToHTTPCode(err), err
2016-06-11 20:20:47 +00:00
}
file.IsDir = info.IsDir()
file.ModTime = info.ModTime()
file.Name = info.Name()
file.Size = info.Size()
return file, 0, nil
2016-06-10 19:54:19 +00:00
}
2016-06-25 20:57:10 +00:00
// GetExtendedInfo is used to get extra parameters for FileInfo struct
func (i *Info) GetExtendedInfo() error {
err := i.Read()
2016-06-23 21:27:13 +00:00
if err != nil {
return err
2016-06-11 21:34:00 +00:00
}
2016-06-25 20:57:10 +00:00
i.Type = SimplifyMimeType(i.Mimetype)
2016-06-11 21:34:00 +00:00
return nil
}
// Read is used to read a file and store its content
2016-06-25 20:57:10 +00:00
func (i *Info) Read() error {
raw, err := ioutil.ReadFile(i.Path)
2016-06-11 21:34:00 +00:00
if err != nil {
return err
}
2016-06-25 20:57:10 +00:00
i.Mimetype = http.DetectContentType(raw)
i.Content = string(raw)
i.Raw = raw
2016-06-11 21:15:42 +00:00
return nil
}
2016-06-10 19:54:19 +00:00
// HumanSize returns the size of the file as a human-readable string
// in IEC format (i.e. power of 2 or base 1024).
2016-06-25 20:57:10 +00:00
func (i Info) HumanSize() string {
return humanize.IBytes(uint64(i.Size))
2016-06-10 19:54:19 +00:00
}
// HumanModTime returns the modified time of the file as a human-readable string.
2016-06-25 20:57:10 +00:00
func (i Info) HumanModTime(format string) string {
return i.ModTime.Format(format)
2016-06-10 19:54:19 +00:00
}
2016-06-11 20:20:47 +00:00
// Delete handles the delete requests
2016-06-25 20:57:10 +00:00
func (i *Info) Delete() (int, error) {
2016-06-11 20:20:47 +00:00
var err error
// If it's a directory remove all the contents inside
2016-06-25 20:57:10 +00:00
if i.IsDir {
err = os.RemoveAll(i.Path)
2016-06-11 20:20:47 +00:00
} else {
2016-06-25 20:57:10 +00:00
err = os.Remove(i.Path)
2016-06-11 20:20:47 +00:00
}
if err != nil {
2016-06-25 20:57:10 +00:00
return errors.ToHTTPCode(err), err
2016-06-11 20:20:47 +00:00
}
return http.StatusOK, nil
}
2016-06-11 20:34:38 +00:00
// Rename function is used tor rename a file or a directory
2016-06-25 20:57:10 +00:00
func (i *Info) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
2016-06-11 20:34:38 +00:00
newname := r.Header.Get("Rename-To")
if newname == "" {
return http.StatusBadRequest, nil
}
newpath := filepath.Clean(newname)
2016-06-25 20:57:10 +00:00
newpath = strings.Replace(i.Path, i.Name, newname, 1)
2016-06-11 20:34:38 +00:00
2016-06-25 20:57:10 +00:00
if err := os.Rename(i.Path, newpath); err != nil {
return errors.ToHTTPCode(err), err
2016-06-11 20:34:38 +00:00
}
2016-06-25 20:57:10 +00:00
i.Path = newpath
return http.StatusOK, nil
2016-06-11 20:34:38 +00:00
}
2016-06-11 21:34:00 +00:00
// ServeAsHTML is used to serve single file pages
2016-06-25 20:57:10 +00:00
func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
if i.IsDir {
return i.serveListing(w, r, c)
2016-06-11 22:01:24 +00:00
}
2016-06-25 20:57:10 +00:00
return i.serveSingleFile(w, r, c)
2016-06-11 22:01:24 +00:00
}
2016-06-25 20:57:10 +00:00
func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
err := i.GetExtendedInfo()
2016-06-11 21:34:00 +00:00
if err != nil {
2016-06-25 20:57:10 +00:00
return errors.ToHTTPCode(err), err
2016-06-11 21:34:00 +00:00
}
2016-06-25 20:57:10 +00:00
if i.Type == "blob" {
return i.ServeRawFile(w, r, c)
2016-06-23 21:27:13 +00:00
}
2016-06-25 20:57:10 +00:00
page := &p.Page{
Info: &p.Info{
Name: i.Name,
Path: i.RootPath,
2016-06-14 19:33:59 +00:00
IsDir: false,
2016-06-25 20:57:10 +00:00
Data: i,
2016-06-14 17:19:10 +00:00
Config: c,
2016-06-11 21:34:00 +00:00
},
}
2016-06-25 21:05:45 +00:00
if CanBeEdited(i.Name) {
2016-06-25 20:57:10 +00:00
editor, err := i.GetEditor()
2016-06-23 22:21:44 +00:00
if err != nil {
return http.StatusInternalServerError, err
}
page.Info.Data = editor
return page.PrintAsHTML(w, "frontmatter", "editor")
}
2016-06-23 21:27:13 +00:00
return page.PrintAsHTML(w, "single")
2016-06-11 21:34:00 +00:00
}
2016-06-25 20:57:10 +00:00
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-06-11 22:01:24 +00:00
var err error
2016-06-25 20:57:10 +00:00
file, err := c.Root.Open(i.RootPath)
2016-06-11 22:01:24 +00:00
if err != nil {
2016-06-25 20:57:10 +00:00
return errors.ToHTTPCode(err), err
2016-06-11 22:01:24 +00:00
}
defer file.Close()
2016-06-25 20:57:10 +00:00
listing, err := i.loadDirectoryContents(file, c)
2016-06-11 22:01:24 +00:00
if err != nil {
fmt.Println(err)
switch {
case os.IsPermission(err):
return http.StatusForbidden, err
case os.IsExist(err):
return http.StatusGone, err
default:
return http.StatusInternalServerError, err
}
}
listing.Context = httpserver.Context{
Root: c.Root,
Req: r,
URL: r.URL,
}
// Copy the query values into the Listing struct
var limit int
listing.Sort, listing.Order, limit, err = handleSortOrder(w, r, c.PathScope)
if err != nil {
return http.StatusBadRequest, err
}
listing.applySort()
if limit > 0 && limit <= len(listing.Items) {
listing.Items = listing.Items[:limit]
listing.ItemsLimitedTo = limit
}
2016-06-25 20:57:10 +00:00
page := &p.Page{
Info: &p.Info{
2016-06-14 17:19:10 +00:00
Name: listing.Name,
2016-06-25 20:57:10 +00:00
Path: i.RootPath,
2016-06-14 19:33:59 +00:00
IsDir: true,
2016-06-14 17:19:10 +00:00
Config: c,
Data: listing,
2016-06-11 22:01:24 +00:00
},
}
2016-06-30 21:37:52 +00:00
if r.Header.Get("Minimal") == "true" {
page.Minimal = true
}
2016-08-11 21:19:21 +00:00
if strings.Contains(r.Header.Get("Accept"), "application/json") {
return page.PrintAsJSON(w)
}
2016-06-23 21:27:13 +00:00
return page.PrintAsHTML(w, "listing")
2016-06-11 22:01:24 +00:00
}
2016-06-25 20:57:10 +00:00
func (i Info) loadDirectoryContents(file http.File, c *config.Config) (*Listing, error) {
2016-06-11 22:01:24 +00:00
files, err := file.Readdir(-1)
if err != nil {
return nil, err
}
2016-06-25 20:57:10 +00:00
listing := directoryListing(files, i.RootPath)
2016-06-11 22:01:24 +00:00
return &listing, nil
}
func directoryListing(files []os.FileInfo, urlPath string) Listing {
var (
2016-06-25 20:57:10 +00:00
fileinfos []Info
2016-06-11 22:01:24 +00:00
dirCount, fileCount int
)
for _, f := range files {
name := f.Name()
if f.IsDir() {
name += "/"
dirCount++
} else {
fileCount++
}
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
2016-06-25 20:57:10 +00:00
fileinfos = append(fileinfos, Info{
2016-06-11 22:01:24 +00:00
IsDir: f.IsDir(),
Name: f.Name(),
Size: f.Size(),
URL: url.String(),
ModTime: f.ModTime().UTC(),
Mode: f.Mode(),
})
}
return Listing{
Name: path.Base(urlPath),
Path: urlPath,
Items: fileinfos,
NumDirs: dirCount,
NumFiles: fileCount,
}
}
2016-06-14 19:33:59 +00:00
// ServeRawFile serves raw files
2016-06-25 20:57:10 +00:00
func (i *Info) ServeRawFile(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
err := i.GetExtendedInfo()
2016-06-14 19:33:59 +00:00
if err != nil {
2016-06-25 20:57:10 +00:00
return errors.ToHTTPCode(err), err
2016-06-14 19:33:59 +00:00
}
2016-06-25 20:57:10 +00:00
if i.Type != "text" {
i.Read()
2016-06-14 19:33:59 +00:00
}
2016-06-25 20:57:10 +00:00
w.Header().Set("Content-Type", i.Mimetype)
w.Write([]byte(i.Content))
2016-06-14 19:33:59 +00:00
return 200, nil
}
2016-06-11 21:34:00 +00:00
// SimplifyMimeType returns the base type of a file
func SimplifyMimeType(name string) string {
if strings.HasPrefix(name, "video") {
return "video"
}
if strings.HasPrefix(name, "audio") {
return "audio"
}
if strings.HasPrefix(name, "image") {
return "image"
}
2016-06-23 21:27:13 +00:00
if strings.HasPrefix(name, "text") {
return "text"
}
if strings.HasPrefix(name, "application/javascript") {
return "text"
}
return "blob"
2016-06-11 21:34:00 +00:00
}