ntfy/util/embedfs.go

67 lines
1.5 KiB
Go
Raw Permalink Normal View History

package util
import (
"embed"
"errors"
"io"
"io/fs"
"time"
)
2021-12-07 17:45:15 +01:00
// CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the
// default static file server can send 304s back. It can be used like this:
//
2022-09-27 18:37:02 +02:00
// var (
// //go:embed docs
// docsStaticFs embed.FS
// docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
// )
2021-12-07 17:45:15 +01:00
//
2022-09-27 18:37:02 +02:00
// http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)
type CachingEmbedFS struct {
ModTime time.Time
FS embed.FS
}
2021-12-07 17:45:15 +01:00
// Open opens a file in the embedded filesystem and returns a fs.File with the static ModTime
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()
}