From 3dc8c188495cd045594338a2ba3e1e752223bb17 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 11 Feb 2025 17:00:18 +0100 Subject: [PATCH] fix(fs): get file by ext sorted by mod time --- pkg/gotenberg/fs.go | 29 ++++++++++++++++++++++++++--- pkg/gotenberg/fs_test.go | 20 +++++++++++++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/pkg/gotenberg/fs.go b/pkg/gotenberg/fs.go index 99b403eb..d1e14a7d 100644 --- a/pkg/gotenberg/fs.go +++ b/pkg/gotenberg/fs.go @@ -4,7 +4,9 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" + "time" "github.com/google/uuid" ) @@ -86,10 +88,15 @@ func (fs *FileSystem) MkdirAll() (string, error) { return path, nil } +type fileWithModTime struct { + path string + modTime time.Time +} + // WalkDir walks through the root level of a directory and returns a list of // files paths that match the specified file extension. func WalkDir(dir, ext string) ([]string, error) { - var files []string + var files []fileWithModTime err := filepath.Walk(dir, func(path string, info os.FileInfo, pathErr error) error { if pathErr != nil { return pathErr @@ -98,11 +105,27 @@ func WalkDir(dir, ext string) ([]string, error) { return nil } if strings.EqualFold(filepath.Ext(info.Name()), ext) { - files = append(files, path) + files = append(files, fileWithModTime{ + path: path, + modTime: info.ModTime(), + }) } return nil }) - return files, err + if err != nil { + return nil, err + } + + sort.Slice(files, func(i, j int) bool { + return files[i].modTime.Before(files[j].modTime) + }) + + sortedPaths := make([]string, len(files)) + for i, f := range files { + sortedPaths[i] = f.path + } + + return sortedPaths, nil } // Interface guards. diff --git a/pkg/gotenberg/fs_test.go b/pkg/gotenberg/fs_test.go index d7f64120..f6c15a35 100644 --- a/pkg/gotenberg/fs_test.go +++ b/pkg/gotenberg/fs_test.go @@ -161,7 +161,7 @@ func TestWalkDir(t *testing.T) { expectError: true, }, { - scenario: "find PDF files", + scenario: "find PDF files, sorted by mod time", dir: func() string { path := fmt.Sprintf("%s/a_directory", os.TempDir()) @@ -170,17 +170,27 @@ func TestWalkDir(t *testing.T) { t.Fatalf(fmt.Sprintf("expected no error but got: %v", err)) } - err = os.WriteFile(fmt.Sprintf("%s/a_foo_file.pdf", path), []byte{1}, 0o755) + err = os.WriteFile(fmt.Sprintf("%s/2.pdf", path), []byte{1}, 0o755) if err != nil { t.Fatalf("expected no error but got: %v", err) } - err = os.WriteFile(fmt.Sprintf("%s/a_bar_file.PDF", path), []byte{1}, 0o755) + err = os.WriteFile(fmt.Sprintf("%s/1.PDF", path), []byte{1}, 0o755) if err != nil { t.Fatalf("expected no error but got: %v", err) } - err = os.WriteFile(fmt.Sprintf("%s/a_baz_file.txt", path), []byte{1}, 0o755) + err = os.WriteFile(fmt.Sprintf("%s/3.txt", path), []byte{1}, 0o755) + if err != nil { + t.Fatalf("expected no error but got: %v", err) + } + + err = os.WriteFile(fmt.Sprintf("%s/10.pdf", path), []byte{1}, 0o755) + if err != nil { + t.Fatalf("expected no error but got: %v", err) + } + + err = os.WriteFile(fmt.Sprintf("%s/11.pdf", path), []byte{1}, 0o755) if err != nil { t.Fatalf("expected no error but got: %v", err) } @@ -189,7 +199,7 @@ func TestWalkDir(t *testing.T) { }(), ext: ".pdf", expectError: false, - expectFiles: []string{"/tmp/a_directory/a_bar_file.PDF", "/tmp/a_directory/a_foo_file.pdf"}, + expectFiles: []string{"/tmp/a_directory/2.pdf", "/tmp/a_directory/1.PDF", "/tmp/a_directory/10.pdf", "/tmp/a_directory/11.pdf"}, }, } { t.Run(tc.scenario, func(t *testing.T) {