Display first image in directory on first load if --successive is enabled
This commit is contained in:
parent
0b7b9983a5
commit
f3280a6b6c
27
cmd/files.go
27
cmd/files.go
|
@ -17,6 +17,33 @@ import (
|
||||||
"github.com/h2non/filetype"
|
"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) {
|
func getNextFile(path string) (string, error) {
|
||||||
re := regexp.MustCompile("(.+)([0-9]{3})(\\..+)")
|
re := regexp.MustCompile("(.+)([0-9]{3})(\\..+)")
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.7.0"
|
var Version = "0.8.0"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
|
61
cmd/web.go
61
cmd/web.go
|
@ -70,13 +70,11 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro
|
||||||
filePath := strings.TrimPrefix(prefixedFilePath, PREFIX)
|
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++ {
|
||||||
if strings.HasPrefix(filePath, paths[i]) {
|
if strings.HasPrefix(filePath, paths[i]) {
|
||||||
matchesPrefix = true
|
matchesPrefix = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if matchesPrefix == false {
|
if matchesPrefix == false {
|
||||||
if Verbose {
|
if Verbose {
|
||||||
fmt.Printf("%v Failed to serve file outside specified path(s): %v", time.Now().Format(LOGDATE), filePath)
|
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
|
var startTime time.Time
|
||||||
|
|
||||||
if Verbose {
|
if Verbose {
|
||||||
startTime = time.Now()
|
startTime = time.Now()
|
||||||
fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath)
|
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 {
|
func serveHtmlHandler(paths []string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.RequestURI == "/" {
|
var filePath string
|
||||||
var filePath string
|
var err error
|
||||||
var err error
|
|
||||||
|
|
||||||
if Successive {
|
refererUri := refererToUri(r.Referer())
|
||||||
refererUri := refererToUri(r.Referer())
|
|
||||||
if refererUri != "" {
|
|
||||||
if Verbose {
|
|
||||||
fmt.Printf("Referer is %v\n", refererUri)
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := url.QueryUnescape(refererUri)
|
switch {
|
||||||
if err != nil {
|
case r.RequestURI == "/" && Successive && refererUri != "":
|
||||||
log.Fatal(err)
|
f, err := url.QueryUnescape(refererUri)
|
||||||
}
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
filePath, err = getNextFile(f)
|
filePath, err = getNextFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if filePath == "" {
|
if filePath == "" {
|
||||||
|
@ -160,11 +151,37 @@ func serveHtmlHandler(paths []string) http.HandlerFunc {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filePath, err = getFirstFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newUrl := r.URL.Host + filePath
|
newUrl := r.URL.Host + filePath
|
||||||
http.Redirect(w, r, newUrl, http.StatusSeeOther)
|
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)
|
filePath, err := url.QueryUnescape(r.RequestURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
Loading…
Reference in New Issue