pkger/http.go

55 lines
824 B
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"
"os"
"path"
"path/filepath"
2019-08-09 05:35:01 +03:00
"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 {
if filepath.Base(name) == "index.html" {
if _, ok := err.(*os.PathError); ok {
return f, nil
}
}
return f, err
}
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 05:35:01 +03:00
info: WithName(strings.TrimPrefix(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
}