filebrowser/file.go

517 lines
10 KiB
Go
Raw Normal View History

2017-06-24 11:12:15 +00:00
package filemanager
import (
2017-07-02 16:53:47 +00:00
"bytes"
2017-06-27 18:00:58 +00:00
"context"
2017-06-27 14:44:20 +00:00
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
2017-07-25 10:57:27 +00:00
"fmt"
2017-06-27 14:44:20 +00:00
"hash"
"io"
2017-06-24 11:12:15 +00:00
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
2017-06-27 18:00:58 +00:00
"sort"
2017-06-24 11:12:15 +00:00
"strings"
"time"
2017-06-29 09:17:35 +00:00
2017-07-02 16:53:47 +00:00
"github.com/spf13/hugo/parser"
2017-06-24 11:12:15 +00:00
)
2017-06-27 14:44:20 +00:00
var (
errInvalidOption = errors.New("Invalid option")
)
2017-06-29 09:17:35 +00:00
// file contains the information about a particular file or directory.
type file struct {
2017-07-02 16:40:52 +00:00
// Indicates the Kind of view on the front-end (listing, editor or preview).
Kind string `json:"kind"`
2017-06-27 18:00:58 +00:00
// The name of the file.
2017-06-28 15:05:30 +00:00
Name string `json:"name"`
2017-06-27 18:00:58 +00:00
// The Size of the file.
2017-06-28 15:05:30 +00:00
Size int64 `json:"size"`
2017-06-27 18:00:58 +00:00
// The absolute URL.
2017-06-28 15:05:30 +00:00
URL string `json:"url"`
2017-06-27 18:00:58 +00:00
// The extension of the file.
2017-06-28 15:05:30 +00:00
Extension string `json:"extension"`
2017-06-27 18:00:58 +00:00
// The last modified time.
2017-06-28 15:05:30 +00:00
ModTime time.Time `json:"modified"`
2017-06-27 18:00:58 +00:00
// The File Mode.
2017-06-28 15:05:30 +00:00
Mode os.FileMode `json:"mode"`
2017-06-27 18:00:58 +00:00
// Indicates if this file is a directory.
2017-06-28 15:05:30 +00:00
IsDir bool `json:"isDir"`
2017-06-25 13:24:26 +00:00
// Absolute path.
2017-06-28 15:05:30 +00:00
Path string `json:"path"`
2017-06-25 13:24:26 +00:00
// Relative path to user's virtual File System.
2017-06-28 15:05:30 +00:00
VirtualPath string `json:"virtualPath"`
2017-06-25 13:24:26 +00:00
// Indicates the file content type: video, text, image, music or blob.
2017-06-28 15:05:30 +00:00
Type string `json:"type"`
2017-06-28 10:45:41 +00:00
// Stores the content of a text file.
2017-06-29 09:17:35 +00:00
Content string `json:"content,omitempty"`
*listing `json:",omitempty"`
2017-07-01 07:50:42 +00:00
Metadata string `json:"metadata,omitempty"`
Language string `json:"language,omitempty"`
2017-06-24 11:12:15 +00:00
}
2017-06-27 18:00:58 +00:00
// A listing is the context used to fill out a template.
type listing struct {
// The items (files and folders) in the path.
2017-07-08 11:46:19 +00:00
Items []*file `json:"items"`
2017-06-27 18:00:58 +00:00
// The number of directories in the listing.
2017-06-28 15:05:30 +00:00
NumDirs int `json:"numDirs"`
2017-06-27 18:00:58 +00:00
// The number of files (items that aren't directories) in the listing.
2017-06-28 15:05:30 +00:00
NumFiles int `json:"numFiles"`
2017-06-27 18:00:58 +00:00
// Which sorting order is used.
2017-06-28 15:05:30 +00:00
Sort string `json:"sort"`
2017-06-27 18:00:58 +00:00
// And which order.
2017-06-28 15:05:30 +00:00
Order string `json:"order"`
2017-06-29 09:17:35 +00:00
// Displays in mosaic or list.
Display string `json:"display"`
}
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-29 09:17:35 +00:00
func getInfo(url *url.URL, c *FileManager, u *User) (*file, error) {
2017-06-24 11:12:15 +00:00
var err error
2017-07-02 16:40:52 +00:00
i := &file{
2017-07-03 10:20:36 +00:00
URL: "/files" + url.Path,
2017-07-02 16:40:52 +00:00
VirtualPath: url.Path,
2017-07-03 07:59:49 +00:00
Path: filepath.Join(string(u.FileSystem), url.Path),
2017-07-02 16:40:52 +00:00
}
2017-06-24 11:12:15 +00:00
2017-07-03 07:59:49 +00:00
info, err := u.FileSystem.Stat(context.TODO(), url.Path)
2017-06-24 11:12:15 +00:00
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)
2017-07-03 14:19:17 +00:00
if i.IsDir && !strings.HasSuffix(i.URL, "/") {
i.URL += "/"
}
2017-06-24 11:12:15 +00:00
return i, nil
}
2017-06-27 18:00:58 +00:00
// getListing gets the information about a specific directory and its files.
func (i *file) getListing(c *RequestContext, r *http.Request) error {
2017-06-27 18:00:58 +00:00
// Gets the directory information using the Virtual File System of
// the user configuration.
f, err := c.User.FileSystem.OpenFile(context.TODO(), c.FI.VirtualPath, os.O_RDONLY, 0)
2017-06-27 18:00:58 +00:00
if err != nil {
2017-06-29 09:17:35 +00:00
return err
2017-06-27 18:00:58 +00:00
}
2017-06-29 09:17:35 +00:00
defer f.Close()
2017-06-27 18:00:58 +00:00
// Reads the directory and gets the information about the files.
2017-06-29 09:17:35 +00:00
files, err := f.Readdir(-1)
2017-06-27 18:00:58 +00:00
if err != nil {
2017-06-29 09:17:35 +00:00
return err
2017-06-27 18:00:58 +00:00
}
var (
2017-07-08 11:46:19 +00:00
fileinfos []*file
2017-06-27 18:00:58 +00:00
dirCount, fileCount int
)
for _, f := range files {
name := f.Name()
allowed := c.User.Allowed("/" + name)
2017-06-27 18:00:58 +00:00
if !allowed {
continue
}
if f.IsDir() {
name += "/"
dirCount++
} else {
fileCount++
}
// Absolute URL
2017-07-02 16:40:52 +00:00
url := url.URL{Path: i.URL + name}
2017-06-27 18:00:58 +00:00
2017-07-08 11:46:19 +00:00
i := &file{
Name: f.Name(),
Size: f.Size(),
ModTime: f.ModTime(),
Mode: f.Mode(),
IsDir: f.IsDir(),
URL: url.String(),
Extension: filepath.Ext(name),
VirtualPath: filepath.Join(i.VirtualPath, name),
Path: filepath.Join(i.Path, name),
2017-06-27 18:00:58 +00:00
}
2017-07-26 08:44:09 +00:00
i.GetFileType(false)
2017-06-27 18:00:58 +00:00
fileinfos = append(fileinfos, i)
}
2017-06-29 09:17:35 +00:00
i.listing = &listing{
2017-06-27 18:00:58 +00:00
Items: fileinfos,
NumDirs: dirCount,
NumFiles: fileCount,
2017-06-29 09:17:35 +00:00
}
return nil
}
// getEditor gets the editor based on a Info struct
2017-07-02 16:40:52 +00:00
func (i *file) getEditor() error {
2017-07-01 07:50:42 +00:00
i.Language = editorLanguage(i.Extension)
// If the editor will hold only content, leave now.
if editorMode(i.Language) == "content" {
return nil
2017-06-29 09:17:35 +00:00
}
2017-07-01 07:50:42 +00:00
// If the file doesn't have any kind of metadata, leave now.
2017-07-02 16:53:47 +00:00
if !hasRune(i.Content) {
2017-07-01 07:50:42 +00:00
return nil
2017-06-29 09:17:35 +00:00
}
2017-07-02 16:53:47 +00:00
buffer := bytes.NewBuffer([]byte(i.Content))
page, err := parser.ReadFrom(buffer)
2017-06-29 09:17:35 +00:00
2017-07-02 16:53:47 +00:00
// If there is an error, just ignore it and return nil.
// This way, the file can be served for editing.
if err != nil {
2017-06-29 09:17:35 +00:00
2017-07-02 16:53:47 +00:00
return nil
}
2017-06-29 09:17:35 +00:00
2017-07-02 16:53:47 +00:00
i.Content = strings.TrimSpace(string(page.Content()))
i.Metadata = strings.TrimSpace(string(page.FrontMatter()))
2017-06-29 09:17:35 +00:00
return nil
2017-06-24 11:12:15 +00:00
}
2017-07-26 08:44:09 +00:00
// GetFileType obtains the mimetype and converts it to a simple
2017-06-25 13:24:26 +00:00
// type nomenclature.
2017-07-26 08:44:09 +00:00
func (i *file) GetFileType(checkContent bool) error {
2017-06-29 09:17:35 +00:00
var content []byte
var err 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-07-08 11:46:19 +00:00
if mimetype == "" && checkContent {
2017-07-10 07:43:46 +00:00
file, err := os.Open(i.Path)
2017-06-24 11:12:15 +00:00
if err != nil {
return err
}
2017-07-10 07:43:46 +00:00
defer file.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil && err != io.EOF {
return err
}
2017-06-24 11:12:15 +00:00
2017-06-25 13:24:26 +00:00
// Tries to get the file mimetype using its first
// 512 bytes.
2017-07-10 07:43:46 +00:00
mimetype = http.DetectContentType(buffer)
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"
2017-06-29 09:17:35 +00:00
goto End
2017-06-24 11:12:15 +00:00
}
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"
2017-06-29 09:17:35 +00:00
goto End
2017-06-24 11:12:15 +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.
for _, extension := range textExtensions {
if strings.HasSuffix(i.Name, extension) {
i.Type = "text"
2017-07-10 07:43:46 +00:00
goto End
2017-06-24 11:12:15 +00:00
}
}
i.Type = "blob"
2017-06-29 09:17:35 +00:00
End:
// If the file type is text, save its content.
if i.Type == "text" {
if len(content) == 0 {
content, err = ioutil.ReadFile(i.Path)
if err != nil {
return err
}
}
2017-06-24 11:12:15 +00:00
2017-06-29 09:17:35 +00:00
i.Content = string(content)
2017-06-24 11:12:15 +00:00
}
2017-06-29 09:17:35 +00:00
2017-06-24 11:12:15 +00:00
return nil
}
2017-06-29 09:17:35 +00:00
func (i file) Checksum(kind string) (string, error) {
2017-06-27 14:44:20 +00:00
file, err := os.Open(i.Path)
if err != nil {
return "", err
}
defer file.Close()
var h hash.Hash
switch kind {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
case "sha512":
h = sha512.New()
default:
return "", errInvalidOption
}
_, err = io.Copy(h, file)
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
2017-06-24 11:12:15 +00:00
// CanBeEdited checks if the extension of a file is supported by the editor
2017-06-29 09:17:35 +00:00
func (i file) CanBeEdited() bool {
2017-06-24 11:12:15 +00:00
return i.Type == "text"
}
2017-06-27 18:00:58 +00:00
// ApplySort applies the sort order using .Order and .Sort
func (l listing) ApplySort() {
// Check '.Order' to know how to sort
if l.Order == "desc" {
switch l.Sort {
case "name":
sort.Sort(sort.Reverse(byName(l)))
case "size":
sort.Sort(sort.Reverse(bySize(l)))
default:
// If not one of the above, do nothing
return
}
} else { // If we had more Orderings we could add them here
switch l.Sort {
case "name":
sort.Sort(byName(l))
case "size":
sort.Sort(bySize(l))
default:
sort.Sort(byName(l))
return
}
}
}
// Implement sorting for listing
type byName listing
type bySize listing
// By Name
func (l byName) Len() int {
return len(l.Items)
}
func (l byName) Swap(i, j int) {
l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
}
// Treat upper and lower case equally
func (l byName) Less(i, j int) bool {
if l.Items[i].IsDir && !l.Items[j].IsDir {
return true
}
if !l.Items[i].IsDir && l.Items[j].IsDir {
return false
}
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
}
// By Size
func (l bySize) Len() int {
return len(l.Items)
}
func (l bySize) Swap(i, j int) {
l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
}
const directoryOffset = -1 << 31 // = math.MinInt32
func (l bySize) Less(i, j int) bool {
iSize, jSize := l.Items[i].Size, l.Items[j].Size
if l.Items[i].IsDir {
iSize = directoryOffset + iSize
}
if l.Items[j].IsDir {
jSize = directoryOffset + jSize
}
return iSize < jSize
}
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-29 09:17:35 +00:00
2017-07-02 16:53:47 +00:00
// hasRune checks if the file has the frontmatter rune
func hasRune(file string) bool {
return strings.HasPrefix(file, "---") ||
strings.HasPrefix(file, "+++") ||
strings.HasPrefix(file, "{")
}
2017-06-29 09:17:35 +00:00
func editorMode(language string) string {
switch language {
case "markdown", "asciidoc", "rst":
2017-07-01 07:50:42 +00:00
return "content+metadata"
2017-06-29 09:17:35 +00:00
}
2017-07-01 07:50:42 +00:00
return "content"
2017-06-29 09:17:35 +00:00
}
func editorLanguage(mode string) string {
2017-06-30 17:03:08 +00:00
mode = strings.TrimPrefix(mode, ".")
2017-06-29 09:17:35 +00:00
switch mode {
case "md", "markdown", "mdown", "mmark":
mode = "markdown"
2017-06-30 17:03:08 +00:00
case "yml":
mode = "yaml"
2017-06-29 09:17:35 +00:00
case "asciidoc", "adoc", "ad":
mode = "asciidoc"
case "rst":
mode = "rst"
case "html", "htm", "xml":
mode = "htmlmixed"
2017-06-29 09:17:35 +00:00
case "js":
mode = "javascript"
case "go":
mode = "golang"
case "":
mode = "text"
2017-06-29 09:17:35 +00:00
}
return mode
}
2017-07-25 10:57:27 +00:00
func copyFile(source string, dest string) (err error) {
sourcefile, err := os.Open(source)
if err != nil {
return err
}
defer sourcefile.Close()
destfile, err := os.Create(dest)
if err != nil {
return err
}
defer destfile.Close()
_, err = io.Copy(destfile, sourcefile)
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
2017-07-25 11:25:15 +00:00
if err != nil {
return err
}
2017-07-25 10:57:27 +00:00
}
}
return
}
func copyDir(source string, dest string) (err error) {
// get properties of source dir
sourceinfo, err := os.Stat(source)
if err != nil {
return err
}
// create dest dir
err = os.MkdirAll(dest, sourceinfo.Mode())
if err != nil {
return err
}
directory, _ := os.Open(source)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
sourcefilepointer := source + "/" + obj.Name()
destinationfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
// create sub-directories - recursively
err = copyDir(sourcefilepointer, destinationfilepointer)
if err != nil {
fmt.Println(err)
}
} else {
// perform copy
err = copyFile(sourcefilepointer, destinationfilepointer)
if err != nil {
fmt.Println(err)
}
}
}
return
}