Replaced const string template with strings.Builder, to be hopefully somewhat more readable while still being moderately efficient

This commit is contained in:
Seednode 2022-11-09 23:33:13 -06:00
parent 43a515c896
commit f12b800a71
2 changed files with 20 additions and 23 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)
var Version = "0.24.0"
var Version = "0.24.1"
func init() {
rootCmd.AddCommand(versionCmd)

View File

@ -25,10 +25,6 @@ const (
LogDate string = `2006-01-02T15:04:05.000-07:00`
Prefix string = `/src`
RedirectStatusCode int = http.StatusSeeOther
// You may not like it, but this is what peak string generation looks like.
RefreshScriptTemplate string = `<script>setTimeout(function(){window.location.href = '/%v';},%v);</script>`
HtmlTemplate string = `<html lang="en"><head><style>a{display:block;height:100%%;width:100%%;text-decoration:none;}img{max-width:100%%;max-height:97vh;object-fit:contain;}</style><title>%v (%vx%v)</title></head><body><a href="/%v"><img src="%v" width="%v" height="%v" alt="Roulette selected: %v"></img></a>%v</body></html>`
)
type Filters struct {
@ -204,33 +200,34 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
queryParams, err := generateQueryParams(filters, r.URL.Query().Get("sort"), refreshInterval)
refreshScript := ""
var htmlBody strings.Builder
htmlBody.WriteString(`<html lang="en"><head>`)
htmlBody.WriteString(`<style>a{display:block;height:100%;width:100%;text-decoration:none;}`)
htmlBody.WriteString(`img{max-width:100%;max-height:97vh;object-fit:contain;}</style>`)
htmlBody.WriteString(fmt.Sprintf(`<title>%v (%vx%v)</title>`,
fileName,
dimensions.Width,
dimensions.Height))
htmlBody.WriteString(`</head><body>`)
htmlBody.WriteString(fmt.Sprintf(`<a href="/%v"><img src="%v" width="%v" height="%v" alt="Roulette selected: %v"></img></a>`,
queryParams,
generateFilePath(filePath),
dimensions.Width,
dimensions.Height,
fileName))
if refreshInterval != "0" {
r, err := strconv.Atoi(refreshInterval)
if err != nil {
return err
}
refreshTimer := strconv.Itoa(r * 1000)
refreshScript = fmt.Sprintf(RefreshScriptTemplate,
htmlBody.WriteString(fmt.Sprintf(`<script>setTimeout(function(){window.location.href = '/%v';},%v);</script>`,
queryParams,
refreshTimer)
refreshTimer))
}
htmlBody.WriteString(`</body></html>`)
htmlBody := fmt.Sprintf(HtmlTemplate,
fileName,
dimensions.Width,
dimensions.Height,
queryParams,
generateFilePath(filePath),
dimensions.Width,
dimensions.Height,
fileName,
refreshScript,
)
formattedBody := gohtml.Format(htmlBody)
_, err = io.WriteString(w, formattedBody)
_, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil {
return err
}