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)