pkger/pkging/osware/file.go

89 lines
1.3 KiB
Go
Raw Normal View History

2019-09-02 00:58:20 +03:00
package hdware
2019-09-01 00:00:24 +03:00
import (
"net/http"
"os"
"github.com/markbates/pkger/here"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-01 00:00:24 +03:00
)
2019-09-02 01:02:45 +03:00
var _ pkging.File = &File{}
2019-09-01 00:00:24 +03:00
type File struct {
*os.File
2019-09-02 01:02:45 +03:00
info *pkging.FileInfo
her here.Info
path pkging.Path
pkging pkging.Warehouse
2019-09-01 00:00:24 +03:00
}
2019-09-02 01:02:45 +03:00
func NewFile(fx pkging.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-02 01:02:45 +03:00
File: osf,
path: pt,
pkging: fx,
2019-09-01 00:00:24 +03:00
}
2019-09-02 01:02:45 +03:00
f.info = pkging.WithName(pt.Name, info)
2019-09-01 00:00:24 +03:00
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) {
2019-09-02 01:02:45 +03:00
return f.pkging.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
}
2019-09-02 01:02:45 +03:00
func (f *File) Path() pkging.Path {
2019-09-01 00:00:24 +03:00
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-02 01:02:45 +03:00
f.info = pkging.NewFileInfo(info)
2019-09-01 23:18:39 +03:00
return info, nil
2019-09-01 00:00:24 +03:00
}