Refine embedfs

This commit is contained in:
Philipp Heckel 2021-11-29 11:10:12 -05:00
parent 28f4e1e55e
commit ccccae9aad

View file

@ -13,25 +13,26 @@ type CachingEmbedFS struct {
FS embed.FS
}
func (e CachingEmbedFS) Open(name string) (fs.File, error) {
f, err := e.FS.Open(name)
func (f CachingEmbedFS) Open(name string) (fs.File, error) {
file, err := f.FS.Open(name)
if err != nil {
return nil, err
}
return &cachingEmbedFile{f, e.ModTime}, nil
stat, err := file.Stat()
if err != nil {
return nil, err
}
return &cachingEmbedFile{file, f.ModTime, stat}, nil
}
type cachingEmbedFile struct {
file fs.File
modTime time.Time
fs.FileInfo
}
func (f cachingEmbedFile) Stat() (fs.FileInfo, error) {
s, err := f.file.Stat()
if err != nil {
return nil, err
}
return &etagEmbedFileInfo{s, f.modTime}, nil
return f, nil
}
func (f cachingEmbedFile) Read(bytes []byte) (int, error) {
@ -45,35 +46,10 @@ func (f *cachingEmbedFile) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("io.Seeker not implemented")
}
func (f cachingEmbedFile) ModTime() time.Time {
return f.modTime // We override this!
}
func (f cachingEmbedFile) Close() error {
return f.file.Close()
}
type etagEmbedFileInfo struct {
file fs.FileInfo
modTime time.Time
}
func (e etagEmbedFileInfo) Name() string {
return e.file.Name()
}
func (e etagEmbedFileInfo) Size() int64 {
return e.file.Size()
}
func (e etagEmbedFileInfo) Mode() fs.FileMode {
return e.file.Mode()
}
func (e etagEmbedFileInfo) ModTime() time.Time {
return e.modTime // We override this!
}
func (e etagEmbedFileInfo) IsDir() bool {
return e.file.IsDir()
}
func (e etagEmbedFileInfo) Sys() interface{} {
return e.file.Sys()
}