pkger/pkging/mem/file.go

270 lines
4.5 KiB
Go
Raw Normal View History

2019-09-03 18:29:28 +03:00
package mem
2019-09-01 00:00:24 +03:00
import (
"bytes"
"encoding/json"
"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-09-02 01:02:45 +03:00
path pkging.Path
2019-09-01 00:00:24 +03:00
data []byte
2019-09-02 01:02:45 +03:00
parent pkging.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-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-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) String() string {
return f.Path().String()
}
func (f File) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
if st.Flag('+') {
b, err := json.MarshalIndent(f, "", " ")
if err != nil {
fmt.Fprint(os.Stderr, err)
return
}
fmt.Fprint(st, string(b))
return
}
fmt.Fprint(st, f.String())
case 'q':
fmt.Fprintf(st, "%q", f.String())
default:
fmt.Fprint(st, f.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-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-02 01:02:45 +03:00
info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
2019-09-03 22:18:31 +03:00
if minf, ok := info.(*pkging.FileInfo); ok {
minf.Details.Name = strings.TrimPrefix(info.Name(), "/")
info = minf
}
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-09-02 01:02:45 +03:00
pt, err := f.pkging.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
}
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
}
2019-09-03 22:18:31 +03:00
func (f File) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{
"info": f.info,
"her": f.her,
"path": f.path,
"data": f.data,
"parent": f.parent,
}
return json.Marshal(m)
}
func (f *File) UnmarshalJSON(b []byte) error {
m := map[string]json.RawMessage{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
info, ok := m["info"]
if !ok {
return fmt.Errorf("missing info")
}
f.info = &pkging.FileInfo{}
if err := json.Unmarshal(info, f.info); err != nil {
return err
}
her, ok := m["her"]
if !ok {
return fmt.Errorf("missing her")
}
if err := json.Unmarshal(her, &f.her); err != nil {
return err
}
path, ok := m["path"]
if !ok {
return fmt.Errorf("missing path")
}
if err := json.Unmarshal(path, &f.path); err != nil {
return err
}
parent, ok := m["parent"]
if !ok {
return fmt.Errorf("missing parent")
}
if err := json.Unmarshal(parent, &f.parent); err != nil {
return err
}
if err := json.Unmarshal(m["data"], &f.data); err != nil {
return err
}
return nil
}