Added symlink handling

This commit is contained in:
Seednode 2022-10-25 00:06:57 -05:00
parent c52ace1fdb
commit 0434d87b3e
3 changed files with 31 additions and 17 deletions

View File

@ -23,6 +23,7 @@ import (
var ( var (
ErrNoImagesFound = fmt.Errorf("no supported image formats found") ErrNoImagesFound = fmt.Errorf("no supported image formats found")
extensions = [5]string{".jpg", ".jpeg", ".png", ".gif", ".webp"}
) )
type Files struct { type Files struct {
@ -79,8 +80,6 @@ func (p *Path) Decrement() {
} }
func preparePath(path string) string { func preparePath(path string) string {
path = filepath.Clean(path)
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
path = fmt.Sprintf("/%v", filepath.ToSlash(path)) path = fmt.Sprintf("/%v", filepath.ToSlash(path))
} }
@ -227,12 +226,10 @@ func splitPath(path string, re regexp.Regexp) (*Path, error) {
} }
func tryExtensions(p *Path) (string, error) { func tryExtensions(p *Path) (string, error) {
extensions := [6]string{p.Extension, ".jpg", ".jpeg", ".png", ".gif", ".webp"}
var fileName string var fileName string
for _, i := range extensions { for _, extension := range extensions {
fileName = fmt.Sprintf("%v%.3d%v", p.Base, p.Number, i) fileName = fmt.Sprintf("%v%.3d%v", p.Base, p.Number, extension)
exists, err := fileExists(fileName) exists, err := fileExists(fileName)
if err != nil { if err != nil {
@ -268,16 +265,19 @@ func pathIsValid(filePath string, paths []string) bool {
} }
} }
if Verbose && !matchesPrefix { switch {
case Verbose && !matchesPrefix:
fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n", fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n",
time.Now().Format(LOGDATE), time.Now().Format(LOGDATE),
filePath, filePath,
) )
return false return false
case !matchesPrefix:
return false
default:
return true
} }
return true
} }
func isImage(path string) (bool, error) { func isImage(path string) (bool, error) {
@ -447,14 +447,29 @@ func pickFile(args []string, filters *Filters, sort string) (string, error) {
func normalizePaths(args []string) ([]string, error) { func normalizePaths(args []string) ([]string, error) {
var paths []string var paths []string
fmt.Println("Paths:")
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
absolutePath, err := filepath.Abs(args[i]) path, err := filepath.EvalSymlinks(args[i])
if err != nil { if err != nil {
return nil, err return nil, err
} }
absolutePath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
if (args[i]) != absolutePath {
fmt.Printf("%v (resolved to %v)\n", args[i], absolutePath)
} else {
fmt.Printf("%v\n", args[i])
}
paths = append(paths, absolutePath) paths = append(paths, absolutePath)
} }
fmt.Println()
return paths, nil return paths, nil
} }

View File

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

View File

@ -188,7 +188,10 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return err return err
} }
filePath := filepath.Clean(strings.TrimPrefix(prefixedFilePath, PREFIX)) filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, PREFIX))
if err != nil {
return err
}
if !pathIsValid(filePath, paths) { if !pathIsValid(filePath, paths) {
http.NotFound(w, r) http.NotFound(w, r)
@ -456,17 +459,13 @@ func serveHtmlHandler(paths []string, re regexp.Regexp) appHandler {
func doNothing(http.ResponseWriter, *http.Request) {} func doNothing(http.ResponseWriter, *http.Request) {}
func ServePage(args []string) error { func ServePage(args []string) error {
fmt.Printf("roulette v%v\n", Version) fmt.Printf("roulette v%v\n\n", Version)
paths, err := normalizePaths(args) paths, err := normalizePaths(args)
if err != nil { if err != nil {
return err return err
} }
for _, i := range paths {
fmt.Println("Paths: " + i)
}
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
http.Handle("/", serveHtmlHandler(paths, *re)) http.Handle("/", serveHtmlHandler(paths, *re))