Skip to content

Commit

Permalink
Merge pull request #63 from xushiwei/cached
Browse files Browse the repository at this point in the history
x/http/fs: remove dataFileInfo
  • Loading branch information
xushiwei authored Sep 3, 2023
2 parents 93418ba + e936ec1 commit fc27c18
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
61 changes: 30 additions & 31 deletions http/fs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,59 @@ type ContentReader interface {
Size() int64
}

type dataFileInfo struct {
r ContentReader
type dataFile struct {
ContentReader
name string
}

func (p *dataFileInfo) Name() string {
return path.Base(p.name)
func (p *dataFile) Close() error {
if r, ok := p.ContentReader.(io.Closer); ok {
return r.Close()
}
return nil
}

func (p *dataFileInfo) Size() int64 {
return p.r.Size()
func (p *dataFile) ReadDir(n int) ([]fs.DirEntry, error) {
return nil, os.ErrInvalid
}

func (p *dataFileInfo) Mode() fs.FileMode {
return 0
func (p *dataFile) Readdir(count int) ([]fs.FileInfo, error) {
return nil, os.ErrInvalid
}

func (p *dataFileInfo) ModTime() time.Time {
if r, ok := p.r.(interface{ ModTime() time.Time }); ok {
return r.ModTime()
}
return time.Now()
func (p *dataFile) IsDir() bool {
return false
}

func (p *dataFileInfo) IsDir() bool {
return false
func (p *dataFile) Mode() fs.FileMode {
return 0
}

func (p *dataFileInfo) Sys() interface{} {
return nil
func (p *dataFile) Stat() (fs.FileInfo, error) {
return p, nil
}

type dataFile struct {
ContentReader
name string
func (p *dataFile) Name() string {
return path.Base(p.name)
}

func (p *dataFile) Close() error {
if r, ok := p.ContentReader.(io.Closer); ok {
return r.Close()
}
return nil
func (p *dataFile) FullName() string {
return p.name
}

func (p *dataFile) ReadDir(n int) ([]fs.DirEntry, error) {
return nil, os.ErrInvalid
func (p *dataFile) Size() int64 {
return p.ContentReader.Size()
}

func (p *dataFile) Readdir(count int) ([]fs.FileInfo, error) {
return nil, os.ErrInvalid
func (p *dataFile) ModTime() time.Time {
if r, ok := p.ContentReader.(interface{ ModTime() time.Time }); ok {
return r.ModTime()
}
return time.Now()
}

func (p *dataFile) Stat() (fs.FileInfo, error) {
return &dataFileInfo{p.ContentReader, p.name}, nil
func (p *dataFile) Sys() interface{} {
return nil
}

// File implements a http.File by a ContentReader which may implement
Expand Down
45 changes: 43 additions & 2 deletions http/fs/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/fs"
"net/http"
"os"
"path"
"time"
)

Expand Down Expand Up @@ -48,8 +49,24 @@ func (p *stream) Readdir(count int) ([]fs.FileInfo, error) {
return nil, os.ErrInvalid
}

func (p *stream) IsDir() bool {
return false
}

func (p *stream) Mode() fs.FileMode {
return 0
}

func (p *stream) Name() string {
return path.Base(p.name)
}

func (p *stream) FullName() string {
return p.name
}

func (p *stream) Stat() (fs.FileInfo, error) {
return &dataFileInfo{p, p.name}, nil
return p, nil
}

func (p *stream) Read(b []byte) (n int, err error) {
Expand Down Expand Up @@ -93,6 +110,10 @@ func (p *stream) ModTime() time.Time {
return time.Now()
}

func (p *stream) Sys() interface{} {
return nil
}

// SequenceFile implements a http.File by a io.ReadCloser object.
func SequenceFile(name string, body io.ReadCloser) http.File {
return &stream{file: body}
Expand Down Expand Up @@ -121,8 +142,24 @@ func (p *httpFile) Readdir(count int) ([]fs.FileInfo, error) {
return nil, os.ErrInvalid
}

func (p *httpFile) IsDir() bool {
return false
}

func (p *httpFile) Mode() fs.FileMode {
return 0
}

func (p *httpFile) Name() string {
return path.Base(p.name)
}

func (p *httpFile) FullName() string {
return p.name
}

func (p *httpFile) Stat() (fs.FileInfo, error) {
return &dataFileInfo{p, p.name}, nil
return p, nil
}

func (p *httpFile) Read(b []byte) (n int, err error) {
Expand Down Expand Up @@ -165,6 +202,10 @@ func (p *httpFile) ModTime() time.Time {
return time.Now()
}

func (p *httpFile) Sys() interface{} {
return nil
}

// HttpFile implements a http.File by a http.Response object.
func HttpFile(name string, resp *http.Response) http.File {
return &httpFile{file: resp.Body, resp: resp, name: name}
Expand Down

0 comments on commit fc27c18

Please sign in to comment.