owncast/static/static.go

79 lines
1.6 KiB
Go
Raw Normal View History

package static
import (
"embed"
"html/template"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
)
//go:embed web/*
//go:embed web/_next/static
//go:embed web/_next/static/chunks/pages/*.js
//go:embed web/_next/static/*/*.js
var webFiles embed.FS
// GetWeb will return an embedded filesystem reference to the admin web app.
func GetWeb() fs.FS {
wf, err := fs.Sub(webFiles, "web")
if err != nil {
log.Fatal(err)
}
return wf
}
//go:embed img/emoji/*
var emojiFiles embed.FS
// GetEmoji will return the emoji files.
func GetEmoji() fs.FS {
ef, err := fs.Sub(emojiFiles, "img/emoji")
if err != nil {
log.Fatal(err)
}
return ef
}
// GetWebIndexTemplate will return the bot/scraper metadata template.
func GetWebIndexTemplate() (*template.Template, error) {
webFiles := GetWeb()
name := "index.html"
t, err := template.ParseFS(webFiles, name)
if err != nil {
return nil, errors.Wrap(err, "unable to open index.html template")
}
tmpl := template.Must(t, err)
return tmpl, err
}
//go:embed offline.ts
var offlineVideoSegment []byte
// GetOfflineSegment will return the offline video segment data.
func GetOfflineSegment() []byte {
return getFileSystemStaticFileOrDefault("offline.ts", offlineVideoSegment)
}
//go:embed img/logo.png
var logo []byte
// GetLogo will return the logo data.
func GetLogo() []byte {
return getFileSystemStaticFileOrDefault("img/logo.png", logo)
}
func getFileSystemStaticFileOrDefault(path string, defaultData []byte) []byte {
fullPath := filepath.Join("static", path)
data, err := os.ReadFile(fullPath) //nolint: gosec
if err != nil {
return defaultData
}
return data
}