From 8923a4c14da33471d7b68a2cc52219aed69c78e0 Mon Sep 17 00:00:00 2001 From: Seednode Date: Thu, 14 Sep 2023 19:10:55 -0500 Subject: [PATCH] Fix validation of files when in cache/not cached mode --- cmd/files.go | 56 +++++++++++++------------------------------------- cmd/root.go | 2 +- types/types.go | 9 ++++++++ 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 52fd41c..2a7987c 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -99,23 +99,14 @@ func preparePath(path string) string { return mediaPrefix + path } -func appendPath(directory, path string, files *files, stats *scanStats, formats *types.Types, shouldCache bool) error { - if shouldCache { - format := formats.FileType(path) - if format == nil { - return nil - } - - if !format.Validate(path) { - return nil - } +func appendPath(directory, path string, files *files, stats *scanStats, formats *types.Types, shouldCache bool) { + if shouldCache && !formats.Validate(path) { + return } files.append(directory, path) stats.filesMatched.Add(1) - - return nil } func appendPaths(path string, files *files, filters *filters, stats *scanStats, formats *types.Types) error { @@ -149,10 +140,7 @@ func appendPaths(path string, files *files, filters *filters, stats *scanStats, filename, filters.included[i], ) { - err := appendPath(directory, path, files, stats, formats, shouldCache) - if err != nil { - return err - } + appendPath(directory, path, files, stats, formats, shouldCache) return nil } @@ -163,10 +151,7 @@ func appendPaths(path string, files *files, filters *filters, stats *scanStats, return nil } - err = appendPath(directory, path, files, stats, formats, shouldCache) - if err != nil { - return err - } + appendPath(directory, path, files, stats, formats, shouldCache) return nil } @@ -326,17 +311,10 @@ func pathHasSupportedFiles(path string, formats *types.Types) (bool, error) { switch { case !Recursive && info.IsDir() && p != path: return filepath.SkipDir - case !info.IsDir(): - format := formats.FileType(p) - if format == nil { - return nil - } + case !info.IsDir() && formats.Validate(p): + hasRegisteredFiles <- true - if format.Validate(p) { - hasRegisteredFiles <- true - - return filepath.SkipAll - } + return filepath.SkipAll } return err @@ -553,20 +531,14 @@ func pickFile(args []string, filters *filters, sort string, cache *fileCache, fo path := fileList[val] - if !fromCache { - format := formats.FileType(path) - if format == nil { - return "", nil - } - - if format.Validate(path) { - return path, nil - } - + switch { + case !fromCache && formats.Validate(path): + return path, nil + case !fromCache: continue + default: + return path, nil } - - return path, nil } return "", ErrNoMediaFound diff --git a/cmd/root.go b/cmd/root.go index 07d364c..3d234a7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,7 +11,7 @@ import ( ) const ( - ReleaseVersion string = "0.84.0" + ReleaseVersion string = "0.84.1" ) var ( diff --git a/types/types.go b/types/types.go index 5f3d6f9..f6f8e2f 100644 --- a/types/types.go +++ b/types/types.go @@ -49,6 +49,15 @@ func (t *Types) Register(format Type) { t.Add(format) } +func (t *Types) Validate(path string) bool { + format, exists := t.Extensions[filepath.Ext(path)] + if !exists { + return false + } + + return format.Validate(path) +} + func (t *Types) GetExtensions() string { var output strings.Builder