pkger/open.go

82 lines
1.2 KiB
Go
Raw Normal View History

2019-07-31 00:21:26 +03:00
package pkger
2019-08-09 04:51:58 +03:00
import (
"net/http"
2019-08-09 22:43:58 +03:00
"os"
2019-08-09 04:51:58 +03:00
"path"
2019-08-09 22:43:58 +03:00
"path/filepath"
"strings"
2019-08-09 04:51:58 +03:00
)
func (f *File) Open(name string) (http.File, error) {
pt, err := Parse(name)
if err != nil {
return nil, err
}
if pt == f.path {
return f, nil
}
pt.Name = path.Join(f.Path().Name, pt.Name)
2019-08-09 05:35:01 +03:00
di, err := Open(pt.String())
2019-08-09 04:51:58 +03:00
if err != nil {
2019-08-09 22:30:12 +03:00
return nil, err
}
fi, err := di.Stat()
if err != nil {
return nil, err
}
if fi.IsDir() {
di.parent = f.path
di.excludes = f.excludes
2019-08-09 04:51:58 +03:00
}
return di, nil
}
2019-08-09 05:35:01 +03:00
func Open(name string) (*File, error) {
pt, err := Parse(name)
if err != nil {
return nil, err
}
dubeg("Open", pt.String())
f, ok := filesCache.Load(pt)
2019-08-09 04:51:58 +03:00
if !ok {
2019-08-09 05:35:01 +03:00
return openDisk(pt)
2019-08-09 04:51:58 +03:00
}
nf := &File{
2019-08-09 22:30:12 +03:00
info: WithName(f.info.Name(), f.info),
2019-08-09 04:51:58 +03:00
path: f.path,
data: f.data,
her: f.her,
}
return nf, nil
}
2019-08-09 22:43:58 +03:00
func openDisk(pt Path) (*File, error) {
dubeg("openDisk", pt.String())
info, err := Info(pt.Pkg)
if err != nil {
return nil, err
}
fp := info.Dir
if len(pt.Name) > 0 {
fp = filepath.Join(fp, pt.Name)
}
fi, err := os.Stat(fp)
if err != nil {
return nil, err
}
f := &File{
info: WithName(strings.TrimPrefix(pt.Name, "/"), NewFileInfo(fi)),
her: info,
path: pt,
}
return f, nil
}