2016-10-18 20:49:46 +00:00
|
|
|
package file
|
2016-10-18 20:06:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2016-10-22 15:37:20 +00:00
|
|
|
"mime"
|
2016-10-18 20:06:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-01-03 15:10:33 +00:00
|
|
|
"time"
|
2016-10-18 20:06:31 +00:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
2016-10-22 11:07:19 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/utils/errors"
|
2016-10-18 20:06:31 +00:00
|
|
|
)
|
|
|
|
|
2016-10-18 20:49:46 +00:00
|
|
|
// Info contains the information about a particular file or directory
|
|
|
|
type Info struct {
|
2017-01-03 15:10:33 +00:00
|
|
|
Name string
|
|
|
|
Size int64
|
2016-10-18 20:06:31 +00:00
|
|
|
URL string
|
2017-01-03 15:10:33 +00:00
|
|
|
ModTime time.Time
|
|
|
|
Mode os.FileMode
|
|
|
|
IsDir bool
|
2016-10-18 20:06:31 +00:00
|
|
|
Path string // Relative path to Caddyfile
|
|
|
|
VirtualPath string // Relative path to u.FileSystem
|
|
|
|
Mimetype string
|
|
|
|
Content []byte
|
|
|
|
Type string
|
|
|
|
UserAllowed bool // Indicates if the user has enough permissions
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInfo gets the file information and, in case of error, returns the
|
|
|
|
// respective HTTP error code
|
2016-10-18 20:49:46 +00:00
|
|
|
func GetInfo(url *url.URL, c *config.Config, u *config.User) (*Info, int, error) {
|
2016-10-18 20:06:31 +00:00
|
|
|
var err error
|
|
|
|
|
2017-01-21 21:10:01 +00:00
|
|
|
i := &Info{URL: c.PrefixURL + url.Path}
|
2016-10-18 20:06:31 +00:00
|
|
|
i.VirtualPath = strings.Replace(url.Path, c.BaseURL, "", 1)
|
|
|
|
i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/")
|
|
|
|
i.VirtualPath = "/" + i.VirtualPath
|
|
|
|
|
|
|
|
i.Path = u.Scope + i.VirtualPath
|
|
|
|
i.Path = strings.Replace(i.Path, "\\", "/", -1)
|
|
|
|
i.Path = filepath.Clean(i.Path)
|
|
|
|
|
2017-01-03 15:10:33 +00:00
|
|
|
info, err := os.Stat(i.Path)
|
2016-10-18 20:06:31 +00:00
|
|
|
if err != nil {
|
2016-10-30 20:42:56 +00:00
|
|
|
return i, errors.ErrorToHTTPCode(err, false), err
|
2016-10-18 20:06:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 15:10:33 +00:00
|
|
|
i.Name = info.Name()
|
|
|
|
i.ModTime = info.ModTime()
|
|
|
|
i.Mode = info.Mode()
|
|
|
|
i.IsDir = info.IsDir()
|
|
|
|
i.Size = info.Size()
|
|
|
|
|
2016-10-22 12:46:10 +00:00
|
|
|
return i, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveFileType obtains the mimetype and a simplified internal Type
|
|
|
|
// using the first 512 bytes from the file.
|
|
|
|
func (i *Info) RetrieveFileType() error {
|
2017-01-03 15:10:33 +00:00
|
|
|
i.Mimetype = mime.TypeByExtension(filepath.Ext(i.Name))
|
2016-10-22 12:09:38 +00:00
|
|
|
|
2016-10-22 15:37:20 +00:00
|
|
|
if i.Mimetype == "" {
|
|
|
|
err := i.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Mimetype = http.DetectContentType(i.Content)
|
2016-10-22 12:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i.Type = simplifyMediaType(i.Mimetype)
|
2016-10-22 12:46:10 +00:00
|
|
|
return nil
|
2016-10-18 20:06:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 12:46:10 +00:00
|
|
|
// Reads the file.
|
2016-10-18 20:49:46 +00:00
|
|
|
func (i *Info) Read() error {
|
2016-10-22 15:37:20 +00:00
|
|
|
if len(i.Content) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 20:06:31 +00:00
|
|
|
var err error
|
|
|
|
i.Content, err = ioutil.ReadFile(i.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 20:49:46 +00:00
|
|
|
// StringifyContent returns the string version of Raw
|
|
|
|
func (i Info) StringifyContent() string {
|
2016-10-18 20:06:31 +00:00
|
|
|
return string(i.Content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HumanSize returns the size of the file as a human-readable string
|
|
|
|
// in IEC format (i.e. power of 2 or base 1024).
|
2016-10-18 20:49:46 +00:00
|
|
|
func (i Info) HumanSize() string {
|
2017-01-03 15:10:33 +00:00
|
|
|
return humanize.IBytes(uint64(i.Size))
|
2016-10-18 20:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HumanModTime returns the modified time of the file as a human-readable string.
|
2016-10-18 20:49:46 +00:00
|
|
|
func (i Info) HumanModTime(format string) string {
|
2017-01-03 15:10:33 +00:00
|
|
|
return i.ModTime.Format(format)
|
2016-10-18 20:06:31 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 11:07:19 +00:00
|
|
|
// CanBeEdited checks if the extension of a file is supported by the editor
|
|
|
|
func (i Info) CanBeEdited() bool {
|
|
|
|
if i.Type == "text" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-25 14:43:13 +00:00
|
|
|
// If the type isn't text (and is blob for example), it will check some
|
|
|
|
// common types that are mistaken not to be text.
|
2016-10-22 11:07:19 +00:00
|
|
|
extensions := [...]string{
|
2016-11-03 14:38:01 +00:00
|
|
|
".md", ".markdown", ".mdown", ".mmark",
|
|
|
|
".asciidoc", ".adoc", ".ad",
|
|
|
|
".rst",
|
|
|
|
".json", ".toml", ".yaml", ".csv", ".xml", ".rss", ".conf", ".ini",
|
2016-11-03 14:41:59 +00:00
|
|
|
".tex", ".sty",
|
2016-10-22 11:07:19 +00:00
|
|
|
".css", ".sass", ".scss",
|
|
|
|
".js",
|
|
|
|
".html",
|
2016-11-03 14:38:01 +00:00
|
|
|
".txt", ".rtf",
|
|
|
|
".sh", ".bash", ".ps1", ".bat", ".cmd",
|
2017-01-03 15:10:33 +00:00
|
|
|
".php", ".pl", ".py",
|
2016-11-03 14:38:01 +00:00
|
|
|
"Caddyfile",
|
2016-11-03 14:41:59 +00:00
|
|
|
".c", ".cc", ".h", ".hh", ".cpp", ".hpp", ".f90",
|
|
|
|
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
|
2016-10-22 11:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, extension := range extensions {
|
2017-01-03 15:10:33 +00:00
|
|
|
if strings.HasSuffix(i.Name, extension) {
|
2016-10-22 11:07:19 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-18 20:49:46 +00:00
|
|
|
func simplifyMediaType(name string) string {
|
2016-10-18 20:06:31 +00:00
|
|
|
if strings.HasPrefix(name, "video") {
|
|
|
|
return "video"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, "audio") {
|
|
|
|
return "audio"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, "image") {
|
|
|
|
return "image"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, "text") {
|
|
|
|
return "text"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, "application/javascript") {
|
|
|
|
return "text"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "blob"
|
|
|
|
}
|