package src import ( "crypto/sha256" "encoding/hex" "io/fs" "os" "path" "strings" "sync" webui "xteve/html" ) // webFS serves the web interface. It is the embedded copy by default and the // local html/ directory when xTeVe runs with -dev. var webFS fs.FS = webui.FS var webETags sync.Map // path -> ETag, only used when not in dev mode // initWebFS : selects embedded or on-disk web files. func initWebFS(dev bool) { if dev { webFS = os.DirFS("html") return } webFS = webui.FS } // readWebFile : reads a file from the web interface, name relative to html/. func readWebFile(name string) ([]byte, error) { name = strings.TrimPrefix(path.Clean("/"+name), "/") if name == "" || name == "." { return nil, fs.ErrNotExist } return fs.ReadFile(webFS, name) } // webFileETag : content hash used for HTTP caching of static web assets. func webFileETag(name string, content []byte) string { if !System.Dev { if v, ok := webETags.Load(name); ok { return v.(string) } } sum := sha256.Sum256(content) etag := `"` + hex.EncodeToString(sum[:16]) + `"` if !System.Dev { webETags.Store(name, etag) } return etag }