filebrowser/info.go

175 lines
3.8 KiB
Go
Raw Normal View History

2017-06-24 11:12:15 +00:00
package filemanager
import (
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
)
2017-06-25 12:03:59 +00:00
// fileInfo contains the information about a particular file or directory.
type fileInfo struct {
2017-06-25 13:24:26 +00:00
// Used to store the file's content temporarily.
2017-06-24 11:12:15 +00:00
content []byte
2017-06-25 13:24:26 +00:00
Name string
Size int64
URL string
Extension string
ModTime time.Time
Mode os.FileMode
IsDir bool
// Absolute path.
Path string
// Relative path to user's virtual File System.
VirtualPath string
// Indicates the file content type: video, text, image, music or blob.
Type string
// Indicates if the user has enough permissions to edit the file.
UserAllowed bool
2017-06-24 11:12:15 +00:00
}
2017-06-25 12:03:59 +00:00
// getInfo gets the file information and, in case of error, returns the
2017-06-24 11:12:15 +00:00
// respective HTTP error code
2017-06-25 12:03:59 +00:00
func getInfo(url *url.URL, c *FileManager, u *user) (*fileInfo, error) {
2017-06-24 11:12:15 +00:00
var err error
2017-06-25 12:03:59 +00:00
i := &fileInfo{URL: c.PrefixURL + url.Path}
2017-06-25 12:09:49 +00:00
i.VirtualPath = strings.Replace(url.Path, c.baseURL, "", 1)
2017-06-24 11:12:15 +00:00
i.VirtualPath = strings.TrimPrefix(i.VirtualPath, "/")
i.VirtualPath = "/" + i.VirtualPath
i.Path = u.scope + i.VirtualPath
2017-06-24 11:12:15 +00:00
i.Path = filepath.Clean(i.Path)
info, err := os.Stat(i.Path)
if err != nil {
return i, err
}
i.Name = info.Name()
i.ModTime = info.ModTime()
i.Mode = info.Mode()
i.IsDir = info.IsDir()
i.Size = info.Size()
i.Extension = filepath.Ext(i.Name)
return i, nil
}
var textExtensions = [...]string{
".md", ".markdown", ".mdown", ".mmark",
".asciidoc", ".adoc", ".ad",
".rst",
".json", ".toml", ".yaml", ".csv", ".xml", ".rss", ".conf", ".ini",
".tex", ".sty",
".css", ".sass", ".scss",
".js",
".html",
".txt", ".rtf",
".sh", ".bash", ".ps1", ".bat", ".cmd",
".php", ".pl", ".py",
"Caddyfile",
".c", ".cc", ".h", ".hh", ".cpp", ".hpp", ".f90",
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
}
2017-06-25 13:24:26 +00:00
// RetrieveFileType obtains the mimetype and converts it to a simple
// type nomenclature.
2017-06-25 12:03:59 +00:00
func (i *fileInfo) RetrieveFileType() error {
2017-06-25 13:24:26 +00:00
// Tries to get the file mimetype using its extension.
mimetype := mime.TypeByExtension(i.Extension)
2017-06-24 11:12:15 +00:00
2017-06-25 13:24:26 +00:00
if mimetype == "" {
2017-06-24 11:12:15 +00:00
err := i.Read()
if err != nil {
return err
}
2017-06-25 13:24:26 +00:00
// Tries to get the file mimetype using its first
// 512 bytes.
mimetype = http.DetectContentType(i.content)
2017-06-24 11:12:15 +00:00
}
2017-06-25 13:24:26 +00:00
if strings.HasPrefix(mimetype, "video") {
2017-06-24 11:12:15 +00:00
i.Type = "video"
return nil
}
2017-06-25 13:24:26 +00:00
if strings.HasPrefix(mimetype, "audio") {
2017-06-24 11:12:15 +00:00
i.Type = "audio"
return nil
}
2017-06-25 13:24:26 +00:00
if strings.HasPrefix(mimetype, "image") {
2017-06-24 11:12:15 +00:00
i.Type = "image"
return nil
}
2017-06-25 13:24:26 +00:00
if strings.HasPrefix(mimetype, "text") {
2017-06-24 11:12:15 +00:00
i.Type = "text"
return nil
}
2017-06-25 13:24:26 +00:00
if strings.HasPrefix(mimetype, "application/javascript") {
2017-06-24 11:12:15 +00:00
i.Type = "text"
return nil
}
// If the type isn't text (and is blob for example), it will check some
// common types that are mistaken not to be text.
for _, extension := range textExtensions {
if strings.HasSuffix(i.Name, extension) {
i.Type = "text"
return nil
}
}
i.Type = "blob"
return nil
}
// Reads the file.
2017-06-25 12:03:59 +00:00
func (i *fileInfo) Read() error {
2017-06-24 11:12:15 +00:00
if len(i.content) != 0 {
return nil
}
var err error
i.content, err = ioutil.ReadFile(i.Path)
if err != nil {
return err
}
return nil
}
2017-06-25 13:24:26 +00:00
// StringifyContent returns a string with the file content.
2017-06-25 12:03:59 +00:00
func (i fileInfo) StringifyContent() string {
2017-06-24 11:12:15 +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).
2017-06-25 12:03:59 +00:00
func (i fileInfo) HumanSize() string {
2017-06-24 11:12:15 +00:00
return humanize.IBytes(uint64(i.Size))
}
// HumanModTime returns the modified time of the file as a human-readable string.
2017-06-25 12:03:59 +00:00
func (i fileInfo) HumanModTime(format string) string {
2017-06-24 11:12:15 +00:00
return i.ModTime.Format(format)
}
// CanBeEdited checks if the extension of a file is supported by the editor
2017-06-25 12:03:59 +00:00
func (i fileInfo) CanBeEdited() bool {
2017-06-24 11:12:15 +00:00
return i.Type == "text"
}