From 1675136966473083e970badfe92433a05d2d7fee Mon Sep 17 00:00:00 2001 From: jakka Date: Sat, 12 Apr 2025 14:44:39 +0300 Subject: implemented database and concurrent file scanning --- files/index.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'files/index.go') diff --git a/files/index.go b/files/index.go index e02e4ba..ea05dc4 100644 --- a/files/index.go +++ b/files/index.go @@ -1,7 +1,56 @@ -package main +package files -import "context" +import ( + "context" + "io/fs" + "path/filepath" + "time" + + "github.com/briandowns/spinner" + "golang.org/x/sync/errgroup" +) func IndexFlacs(ctx context.Context) error { + spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond) + spin.Suffix = " Indexing flacs..." + + spin.Start() + + wg := new(errgroup.Group) + wg.SetLimit(100) + + if err := filepath.WalkDir(ctx.Value("path").(string), func(path string, info fs.DirEntry, err error) error { + select { + case <-ctx.Done(): + spin.FinalMSG = "Stopping...\n" + spin.Stop() + return nil + default: + if err != nil { + return err + } + if !info.IsDir() { + if filepath.Ext(path) == ".flac" { + wg.Go(func() error { + select { + case <-ctx.Done(): + return nil + default: + return ProcessFile(ctx, path, info) + } + }) + } + + } + return nil + } + + }); err != nil { + return err + } + + wg.Wait() + spin.FinalMSG = "Done indexing flacs" + spin.Stop() return nil } -- cgit v1.3.1