Skip to content

Commit

Permalink
global regex so its the same every time
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Dec 2, 2022
1 parent 697fa7b commit 7a1be10
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/imageconvert/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"
)

var ImageExtensionRegex = regexp.MustCompile(".*.jpg$|.*.jpeg$|.*.png$|.*.webp$|.*.JPG$|.*.JPEG$|.*.PNG$|.*.WEBP$")

// FileInfo is a copy of fs.FileInfo that
// allows us to modify the filename
type FileInfo struct {
Expand All @@ -21,7 +23,6 @@ type FileInfo struct {
// files in the given input
func ListAllFiles(inputPath string) ([]FileInfo, error) {
var allFiles []FileInfo
var suffixRegex = regexp.MustCompile(".*.jpg$|.*.jpeg$|.*.png$|.*.webp$")

// expand ~ paths
if strings.Contains(inputPath, "~") {
Expand Down Expand Up @@ -51,7 +52,7 @@ func ListAllFiles(inputPath string) ([]FileInfo, error) {
return nil, fmt.Errorf("could not list files in dir: %s, err: %w", file, err)
}
allFiles = append(allFiles, dirFiles...)
} else if suffixRegex.MatchString(strings.ToLower(file)) {
} else if ImageExtensionRegex.MatchString(strings.ToLower(file)) {
allFiles = append(allFiles, FileInfo{Name: file, ModTime: stat.ModTime()})
}
}
Expand All @@ -68,17 +69,19 @@ func ListDirFiles(root string) ([]FileInfo, error) {
return nil, fmt.Errorf("error listing all files in dir: %s, error: %s", root, err.Error())
}

var suffixRegex = regexp.MustCompile(".*.jpg$|.*.jpeg$|.*.png$|.*.webp$")

for _, file := range files {
var fullPath = filepath.Join(root, file.Name())

if file.IsDir() {

recursiveImages, err := ListDirFiles(fullPath)
if err != nil {
return nil, fmt.Errorf("error from recursive call to ListFiles, error: %s", err.Error())
}
allFiles = append(allFiles, recursiveImages...)
} else if suffixRegex.MatchString(strings.ToLower(file.Name())) {

} else if ImageExtensionRegex.MatchString(strings.ToLower(file.Name())) {

var info, err = file.Info()
if err != nil {
return nil, fmt.Errorf("could not get FileInfo for file: %s, error: %s", file.Name(), err.Error())
Expand Down

0 comments on commit 7a1be10

Please sign in to comment.