2021-11-29 15:34:43 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CachingEmbedFS struct {
|
|
|
|
ModTime time.Time
|
|
|
|
FS embed.FS
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:10:12 +01:00
|
|
|
func (f CachingEmbedFS) Open(name string) (fs.File, error) {
|
|
|
|
file, err := f.FS.Open(name)
|
2021-11-29 15:34:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-29 17:10:12 +01:00
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cachingEmbedFile{file, f.ModTime, stat}, nil
|
2021-11-29 15:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachingEmbedFile struct {
|
|
|
|
file fs.File
|
|
|
|
modTime time.Time
|
2021-11-29 17:10:12 +01:00
|
|
|
fs.FileInfo
|
2021-11-29 15:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f cachingEmbedFile) Stat() (fs.FileInfo, error) {
|
2021-11-29 17:10:12 +01:00
|
|
|
return f, nil
|
2021-11-29 15:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f cachingEmbedFile) Read(bytes []byte) (int, error) {
|
|
|
|
return f.file.Read(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *cachingEmbedFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
if seeker, ok := f.file.(io.Seeker); ok {
|
|
|
|
return seeker.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
return 0, errors.New("io.Seeker not implemented")
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:10:12 +01:00
|
|
|
func (f cachingEmbedFile) ModTime() time.Time {
|
|
|
|
return f.modTime // We override this!
|
2021-11-29 15:34:43 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:10:12 +01:00
|
|
|
func (f cachingEmbedFile) Close() error {
|
|
|
|
return f.file.Close()
|
2021-11-29 15:34:43 +01:00
|
|
|
}
|