1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-26 21:25:18 +01:00
ntfy/util/embedfs.go

56 lines
1,003 B
Go
Raw Normal View History

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)
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
}
type cachingEmbedFile struct {
file fs.File
modTime time.Time
2021-11-29 17:10:12 +01:00
fs.FileInfo
}
func (f cachingEmbedFile) Stat() (fs.FileInfo, error) {
2021-11-29 17:10:12 +01:00
return f, nil
}
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 17:10:12 +01:00
func (f cachingEmbedFile) Close() error {
return f.file.Close()
}