Reworked to allow use of back button to return to previous results, via HTTP redirects

This commit is contained in:
Seednode 2022-09-16 13:45:33 -05:00
parent 3febbedacc
commit 4e74d99019
3 changed files with 55 additions and 26 deletions

View File

@ -74,19 +74,19 @@ func getFilesRecursive(path string) ([]string, error) {
return paths, nil return paths, nil
} }
func getFileList(args []string) ([]string, error) { func getFileList(paths []string) ([]string, error) {
fileList := []string{} fileList := []string{}
for i := 0; i < len(args); i++ { for i := 0; i < len(paths); i++ {
if Recursive { if Recursive {
f, err := getFilesRecursive(args[i]) f, err := getFilesRecursive(paths[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
fileList = append(fileList, f...) fileList = append(fileList, f...)
} else { } else {
f, err := getFiles(args[i]) f, err := getFiles(paths[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -98,7 +98,12 @@ func getFileList(args []string) ([]string, error) {
return fileList, nil return fileList, nil
} }
func pickFile(fileList []string) (string, string, error) { func pickFile(args []string) (string, error) {
fileList, err := getFileList(args)
if err != nil {
return "", err
}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] }) rand.Shuffle(len(fileList), func(i, j int) { fileList[i], fileList[j] = fileList[j], fileList[i] })
@ -107,17 +112,16 @@ func pickFile(fileList []string) (string, string, error) {
filePath := fileList[i] filePath := fileList[i]
isImage, err := checkIfImage(filePath) isImage, err := checkIfImage(filePath)
if err != nil { if err != nil {
return "", "", err return "", err
} }
if isImage { if isImage {
fileName := filepath.Base(filePath) return filePath, nil
return fileName, filePath, nil
} }
} }
err := errors.New("no images found") err = errors.New("no images found")
return "", "", err return "", err
} }
func normalizePaths(args []string) ([]string, error) { func normalizePaths(args []string) ([]string, error) {

View File

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

View File

@ -12,6 +12,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -19,17 +20,10 @@ import (
const LOGDATE string = "2006-01-02T15:04:05.000000000-07:00" const LOGDATE string = "2006-01-02T15:04:05.000000000-07:00"
func generatePageHtml(w http.ResponseWriter, r http.Request, paths []string) error { const PREFIX string = "/src"
fileList, err := getFileList(paths)
if err != nil {
return err
}
fileName, filePath, err := pickFile(fileList) func generatePageHtml(w http.ResponseWriter, r http.Request, filePath string) error {
if err != nil { fileName := filepath.Base(filePath)
http.NotFound(w, &r)
return nil
}
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
@ -42,12 +36,12 @@ func generatePageHtml(w http.ResponseWriter, r http.Request, paths []string) err
</head> </head>
<body> <body>
<a href="/"><img src="` <a href="/"><img src="`
htmlBody += filePath htmlBody += PREFIX + filePath
htmlBody += `"></img></a> htmlBody += `"></img></a>
</body> </body>
</html>` </html>`
_, err = io.WriteString(w, htmlBody) _, err := io.WriteString(w, htmlBody)
if err != nil { if err != nil {
return err return err
} }
@ -58,11 +52,13 @@ func generatePageHtml(w http.ResponseWriter, r http.Request, paths []string) err
func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) error { func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) error {
request := r.RequestURI request := r.RequestURI
filePath, err := url.QueryUnescape(request) prefixedFilePath, err := url.QueryUnescape(request)
if err != nil { if err != nil {
return err return err
} }
filePath := strings.TrimPrefix(prefixedFilePath, PREFIX)
var matchesPrefix = false var matchesPrefix = false
for i := 0; i < len(paths); i++ { for i := 0; i < len(paths); i++ {
@ -115,19 +111,44 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro
return nil return nil
} }
func servePageHandler(paths []string) http.HandlerFunc { func serveStaticFileHandler(paths []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
err := generatePageHtml(w, *r, paths)
if err != nil {
log.Fatal(err)
}
} else {
err := serveStaticFile(w, *r, paths) err := serveStaticFile(w, *r, paths)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
}
func servePageHandler(paths []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
filePath, err := pickFile(paths)
if err != nil {
log.Fatal(err)
}
newUrl := r.URL.Host + filePath
http.Redirect(w, r, newUrl, http.StatusSeeOther)
} else {
filePath, err := url.QueryUnescape(r.RequestURI)
if err != nil {
log.Fatal(err)
}
isImage, err := checkIfImage(filePath)
if err != nil {
fmt.Println(err)
http.NotFound(w, r)
}
if isImage {
err := generatePageHtml(w, *r, filePath)
if err != nil {
log.Fatal(err)
}
}
}
} }
} }
@ -139,7 +160,11 @@ func ServePage(args []string) {
log.Fatal(err) log.Fatal(err)
} }
for _, i := range paths {
fmt.Println("Paths: " + i)
}
http.HandleFunc("/", servePageHandler(paths)) http.HandleFunc("/", servePageHandler(paths))
http.Handle(PREFIX+"/", http.StripPrefix(PREFIX, serveStaticFileHandler(paths)))
http.HandleFunc("/favicon.ico", doNothing) http.HandleFunc("/favicon.ico", doNothing)
port := strconv.Itoa(Port) port := strconv.Itoa(Port)