From f3280a6b6c2a458346eb9ed76877f01658174ecf Mon Sep 17 00:00:00 2001 From: Seednode Date: Sun, 18 Sep 2022 13:07:46 -0500 Subject: [PATCH] Display first image in directory on first load if --successive is enabled --- cmd/files.go | 27 ++++++++++++++++++++++ cmd/version.go | 2 +- cmd/web.go | 61 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 0650680..22b1d12 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -17,6 +17,33 @@ import ( "github.com/h2non/filetype" ) +func getFirstFile(path string) (string, error) { + re := regexp.MustCompile("(.+)([0-9]{3})(\\..+)") + + split := re.FindAllStringSubmatch(path, -1) + + if len(split) < 1 || len(split[0]) < 3 { + return "", nil + } + + base := split[0][1] + number := 1 + extension := split[0][3] + + fileName := fmt.Sprintf("%v%.3d%v", base, number, extension) + + nextFile, err := checkNextFile(fileName) + if err != nil { + return "", err + } + + if !nextFile { + return "", nil + } + + return fileName, nil +} + func getNextFile(path string) (string, error) { re := regexp.MustCompile("(.+)([0-9]{3})(\\..+)") diff --git a/cmd/version.go b/cmd/version.go index 5137955..1b34417 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.7.0" +var Version = "0.8.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 909571f..6156469 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -70,13 +70,11 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro filePath := strings.TrimPrefix(prefixedFilePath, PREFIX) var matchesPrefix = false - for i := 0; i < len(paths); i++ { if strings.HasPrefix(filePath, paths[i]) { matchesPrefix = true } } - if matchesPrefix == false { if Verbose { fmt.Printf("%v Failed to serve file outside specified path(s): %v", time.Now().Format(LOGDATE), filePath) @@ -101,7 +99,6 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro } var startTime time.Time - if Verbose { startTime = time.Now() fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath) @@ -132,27 +129,21 @@ func serveStaticFileHandler(paths []string) http.HandlerFunc { func serveHtmlHandler(paths []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/" { - var filePath string - var err error + var filePath string + var err error - if Successive { - refererUri := refererToUri(r.Referer()) - if refererUri != "" { - if Verbose { - fmt.Printf("Referer is %v\n", refererUri) - } + refererUri := refererToUri(r.Referer()) - f, err := url.QueryUnescape(refererUri) - if err != nil { - log.Fatal(err) - } + switch { + case r.RequestURI == "/" && Successive && refererUri != "": + f, err := url.QueryUnescape(refererUri) + if err != nil { + log.Fatal(err) + } - filePath, err = getNextFile(f) - if err != nil { - log.Fatal(err) - } - } + filePath, err = getNextFile(f) + if err != nil { + log.Fatal(err) } if filePath == "" { @@ -160,11 +151,37 @@ func serveHtmlHandler(paths []string) http.HandlerFunc { if err != nil { log.Fatal(err) } + + filePath, err = getFirstFile(filePath) + if err != nil { + log.Fatal(err) + } } newUrl := r.URL.Host + filePath http.Redirect(w, r, newUrl, http.StatusSeeOther) - } else { + case r.RequestURI == "/" && Successive && refererUri == "": + filePath, err = pickFile(paths) + if err != nil { + log.Fatal(err) + } + + filePath, err = getFirstFile(filePath) + if err != nil { + log.Fatal(err) + } + + newUrl := r.URL.Host + filePath + http.Redirect(w, r, newUrl, http.StatusSeeOther) + case r.RequestURI == "/": + filePath, err = pickFile(paths) + if err != nil { + log.Fatal(err) + } + + newUrl := r.URL.Host + filePath + http.Redirect(w, r, newUrl, http.StatusSeeOther) + default: filePath, err := url.QueryUnescape(r.RequestURI) if err != nil { log.Fatal(err)