pkger/fs/hdwh/file.go

89 lines
1.2 KiB
Go
Raw Normal View History

2019-09-02 00:52:06 +03:00
package hdwh
2019-09-01 00:00:24 +03:00
import (
"net/http"
"os"
"github.com/markbates/pkger/fs"
"github.com/markbates/pkger/here"
)
var _ fs.File = &File{}
type File struct {
*os.File
2019-09-01 23:18:39 +03:00
info *fs.FileInfo
her here.Info
path fs.Path
2019-09-02 00:47:10 +03:00
fs fs.Warehouse
2019-09-01 00:00:24 +03:00
}
2019-09-02 00:47:10 +03:00
func NewFile(fx fs.Warehouse, osf *os.File) (*File, error) {
2019-09-01 04:13:35 +03:00
2019-09-01 23:18:39 +03:00
pt, err := fx.Parse(osf.Name())
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
2019-09-01 04:13:35 +03:00
info, err := osf.Stat()
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
f := &File{
2019-09-01 23:18:39 +03:00
File: osf,
path: pt,
fs: fx,
2019-09-01 00:00:24 +03:00
}
f.info = fs.WithName(pt.Name, info)
her, err := here.Package(pt.Pkg)
if err != nil {
return nil, err
}
f.her = her
return f, nil
}
func (f *File) Close() error {
return f.File.Close()
}
2019-09-01 23:18:39 +03:00
func (f *File) Abs() (string, error) {
return f.fs.AbsPath(f.path)
2019-09-01 00:00:24 +03:00
}
func (f *File) Info() here.Info {
return f.her
}
func (f *File) Name() string {
return f.info.Name()
}
func (f *File) Open(name string) (http.File, error) {
return f.File, nil
}
func (f *File) Path() fs.Path {
return f.path
}
func (f *File) Stat() (os.FileInfo, error) {
2019-09-01 23:18:39 +03:00
if f.info != nil {
return f.info, nil
}
abs, err := f.Abs()
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
if err != nil {
return nil, err
2019-09-01 00:00:24 +03:00
}
2019-09-01 23:18:39 +03:00
f.info = fs.NewFileInfo(info)
return info, nil
2019-09-01 00:00:24 +03:00
}