Added automatic reload functionality via query parameters, and rewrote generateQueryParams() to hopefully be more readable

This commit is contained in:
Seednode 2022-11-08 19:24:49 -06:00
parent c213911969
commit ea2e17d265
3 changed files with 86 additions and 21 deletions

View File

@ -48,6 +48,12 @@ If any other (or no) value is provided, the selected file will be random.
Note: These patterns require sequentially-numbered files matching the following pattern: `filename###.extension`. Note: These patterns require sequentially-numbered files matching the following pattern: `filename###.extension`.
## Refresh
If a `refresh=<time in seconds>` query parameter is provided, the page will reload after that interval.
This can be used to generate a sort of slideshow of images.
## Usage output ## Usage output
``` ```
Usage: Usage:

View File

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

View File

@ -98,24 +98,52 @@ func splitQueryParams(query string) []string {
return params return params
} }
func generateQueryParams(filters *Filters, sortOrder string) string { func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) (string, error) {
switch { refresh, err := strconv.Atoi(refreshInterval)
case Filter && !Sort: if err != nil {
return fmt.Sprintf("?include=%v&exclude=%v", return "", err
filters.GetIncludes(),
filters.GetExcludes(),
)
case !Filter && Sort:
return fmt.Sprintf("?sort=%v", sortOrder)
case Filter && Sort:
return fmt.Sprintf("?include=%v&exclude=%v&sort=%v",
filters.GetIncludes(),
filters.GetExcludes(),
sortOrder,
)
} }
return "" var hasParams bool
var queryParams string
if Filter || Sort || (refresh != 0) {
queryParams += "?"
}
if Filter {
queryParams += "include="
if filters.HasIncludes() {
queryParams += filters.GetIncludes()
}
queryParams += "&exclude="
if filters.HasExcludes() {
queryParams += filters.GetExcludes()
}
hasParams = true
}
if Sort {
if hasParams {
queryParams += "&"
}
queryParams += fmt.Sprintf("sort=%v", sortOrder)
hasParams = true
}
if refresh != 0 {
if hasParams {
queryParams += "&"
}
queryParams += fmt.Sprintf("refresh=%v", refresh)
}
return queryParams, nil
} }
func stripQueryParams(u string) (string, error) { func stripQueryParams(u string) (string, error) {
@ -163,6 +191,10 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions stri
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
refreshInterval := r.URL.Query().Get("refresh")
queryParams, err := generateQueryParams(filters, r.URL.Query().Get("sort"), refreshInterval)
htmlBody := `<html lang="en"> htmlBody := `<html lang="en">
<head> <head>
<style> <style>
@ -174,15 +206,32 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions stri
htmlBody += `</title> htmlBody += `</title>
</head> </head>
<body> <body>
<a href="/` `
htmlBody += generateQueryParams(filters, r.URL.Query().Get("sort")) if refreshInterval != "0" {
r, err := strconv.Atoi(refreshInterval)
if err != nil {
return err
}
refreshTimer := strconv.Itoa(r * 1000)
htmlBody += ` <script>
setTimeout(function(){
window.location.href = '`
htmlBody += fmt.Sprintf("/%v", queryParams)
htmlBody += `';
},`
htmlBody += fmt.Sprintf("%v);\n", refreshTimer)
htmlBody += ` </script>
`
}
htmlBody += ` <a href="/`
htmlBody += queryParams
htmlBody += `"><img src="` htmlBody += `"><img src="`
htmlBody += generateFilePath(filePath) htmlBody += generateFilePath(filePath)
htmlBody += `"></img></a> htmlBody += `"></img></a>
</body> </body>
</html>` </html>`
_, err := io.WriteString(w, htmlBody) _, err = io.WriteString(w, htmlBody)
if err != nil { if err != nil {
return err return err
} }
@ -262,6 +311,11 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
sortOrder := r.URL.Query().Get("sort") sortOrder := r.URL.Query().Get("sort")
refreshInterval := r.URL.Query().Get("refresh")
if refreshInterval == "" {
refreshInterval = "0"
}
if r.URL.Path == "/" { if r.URL.Path == "/" {
var filePath string var filePath string
var err error var err error
@ -283,10 +337,15 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
} }
} }
queryParams, err := generateQueryParams(&filters, sortOrder, refreshInterval)
if err != nil {
return err
}
newUrl := fmt.Sprintf("http://%v%v%v", newUrl := fmt.Sprintf("http://%v%v%v",
r.Host, r.Host,
preparePath(filePath), preparePath(filePath),
generateQueryParams(&filters, sortOrder), queryParams,
) )
http.Redirect(w, r, newUrl, RedirectStatusCode) http.Redirect(w, r, newUrl, RedirectStatusCode)
} else { } else {