Skip to content

Commit

Permalink
add scanner.scanDir
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Nov 10, 2021
1 parent 43a929c commit 870bec7
Showing 1 changed file with 41 additions and 45 deletions.
86 changes: 41 additions & 45 deletions server/scanner/scanner.go
Expand Up @@ -57,28 +57,18 @@ type ScanOptions struct {
}

func (s *Scanner) ScanAndClean(opts ScanOptions) error {
c := &collected{
seenTracks: map[int]struct{}{},
seenAlbums: map[int]struct{}{},
}
if err := s.scan(c, opts.IsFull); err != nil {
return err
}
if err := s.clean(c); err != nil {
return err
}
return nil
}

func (s *Scanner) scan(c *collected, isFull bool) error {
if s.IsScanning() {
return ErrAlreadyScanning
}
atomic.StoreInt32(s.scanning, 1)
defer atomic.StoreInt32(s.scanning, 0)

start := time.Now()
itemErrs := multierr.Err{}
itemErrs := &multierr.Err{}
c := &collected{
seenTracks: map[int]struct{}{},
seenAlbums: map[int]struct{}{},
}

log.Println("starting scan")
defer func() {
Expand All @@ -87,38 +77,16 @@ func (s *Scanner) scan(c *collected, isFull bool) error {
}()

for _, musicPath := range s.musicPaths {
err := godirwalk.Walk(musicPath, &godirwalk.Options{
Callback: func(_ string, _ *godirwalk.Dirent) error {
return nil
},
PostChildrenCallback: func(itemPath string, _ *godirwalk.Dirent) error {
log.Printf("processing folder `%s`", itemPath)
return s.callback(c, isFull, musicPath, itemPath)
},
Unsorted: !s.sorted,
FollowSymbolicLinks: true,
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
itemErrs.Add(fmt.Errorf("%q: %w", path, err))
return godirwalk.SkipNode
},
})
if err != nil {
return fmt.Errorf("walking filesystem: %w", err)
err := s.scanPath(c, opts.IsFull, musicPath)
var subItemErrs *multierr.Err
switch {
case errors.As(err, &subItemErrs):
itemErrs.Extend(subItemErrs.Errors())
case err != nil:
return fmt.Errorf("scan %q: %w", musicPath, err)
}
}

if err := s.db.SetSetting("last_scan_time", strconv.FormatInt(time.Now().Unix(), 10)); err != nil {
return fmt.Errorf("set scan time: %w", err)
}

if itemErrs.Len() > 0 {
return itemErrs
}

return nil
}

func (s *Scanner) clean(c *collected) error {
if err := s.cleanTracks(c.seenTracks); err != nil {
return fmt.Errorf("clean tracks: %w", err)
}
Expand All @@ -131,15 +99,43 @@ func (s *Scanner) clean(c *collected) error {
if err := s.cleanGenres(); err != nil {
return fmt.Errorf("clean genres: %w", err)
}

if err := s.db.SetSetting("last_scan_time", strconv.FormatInt(time.Now().Unix(), 10)); err != nil {
return fmt.Errorf("set scan time: %w", err)
}

if itemErrs.Len() > 0 {
return itemErrs
}

return nil
}

func (s *Scanner) scanPath(c *collected, isFull bool, musicPath string) error {
itemErrs := multierr.Err{}
return godirwalk.Walk(musicPath, &godirwalk.Options{
Callback: func(_ string, _ *godirwalk.Dirent) error {
return nil
},
PostChildrenCallback: func(itemPath string, _ *godirwalk.Dirent) error {
return s.callback(c, isFull, musicPath, itemPath)
},
Unsorted: !s.sorted,
FollowSymbolicLinks: true,
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
itemErrs.Add(fmt.Errorf("%q: %w", path, err))
return godirwalk.SkipNode
},
})
}

func (s *Scanner) callback(c *collected, isFull bool, rootAbsPath string, itemAbsPath string) error {
relpath, _ := filepath.Rel(rootAbsPath, itemAbsPath)
log.Printf("processing folder `%s`", relpath)
if rootAbsPath == itemAbsPath {
return nil
}

relpath, _ := filepath.Rel(rootAbsPath, itemAbsPath)
gs, err := godirwalk.NewScanner(itemAbsPath)
if err != nil {
return err
Expand Down

0 comments on commit 870bec7

Please sign in to comment.