diff --git a/cmd/files.go b/cmd/files.go index 36430ac..bae94c2 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -30,7 +30,7 @@ import ( ) var ( - ErrNoImagesFound = fmt.Errorf("no supported image formats found") + ErrNoImagesFound = fmt.Errorf("no supported image formats found which match all criteria") extensions = [6]string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"} ) @@ -107,6 +107,9 @@ func humanReadableSize(bytes int) string { func getImageDimensions(path string) (string, error) { file, err := os.Open(path) + if err != nil { + return "", err + } defer file.Close() myImage, _, err := image.DecodeConfig(file) @@ -200,60 +203,60 @@ func appendPaths(path string, files *Files, filters *Filters, stats *Stats) erro return nil } -func getFirstFile(p *Path) (string, error) { - p.Number = 1 +func getNewFile(paths []string, filters *Filters, sortOrder string, re regexp.Regexp, fileCache *[]string) (string, error) { + filePath, err := pickFile(paths, filters, sortOrder, fileCache) + if err != nil { + return "", nil + } - fileName, err := tryExtensions(p) + path, err := splitPath(filePath, re) if err != nil { return "", err } - return fileName, nil -} + path.Number = 1 -func getLastFile(p *Path) (string, error) { - var fileName string - var err error - - p.Number = 1 - - for { - p.Increment() - - fileName, err = tryExtensions(p) + switch { + case sortOrder == "asc": + filePath, err = tryExtensions(path) if err != nil { return "", err } + case sortOrder == "desc": + for { + path.Increment() - if fileName == "" { - p.Decrement() - - fileName, err = tryExtensions(p) + filePath, err = tryExtensions(path) if err != nil { return "", err } - break + if filePath == "" { + path.Decrement() + + filePath, err = tryExtensions(path) + if err != nil { + return "", err + } + + break + } } } - return fileName, nil + return filePath, nil } -func getNextFile(p *Path) (string, error) { - p.Increment() - - fileName, err := tryExtensions(p) - if err != nil { - return "", err +func getNextFile(p *Path, sortOrder string) (string, error) { + switch { + case sortOrder == "asc": + p.Increment() + case sortOrder == "desc": + p.Decrement() + default: + return "", nil } - return fileName, err -} - -func getPreviousFile(p *Path) (string, error) { - p.Decrement() - fileName, err := tryExtensions(p) if err != nil { return "", err @@ -497,6 +500,9 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string) } fileCount := len(fileList) + if fileCount == 0 { + return "", ErrNoImagesFound + } r := rand.Intn(fileCount - 1) diff --git a/cmd/version.go b/cmd/version.go index 0ca78ee..affd806 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.21.1" +var Version = "0.22.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index e64e78a..859865d 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -31,19 +31,11 @@ type Filters struct { } func (f *Filters) IsEmpty() bool { - if !(f.HasIncludes() && f.HasExcludes()) { - return true - } - - return false + return !(f.HasIncludes() && f.HasExcludes()) } func (f *Filters) HasIncludes() bool { - if len(f.Includes) == 0 { - return false - } - - return true + return len(f.Includes) != 0 } func (f *Filters) GetIncludes() string { @@ -51,11 +43,7 @@ func (f *Filters) GetIncludes() string { } func (f *Filters) HasExcludes() bool { - if len(f.Excludes) == 0 { - return false - } - - return true + return len(f.Excludes) != 0 } func (f *Filters) GetExcludes() string { @@ -130,19 +118,45 @@ func generateQueryParams(filters *Filters, sort string) string { return "" } -func stripQueryParams(inUrl string) (string, error) { - url, err := url.Parse(inUrl) +func stripQueryParams(u string) (string, error) { + uri, err := url.Parse(u) if err != nil { return "", err } - url.RawQuery = "" + uri.RawQuery = "" - if runtime.GOOS == "windows" { - return strings.TrimPrefix(url.String(), "/"), nil + escapedUri, err := url.QueryUnescape(uri.String()) + if err != nil { + return "", err } - return url.String(), nil + if runtime.GOOS == "windows" { + return strings.TrimPrefix(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 { + htmlBody := PREFIX + if runtime.GOOS == "windows" { + htmlBody += "/" + } + htmlBody += filePath + + return htmlBody } func refererToUri(referer string) string { @@ -160,6 +174,11 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions stri 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 := `