Fixed issue where having empty include but non-empty exclude caused 404

This commit is contained in:
Seednode 2022-10-20 10:38:32 -05:00
parent 5c1f27a3ad
commit 31d16f65b8
3 changed files with 86 additions and 19 deletions

View File

@ -44,13 +44,40 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
directory, filename := filepath.Split(absolutePath) directory, filename := filepath.Split(absolutePath)
if filters.IsEmpty() { filename = strings.ToLower(filename)
switch {
case filters.HasIncludes() && !filters.HasExcludes():
for i := 0; i < len(filters.Includes); i++ {
if strings.Contains(
filename,
filters.Includes[i],
) {
m[directory] = append(m[directory], path) m[directory] = append(m[directory], path)
} else {
return m, nil
}
}
return m, nil
case !filters.HasIncludes() && filters.HasExcludes():
for i := 0; i < len(filters.Excludes); i++ { for i := 0; i < len(filters.Excludes); i++ {
if strings.Contains( if strings.Contains(
strings.ToLower(filename), filename,
strings.ToLower(filters.Excludes[i]), filters.Excludes[i],
) {
return m, nil
}
}
m[directory] = append(m[directory], path)
return m, nil
case filters.HasIncludes() && filters.HasExcludes():
for i := 0; i < len(filters.Excludes); i++ {
if strings.Contains(
filename,
filters.Excludes[i],
) { ) {
return m, nil return m, nil
} }
@ -58,17 +85,21 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
for i := 0; i < len(filters.Includes); i++ { for i := 0; i < len(filters.Includes); i++ {
if strings.Contains( if strings.Contains(
strings.ToLower(filename), filename,
strings.ToLower(filters.Includes[i]), filters.Includes[i],
) { ) {
m[directory] = append(m[directory], path) m[directory] = append(m[directory], path)
break return m, nil
}
} }
} }
return m, nil return m, nil
default:
m[directory] = append(m[directory], path)
return m, nil
}
} }
func getFirstFile(p *Path) (string, error) { func getFirstFile(p *Path) (string, error) {
@ -227,6 +258,17 @@ func getFiles(m map[string][]string, path string, filters *Filters) (map[string]
return err return err
} }
if Filter && filters.HasExcludes() {
for i := 0; i < len(filters.Excludes); i++ {
if strings.Contains(
strings.ToLower(p),
strings.ToLower(filters.Excludes[i]),
) {
return filepath.SkipDir
}
}
}
switch { switch {
case !Recursive && info.IsDir() && p != path: case !Recursive && info.IsDir() && p != path:
return filepath.SkipDir return filepath.SkipDir

View File

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

View File

@ -22,17 +22,33 @@ type Filters struct {
} }
func (f *Filters) IsEmpty() bool { func (f *Filters) IsEmpty() bool {
if len(f.Includes) == 0 && len(f.Excludes) == 0 { if !f.HasIncludes() && !f.HasExcludes() {
return true return true
} }
return false return false
} }
func (f *Filters) HasIncludes() bool {
if len(f.Includes) == 0 {
return false
}
return true
}
func (f *Filters) GetIncludes() string { func (f *Filters) GetIncludes() string {
return strings.Join(f.Includes, ",") return strings.Join(f.Includes, ",")
} }
func (f *Filters) HasExcludes() bool {
if len(f.Excludes) == 0 {
return false
}
return true
}
func (f *Filters) GetExcludes() string { func (f *Filters) GetExcludes() string {
return strings.Join(f.Excludes, ",") return strings.Join(f.Excludes, ",")
} }
@ -53,7 +69,13 @@ func splitQueryParams(query string) []string {
return []string{} return []string{}
} }
return strings.Split(query, ",") params := strings.Split(query, ",")
for i := 0; i < len(params); i++ {
params[i] = strings.ToLower(params[i])
}
return params
} }
func generateQueryParams(filters *Filters, sort string) string { func generateQueryParams(filters *Filters, sort string) string {
@ -114,7 +136,8 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string) error {
case Filter && Sort: case Filter && Sort:
htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v&sort=%v"><img src="`, htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v&sort=%v"><img src="`,
r.URL.Query().Get("include"), r.URL.Query().Get("include"),
r.URL.Query().Get("exclude"), r.URL.Query().Get("sort"), r.URL.Query().Get("exclude"),
r.URL.Query().Get("sort"),
) )
case Filter && !Sort: case Filter && !Sort:
htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v"><img src="`, htmlBody += fmt.Sprintf(`<a href="/?include=%v&exclude=%v"><img src="`,
@ -211,6 +234,8 @@ func serveHtmlHandler(paths []string) appHandler {
if Filter { if Filter {
filters.Includes = splitQueryParams(r.URL.Query().Get("include")) filters.Includes = splitQueryParams(r.URL.Query().Get("include"))
filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude")) filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude"))
} else {
fmt.Println("Filters disabled")
} }
sortOrder := "" sortOrder := ""
@ -262,7 +287,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, filePath,
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, http.StatusSeeOther) http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "": case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "":
filePath, err := pickFile(paths, &filters, sortOrder) filePath, err := pickFile(paths, &filters, sortOrder)
if err != nil && err == ErrNoImagesFound { if err != nil && err == ErrNoImagesFound {
@ -288,7 +313,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, filePath,
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, http.StatusSeeOther) http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
case r.URL.Path == "/" && sortOrder == "desc" && refererUri != "": case r.URL.Path == "/" && sortOrder == "desc" && refererUri != "":
query, err := url.QueryUnescape(refererUri) query, err := url.QueryUnescape(refererUri)
if err != nil { if err != nil {
@ -332,7 +357,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, filePath,
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, http.StatusSeeOther) http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
case r.URL.Path == "/" && sortOrder == "desc" && refererUri == "": case r.URL.Path == "/" && sortOrder == "desc" && refererUri == "":
filePath, err := pickFile(paths, &filters, sortOrder) filePath, err := pickFile(paths, &filters, sortOrder)
if err != nil && err == ErrNoImagesFound { if err != nil && err == ErrNoImagesFound {
@ -358,7 +383,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, filePath,
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, http.StatusSeeOther) http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
case r.URL.Path == "/": case r.URL.Path == "/":
filePath, err := pickFile(paths, &filters, sortOrder) filePath, err := pickFile(paths, &filters, sortOrder)
if err != nil && err == ErrNoImagesFound { if err != nil && err == ErrNoImagesFound {
@ -374,7 +399,7 @@ func serveHtmlHandler(paths []string) appHandler {
filePath, filePath,
generateQueryParams(&filters, sortOrder), generateQueryParams(&filters, sortOrder),
) )
http.Redirect(w, r, newUrl, http.StatusSeeOther) http.Redirect(w, r, newUrl, http.StatusTemporaryRedirect)
default: default:
filePath := r.URL.Path filePath := r.URL.Path