2019-09-03 18:29:28 +03:00
|
|
|
package stdos
|
2019-09-01 00:00:24 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-09-05 19:22:35 +03:00
|
|
|
"path"
|
2019-09-01 00:00:24 +03:00
|
|
|
|
|
|
|
"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
|
2019-10-09 20:21:54 +03:00
|
|
|
path here.Path
|
2019-09-03 18:29:28 +03:00
|
|
|
pkging pkging.Pkger
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-09-22 19:16:17 +03:00
|
|
|
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|
|
|
osinfos, err := f.File.Readdir(count)
|
2019-09-05 19:22:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
infos := make([]os.FileInfo, len(osinfos))
|
|
|
|
for i, info := range osinfos {
|
2019-09-22 00:07:49 +03:00
|
|
|
infos[i] = pkging.WithRelName(info.Name(), info)
|
2019-09-05 19:22:35 +03:00
|
|
|
}
|
|
|
|
return infos, err
|
|
|
|
}
|
2019-09-01 00:00:24 +03:00
|
|
|
func (f *File) Open(name string) (http.File, error) {
|
2019-09-05 19:22:35 +03:00
|
|
|
fp := path.Join(f.Path().Name, name)
|
|
|
|
f2, err := f.pkging.Open(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-22 19:16:17 +03:00
|
|
|
return f2, nil
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 20:21:54 +03:00
|
|
|
func (f *File) Path() here.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
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
info, err := f.File.Stat()
|
2019-09-01 23:18:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|
2019-10-15 23:40:45 +03:00
|
|
|
f.info = pkging.WithName(f.path.Name, info)
|
2019-09-20 17:56:26 +03:00
|
|
|
return f.info, nil
|
2019-09-01 00:00:24 +03:00
|
|
|
}
|