pkger/file_info.go

140 lines
2.4 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
import (
"encoding/json"
2019-08-02 22:14:11 +03:00
"fmt"
2019-07-31 00:21:26 +03:00
"os"
2019-08-09 22:30:12 +03:00
"strings"
2019-07-31 00:21:26 +03:00
"time"
)
type FileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
2019-08-09 04:51:58 +03:00
virtual bool
2019-07-31 00:21:26 +03:00
sys interface{}
}
func (f *FileInfo) String() string {
b, _ := json.MarshalIndent(f, "", " ")
return string(b)
}
func (f *FileInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"name": f.name,
"size": f.size,
"mode": f.mode,
"modTime": f.modTime.Format(timeFmt),
"isDir": f.isDir,
2019-08-10 00:15:14 +03:00
"virtual": true,
2019-07-31 00:21:26 +03:00
"sys": f.sys,
})
}
2019-08-02 22:14:11 +03:00
func (f *FileInfo) UnmarshalJSON(b []byte) error {
m := map[string]interface{}{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
var ok bool
f.name, ok = m["name"].(string)
if !ok {
return fmt.Errorf("could not determine name %q", m["name"])
}
size, ok := m["size"].(float64)
if !ok {
return fmt.Errorf("could not determine size %q", m["size"])
}
f.size = int64(size)
mode, ok := m["mode"].(float64)
if !ok {
return fmt.Errorf("could not determine mode %q", m["mode"])
}
f.mode = os.FileMode(mode)
modTime, ok := m["modTime"].(string)
if !ok {
return fmt.Errorf("could not determine modTime %q", m["modTime"])
}
t, err := time.Parse(timeFmt, modTime)
if err != nil {
return err
}
f.modTime = t
f.isDir, ok = m["isDir"].(bool)
if !ok {
return fmt.Errorf("could not determine isDir %q", m["isDir"])
}
f.sys = m["sys"]
2019-08-10 00:15:14 +03:00
f.virtual = true
2019-08-02 22:14:11 +03:00
return nil
}
2019-07-31 00:21:26 +03:00
func (f *FileInfo) Name() string {
return f.name
}
func (f *FileInfo) Size() int64 {
return f.size
}
func (f *FileInfo) Mode() os.FileMode {
return f.mode
}
func (f *FileInfo) ModTime() time.Time {
return f.modTime
}
func (f *FileInfo) IsDir() bool {
return f.isDir
}
func (f *FileInfo) Sys() interface{} {
return f.sys
}
var _ os.FileInfo = &FileInfo{}
func NewFileInfo(info os.FileInfo) *FileInfo {
fi := &FileInfo{
2019-08-30 00:05:44 +03:00
name: cleanName(info.Name()),
2019-07-31 00:21:26 +03:00
size: info.Size(),
mode: info.Mode(),
modTime: info.ModTime(),
isDir: info.IsDir(),
sys: info.Sys(),
}
return fi
}
func WithName(name string, info os.FileInfo) *FileInfo {
if ft, ok := info.(*FileInfo); ok {
2019-08-30 00:05:44 +03:00
ft.name = cleanName(name)
2019-07-31 00:21:26 +03:00
return ft
}
fo := NewFileInfo(info)
2019-08-30 00:05:44 +03:00
fo.name = cleanName(name)
2019-07-31 00:21:26 +03:00
return fo
}
2019-08-30 00:05:44 +03:00
func cleanName(s string) string {
if strings.Contains(s, "\\") {
s = strings.Replace(s, "\\", "/", -1)
}
if !strings.HasPrefix(s, "/") {
s = "/" + s
}
return s
}