pkger/pkging/mem/file.go

224 lines
3.7 KiB
Go
Raw Permalink Normal View History

2019-09-03 18:29:28 +03:00
package mem
2019-09-01 00:00:24 +03:00
import (
"bytes"
2019-09-21 19:51:29 +03:00
"encoding/json"
2019-09-01 00:00:24 +03:00
"fmt"
"io"
"net/http"
"os"
"path"
2019-09-03 19:41:21 +03:00
"path/filepath"
2019-09-01 00:00:24 +03:00
"strings"
"time"
"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
)
const timeFmt = time.RFC3339Nano
2019-09-02 01:02:45 +03:00
var _ pkging.File = &File{}
2019-09-01 00:00:24 +03:00
type File struct {
2019-09-02 01:02:45 +03:00
info *pkging.FileInfo
2019-09-01 00:00:24 +03:00
her here.Info
2019-10-09 20:21:54 +03:00
path here.Path
2019-09-01 00:00:24 +03:00
data []byte
2019-10-09 20:21:54 +03:00
parent here.Path
2019-09-01 00:00:24 +03:00
writer *bytes.Buffer
reader io.Reader
2019-09-03 18:29:28 +03:00
pkging pkging.Pkger
2019-09-01 00:00:24 +03:00
}
2019-09-21 19:51:29 +03:00
type fJay struct {
Info *pkging.FileInfo `json:"info"`
Her here.Info `json:"her"`
2019-10-09 20:21:54 +03:00
Path here.Path `json:"path"`
2019-09-21 19:51:29 +03:00
Data []byte `json:"data"`
2019-10-09 20:21:54 +03:00
Parent here.Path `json:"parent"`
2019-09-21 19:51:29 +03:00
}
func (f File) MarshalJSON() ([]byte, error) {
return json.Marshal(fJay{
Info: f.info,
Her: f.her,
Path: f.path,
Data: f.data,
Parent: f.parent,
})
}
func (f *File) UnmarshalJSON(b []byte) error {
var y fJay
if err := json.Unmarshal(b, &y); err != nil {
return err
}
f.info = y.Info
f.her = y.Her
f.path = y.Path
f.data = y.Data
f.parent = y.Parent
return nil
}
2019-09-02 01:02:45 +03:00
func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
2019-09-01 00:00:24 +03:00
if sk, ok := f.reader.(io.Seeker); ok {
2019-09-02 01:02:45 +03:00
return sk.Seek(ofpkginget, whence)
2019-09-01 00:00:24 +03:00
}
return 0, nil
}
func (f *File) Close() error {
defer func() {
f.reader = nil
f.writer = nil
}()
if f.reader != nil {
if c, ok := f.reader.(io.Closer); ok {
if err := c.Close(); err != nil {
return err
}
}
}
if f.writer == nil {
return nil
}
f.data = f.writer.Bytes()
fi := f.info
fi.Details.Size = int64(len(f.data))
2019-09-02 01:02:45 +03:00
fi.Details.ModTime = pkging.ModTime(time.Now())
2019-09-01 00:00:24 +03:00
f.info = fi
return nil
}
func (f *File) Read(p []byte) (int, error) {
if len(f.data) > 0 && f.reader == nil {
f.reader = bytes.NewReader(f.data)
}
if f.reader != nil {
return f.reader.Read(p)
}
return 0, fmt.Errorf("unable to read %s", f.Name())
}
func (f *File) Write(b []byte) (int, error) {
if f.writer == nil {
f.writer = &bytes.Buffer{}
}
i, err := f.writer.Write(b)
return i, err
}
func (f File) Info() here.Info {
return f.her
}
func (f File) Stat() (os.FileInfo, error) {
if f.info == nil {
return nil, os.ErrNotExist
}
return f.info, nil
}
func (f File) Name() string {
return f.info.Name()
}
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
}
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) String() string {
return f.Path().String()
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
var infos []os.FileInfo
root := f.Path().String()
2019-09-02 01:02:45 +03:00
err := f.pkging.Walk(root, func(path string, info os.FileInfo, err error) error {
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
if count > 0 && len(infos) == count {
return io.EOF
}
2019-09-21 21:05:59 +03:00
if root == path {
return nil
}
2019-09-02 01:02:45 +03:00
pt, err := f.pkging.Parse(path)
2019-09-01 00:00:24 +03:00
if err != nil {
return err
}
if pt.Name == f.parent.Name {
return nil
}
2019-09-03 19:41:21 +03:00
2019-09-03 22:24:16 +03:00
info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
2019-09-01 00:00:24 +03:00
infos = append(infos, info)
2019-09-03 19:41:21 +03:00
if info.IsDir() && path != root {
return filepath.SkipDir
}
2019-09-01 00:00:24 +03:00
return nil
})
if err != nil {
if _, ok := err.(*os.PathError); ok {
return infos, nil
}
if err != io.EOF {
return nil, err
}
}
return infos, nil
}
func (f *File) Open(name string) (http.File, error) {
2019-10-16 00:37:51 +03:00
pt, err := f.her.Parse(name)
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
if pt == f.path {
return f, nil
}
pt.Name = path.Join(f.Path().Name, pt.Name)
2019-09-02 01:02:45 +03:00
di, err := f.pkging.Open(pt.String())
2019-09-01 00:00:24 +03:00
if err != nil {
return nil, err
}
fi, err := di.Stat()
if err != nil {
return nil, err
}
2019-10-16 00:37:51 +03:00
2019-09-01 00:00:24 +03:00
if fi.IsDir() {
d2 := &File{
2019-09-02 01:02:45 +03:00
info: pkging.NewFileInfo(fi),
2019-09-01 00:00:24 +03:00
her: di.Info(),
path: pt,
parent: f.path,
2019-09-02 01:02:45 +03:00
pkging: f.pkging,
2019-09-01 00:00:24 +03:00
}
di = d2
}
return di, nil
}