From c21391196952d3e7e89252e91165b81d63143b46 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 5 Nov 2022 11:17:31 -0500 Subject: [PATCH] Only generate filter struct once, further simplify switch/case om serveHtmlHandler() into a simple if/else --- cmd/files.go | 13 +++++++--- cmd/version.go | 2 +- cmd/web.go | 67 ++++++++++++++------------------------------------ 3 files changed, 28 insertions(+), 54 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index bae94c2..5e2320f 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -247,17 +247,22 @@ func getNewFile(paths []string, filters *Filters, sortOrder string, re regexp.Re 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 { case sortOrder == "asc": - p.Increment() + path.Increment() case sortOrder == "desc": - p.Decrement() + path.Decrement() default: return "", nil } - fileName, err := tryExtensions(p) + fileName, err := tryExtensions(path) if err != nil { return "", err } diff --git a/cmd/version.go b/cmd/version.go index affd806..7ec7b2b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.22.0" +var Version = "0.22.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 859865d..5766c64 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -98,7 +98,7 @@ func splitQueryParams(query string) []string { return params } -func generateQueryParams(filters *Filters, sort string) string { +func generateQueryParams(filters *Filters, sortOrder string) string { switch { case Filter && !Sort: return fmt.Sprintf("?include=%v&exclude=%v", @@ -106,12 +106,12 @@ func generateQueryParams(filters *Filters, sort string) string { filters.GetExcludes(), ) case !Filter && Sort: - return fmt.Sprintf("?sort=%v", sort) + return fmt.Sprintf("?sort=%v", sortOrder) case Filter && Sort: return fmt.Sprintf("?include=%v&exclude=%v&sort=%v", filters.GetIncludes(), filters.GetExcludes(), - sort, + sortOrder, ) } @@ -138,17 +138,6 @@ func stripQueryParams(u string) (string, error) { 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 { htmlBody := PREFIX if runtime.GOOS == "windows" { @@ -169,16 +158,11 @@ func refererToUri(referer string) string { 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) 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 := `