diff --git a/cmd/files.go b/cmd/files.go index dc3fcb6..042e7b5 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -137,17 +137,12 @@ func getFiles(m map[string][]string, path, filter string) (map[string][]string, switch { case !Recursive && info.IsDir() && p != path: return filepath.SkipDir - case Filter != "" && !info.IsDir(): - m, err = appendPaths(m, p, Filter) - if err != nil { - return err - } case filter != "" && !info.IsDir(): m, err = appendPaths(m, p, filter) if err != nil { return err } - default: + case !info.IsDir(): m, err = appendPaths(m, p, "") if err != nil { return err @@ -190,17 +185,19 @@ func prepareDirectory(directory []string) []string { _, last := filepath.Split(directory[len(directory)-1]) last = cleanFilename(last) + fmt.Printf("Comparing %v to %v\n", first, last) + if first == last { d := append([]string{}, directory[0]) + fmt.Printf("Appending %v to empty directory\n", d) return d } else { + fmt.Printf("Returning directory as-is\n") return directory } } -func prepareDirectories(m map[string][]string) []string { - rand.Seed(time.Now().UnixNano()) - +func prepareDirectories(m map[string][]string, successive string) []string { directories := []string{} keys := make([]string, len(m)) @@ -212,7 +209,8 @@ func prepareDirectories(m map[string][]string) []string { } switch { - case Successive: + case successive != "": + fmt.Println("Successive") for i := 0; i < len(keys); i++ { directories = append(directories, prepareDirectory(m[keys[i]])...) } @@ -225,13 +223,17 @@ func prepareDirectories(m map[string][]string) []string { return directories } -func pickFile(args []string, filter string) (string, error) { +func pickFile(args []string, filter, successive string) (string, error) { fileMap, err := getFileList(args, filter) if err != nil { return "", err } - fileList := prepareDirectories(fileMap) + rand.Seed(time.Now().UnixNano()) + + fileList := prepareDirectories(fileMap, successive) + + rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) for i := 0; i < len(fileList); i++ { filePath := fileList[i] diff --git a/cmd/root.go b/cmd/root.go index e297a16..58181cd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,10 +10,8 @@ import ( "github.com/spf13/cobra" ) -var Filter string var Port int var Recursive bool -var Successive bool var Verbose bool var rootCmd = &cobra.Command{ @@ -33,10 +31,8 @@ func Execute() { } func init() { - rootCmd.Flags().StringVarP(&Filter, "filter", "f", "", "only display images matching specified pattern (case-insensitive)") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") - rootCmd.Flags().BoolVarP(&Successive, "successive", "s", false, "load the next sequential file, if possible") rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "log accessed files to stdout") rootCmd.Flags().SetInterspersed(true) } diff --git a/cmd/version.go b/cmd/version.go index ec45f48..363e588 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.11.0" +var Version = "0.12.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index ae0f7b7..bf18155 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -54,7 +54,7 @@ func serveHtml(w http.ResponseWriter, r http.Request, filePath string) error { htmlBody += `
` - htmlBody += fmt.Sprintf(`" @@ -138,10 +138,11 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { refererUri := stripQueryParam(refererToUri(r.Referer())) - filter := r.URL.Query().Get("filter") + filter := r.URL.Query().Get("f") + successive := r.URL.Query().Get("s") switch { - case r.URL.Path == "/" && Successive && refererUri != "": + case r.URL.Path == "/" && successive != "" && refererUri != "": query, err := url.QueryUnescape(refererUri) if err != nil { log.Fatal(err) @@ -153,7 +154,7 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { } if filePath == "" { - filePath, err = pickFile(paths, filter) + filePath, err = pickFile(paths, filter, successive) switch { case err != nil && err == ErrNoImagesFound: http.NotFound(w, r) @@ -168,10 +169,10 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { } } - newUrl := fmt.Sprintf("%v%v?filter=%v", r.URL.Host, filePath, filter) + newUrl := fmt.Sprintf("%v%v?f=%v&s=%v", r.URL.Host, filePath, filter, successive) http.Redirect(w, r, newUrl, http.StatusSeeOther) - case r.URL.Path == "/" && Successive && refererUri == "": - filePath, err := pickFile(paths, filter) + case r.URL.Path == "/" && successive != "" && refererUri == "": + filePath, err := pickFile(paths, filter, successive) switch { case err != nil && err == ErrNoImagesFound: http.NotFound(w, r) @@ -185,10 +186,10 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { log.Fatal(err) } - newUrl := fmt.Sprintf("%v%v?filter=%v", r.URL.Host, filePath, filter) + newUrl := fmt.Sprintf("%v%v?f=%v&s=%v", r.URL.Host, filePath, filter, successive) http.Redirect(w, r, newUrl, http.StatusSeeOther) case r.URL.Path == "/": - filePath, err := pickFile(paths, filter) + filePath, err := pickFile(paths, filter, successive) switch { case err != nil && err == ErrNoImagesFound: http.NotFound(w, r) @@ -197,7 +198,7 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { log.Fatal(err) } - newUrl := fmt.Sprintf("%v%v?filter=%v", r.URL.Host, filePath, filter) + newUrl := fmt.Sprintf("%v%v?f=%v&s=%v", r.URL.Host, filePath, filter, successive) http.Redirect(w, r, newUrl, http.StatusSeeOther) default: filePath := r.URL.Path