70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"net/url"
|
|
|
|
"github.com/htfy96/reformism"
|
|
"go.fifitido.net/ytdl-web/pkg/view/html"
|
|
"go.fifitido.net/ytdl-web/pkg/ytdl/metadata"
|
|
)
|
|
|
|
//go:embed views/*
|
|
var viewsfs embed.FS
|
|
|
|
var Views = html.New(
|
|
viewsfs,
|
|
html.DefaultOptions().
|
|
WithBaseDir("views").
|
|
WithFunction("unsafe", func(s string) template.HTML {
|
|
return template.HTML(s)
|
|
}).
|
|
WithFunction("queryEscape", func(s string) string {
|
|
return url.QueryEscape(s)
|
|
}).
|
|
WithFunction("jsonMarshal", func(s any) (string, error) {
|
|
j, err := json.MarshalIndent(s, "", " ")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(j), nil
|
|
}).
|
|
WithFunction("downloadContext", func(meta metadata.Metadata, url, basePath string, format metadata.Format) map[string]any {
|
|
return map[string]any{
|
|
"Meta": meta,
|
|
"Url": url,
|
|
"BasePath": basePath,
|
|
"Format": format,
|
|
}
|
|
}).
|
|
WithFunction("sprintf", func(format string, args ...any) string {
|
|
return fmt.Sprintf(format, args...)
|
|
}).
|
|
WithFunction("filesize", func(size *int) string {
|
|
if size == nil {
|
|
return "unknown"
|
|
}
|
|
const unit = 1000
|
|
if *size < unit {
|
|
return fmt.Sprintf("%d B", *size)
|
|
}
|
|
div, exp := int64(unit), 0
|
|
for n := *size / unit; n >= unit; n /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.1f %cB",
|
|
float64(*size)/float64(div), "kMGTPE"[exp])
|
|
}).
|
|
WithFunctions(reformism.FuncsHTML),
|
|
)
|
|
|
|
func init() {
|
|
if err := Views.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|