Refine embedfs

pull/29/head
Philipp Heckel 2021-11-29 11:10:12 -05:00
parent 28f4e1e55e
commit ccccae9aad
1 changed files with 13 additions and 37 deletions

View File

@ -13,25 +13,26 @@ type CachingEmbedFS struct {
FS embed.FS FS embed.FS
} }
func (e CachingEmbedFS) Open(name string) (fs.File, error) { func (f CachingEmbedFS) Open(name string) (fs.File, error) {
f, err := e.FS.Open(name) file, err := f.FS.Open(name)
if err != nil { if err != nil {
return nil, err 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 { type cachingEmbedFile struct {
file fs.File file fs.File
modTime time.Time modTime time.Time
fs.FileInfo
} }
func (f cachingEmbedFile) Stat() (fs.FileInfo, error) { func (f cachingEmbedFile) Stat() (fs.FileInfo, error) {
s, err := f.file.Stat() return f, nil
if err != nil {
return nil, err
}
return &etagEmbedFileInfo{s, f.modTime}, nil
} }
func (f cachingEmbedFile) Read(bytes []byte) (int, error) { 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") 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 { func (f cachingEmbedFile) Close() error {
return f.file.Close() 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()
}