Former-commit-id: 35d375188e7c700484d32a27eb48b30192e20537
This commit is contained in:
Henrique Dias
2017-01-03 15:10:33 +00:00
parent 5fc83eff7e
commit f1f248e4d6
10 changed files with 160 additions and 99 deletions

View File

@@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/hacdias/caddy-filemanager/config"
@@ -16,8 +17,12 @@ import (
// Info contains the information about a particular file or directory
type Info struct {
os.FileInfo
Name string
Size int64
URL string
ModTime time.Time
Mode os.FileMode
IsDir bool
Path string // Relative path to Caddyfile
VirtualPath string // Relative path to u.FileSystem
Mimetype string
@@ -40,18 +45,24 @@ func GetInfo(url *url.URL, c *config.Config, u *config.User) (*Info, int, error)
i.Path = strings.Replace(i.Path, "\\", "/", -1)
i.Path = filepath.Clean(i.Path)
i.FileInfo, err = os.Stat(i.Path)
info, err := os.Stat(i.Path)
if err != nil {
return i, errors.ErrorToHTTPCode(err, false), err
}
i.Name = info.Name()
i.ModTime = info.ModTime()
i.Mode = info.Mode()
i.IsDir = info.IsDir()
i.Size = info.Size()
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 {
i.Mimetype = mime.TypeByExtension(filepath.Ext(i.Name()))
i.Mimetype = mime.TypeByExtension(filepath.Ext(i.Name))
if i.Mimetype == "" {
err := i.Read()
@@ -88,12 +99,12 @@ func (i Info) StringifyContent() string {
// HumanSize returns the size of the file as a human-readable string
// in IEC format (i.e. power of 2 or base 1024).
func (i Info) HumanSize() string {
return humanize.IBytes(uint64(i.Size()))
return humanize.IBytes(uint64(i.Size))
}
// HumanModTime returns the modified time of the file as a human-readable string.
func (i Info) HumanModTime(format string) string {
return i.ModTime().Format(format)
return i.ModTime.Format(format)
}
// CanBeEdited checks if the extension of a file is supported by the editor
@@ -113,14 +124,14 @@ func (i Info) CanBeEdited() bool {
".html",
".txt", ".rtf",
".sh", ".bash", ".ps1", ".bat", ".cmd",
".php", ".pl", ".py",
".php", ".pl", ".py",
"Caddyfile",
".c", ".cc", ".h", ".hh", ".cpp", ".hpp", ".f90",
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
}
for _, extension := range extensions {
if strings.HasSuffix(i.Name(), extension) {
if strings.HasSuffix(i.Name, extension) {
return true
}
}

View File

@@ -74,7 +74,11 @@ func GetListing(u *config.User, filePath string, baseURL string) (*Listing, erro
url := url.URL{Path: baseURL + name}
i := Info{
FileInfo: f,
Name: f.Name(),
Size: f.Size(),
ModTime: f.ModTime(),
Mode: f.Mode(),
IsDir: f.IsDir(),
URL: url.String(),
UserAllowed: allowed,
}
@@ -138,15 +142,15 @@ func (l byName) Swap(i, j int) {
// Treat upper and lower case equally
func (l byName) Less(i, j int) bool {
if l.Items[i].IsDir() && !l.Items[j].IsDir() {
if l.Items[i].IsDir && !l.Items[j].IsDir {
return true
}
if !l.Items[i].IsDir() && l.Items[j].IsDir() {
if !l.Items[i].IsDir && l.Items[j].IsDir {
return false
}
return strings.ToLower(l.Items[i].Name()) < strings.ToLower(l.Items[j].Name())
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
}
// By Size
@@ -160,11 +164,11 @@ func (l bySize) Swap(i, j int) {
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, jSize := l.Items[i].Size, l.Items[j].Size
if l.Items[i].IsDir {
iSize = directoryOffset + iSize
}
if l.Items[j].IsDir() {
if l.Items[j].IsDir {
jSize = directoryOffset + jSize
}
return iSize < jSize
@@ -178,5 +182,5 @@ func (l byTime) Swap(i, j int) {
l.Items[i], l.Items[j] = l.Items[j], l.Items[i]
}
func (l byTime) Less(i, j int) bool {
return l.Items[i].ModTime().Before(l.Items[j].ModTime())
return l.Items[i].ModTime.Before(l.Items[j].ModTime)
}