Moved successive and filter arguments to exclusively query parsms

This commit is contained in:
Seednode 2022-09-24 20:54:51 -05:00
parent 159207dd5f
commit b51c8afc15
4 changed files with 26 additions and 27 deletions

View File

@ -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]

View File

@ -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)
}

View File

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

View File

@ -54,7 +54,7 @@ func serveHtml(w http.ResponseWriter, r http.Request, filePath string) error {
htmlBody += `</title>
</head>
<body>`
htmlBody += fmt.Sprintf(`"<a href="/?filter=%v"><img src="`, r.URL.Query().Get("filter"))
htmlBody += fmt.Sprintf(`"<a href="/?f=%v&s=%v"><img src="`, r.URL.Query().Get("f"), r.URL.Query().Get("s"))
htmlBody += PREFIX + filePath
htmlBody += `"></img></a>
</body>
@ -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