Only generate filter struct once, further simplify switch/case om serveHtmlHandler() into a simple if/else

This commit is contained in:
Seednode 2022-11-05 11:17:31 -05:00
parent b6f7d9c104
commit c213911969
3 changed files with 28 additions and 54 deletions

View File

@ -247,17 +247,22 @@ func getNewFile(paths []string, filters *Filters, sortOrder string, re regexp.Re
return filePath, nil return filePath, nil
} }
func getNextFile(p *Path, sortOrder string) (string, error) { func getNextFile(filePath, sortOrder string, re regexp.Regexp) (string, error) {
path, err := splitPath(filePath, re)
if err != nil {
return "", err
}
switch { switch {
case sortOrder == "asc": case sortOrder == "asc":
p.Increment() path.Increment()
case sortOrder == "desc": case sortOrder == "desc":
p.Decrement() path.Decrement()
default: default:
return "", nil return "", nil
} }
fileName, err := tryExtensions(p) fileName, err := tryExtensions(path)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

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

View File

@ -98,7 +98,7 @@ func splitQueryParams(query string) []string {
return params return params
} }
func generateQueryParams(filters *Filters, sort string) string { func generateQueryParams(filters *Filters, sortOrder string) string {
switch { switch {
case Filter && !Sort: case Filter && !Sort:
return fmt.Sprintf("?include=%v&exclude=%v", return fmt.Sprintf("?include=%v&exclude=%v",
@ -106,12 +106,12 @@ func generateQueryParams(filters *Filters, sort string) string {
filters.GetExcludes(), filters.GetExcludes(),
) )
case !Filter && Sort: case !Filter && Sort:
return fmt.Sprintf("?sort=%v", sort) return fmt.Sprintf("?sort=%v", sortOrder)
case Filter && Sort: case Filter && Sort:
return fmt.Sprintf("?include=%v&exclude=%v&sort=%v", return fmt.Sprintf("?include=%v&exclude=%v&sort=%v",
filters.GetIncludes(), filters.GetIncludes(),
filters.GetExcludes(), filters.GetExcludes(),
sort, sortOrder,
) )
} }
@ -138,17 +138,6 @@ func stripQueryParams(u string) (string, error) {
return escapedUri, nil return escapedUri, nil
} }
func createFilters(includes, excludes []string) Filters {
filters := Filters{}
if Filter {
filters.Includes = includes
filters.Excludes = excludes
}
return filters
}
func generateFilePath(filePath string) string { func generateFilePath(filePath string) string {
htmlBody := PREFIX htmlBody := PREFIX
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -169,16 +158,11 @@ func refererToUri(referer string) string {
return "/" + parts[3] return "/" + parts[3]
} }
func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions string) error { func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions string, filters *Filters) error {
fileName := filepath.Base(filePath) fileName := filepath.Base(filePath)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
filters := Filters{}
if Filter {
filters = createFilters(splitQueryParams(r.URL.Query().Get("include")), splitQueryParams(r.URL.Query().Get("exclude")))
}
htmlBody := `<html lang="en"> htmlBody := `<html lang="en">
<head> <head>
<style> <style>
@ -191,7 +175,7 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions stri
</head> </head>
<body> <body>
<a href="/` <a href="/`
htmlBody += generateQueryParams(&filters, r.URL.Query().Get("sort")) htmlBody += generateQueryParams(filters, r.URL.Query().Get("sort"))
htmlBody += `"><img src="` htmlBody += `"><img src="`
htmlBody += generateFilePath(filePath) htmlBody += generateFilePath(filePath)
htmlBody += `"></img></a> htmlBody += `"></img></a>
@ -272,20 +256,21 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
return err return err
} }
filters := createFilters(splitQueryParams(r.URL.Query().Get("include")), splitQueryParams(r.URL.Query().Get("exclude"))) filters := Filters{}
filters.Includes = splitQueryParams(r.URL.Query().Get("include"))
filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude"))
sortOrder := r.URL.Query().Get("sort") sortOrder := r.URL.Query().Get("sort")
switch { if r.URL.Path == "/" {
case r.URL.Path == "/" && refererUri != "": var filePath string
path, err := splitPath(refererUri, re) var err error
if err != nil {
return err
}
filePath, err := getNextFile(path, sortOrder) if refererUri != "" {
if err != nil { filePath, err = getNextFile(refererUri, sortOrder, re)
return err if err != nil {
return err
}
} }
if filePath == "" { if filePath == "" {
@ -304,22 +289,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, RedirectStatusCode) http.Redirect(w, r, newUrl, RedirectStatusCode)
case r.URL.Path == "/" && refererUri == "": } else {
filePath, err := getNewFile(paths, &filters, sortOrder, re, fileCache)
switch {
case err != nil && err == ErrNoImagesFound:
http.NotFound(w, r)
case err != nil:
return err
}
newUrl := fmt.Sprintf("http://%v%v%v",
r.Host,
preparePath(filePath),
generateQueryParams(&filters, sortOrder),
)
http.Redirect(w, r, newUrl, RedirectStatusCode)
default:
filePath := r.URL.Path filePath := r.URL.Path
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -330,7 +300,6 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
if err != nil { if err != nil {
return err return err
} }
if !exists { if !exists {
notFound(w, r) notFound(w, r)
} }
@ -348,7 +317,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
return err return err
} }
err = serveHtml(w, r, filePath, dimensions) err = serveHtml(w, r, filePath, dimensions, &filters)
if err != nil { if err != nil {
return err return err
} }