From 05a8dbf46792adfef007a0ffbcb654026db036fa Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Tue, 4 Jul 2023 19:01:17 +0200 Subject: feat: Add use based file scanner --- pkg/worker/file_scanner.go | 83 ---------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 pkg/worker/file_scanner.go (limited to 'pkg/worker/file_scanner.go') diff --git a/pkg/worker/file_scanner.go b/pkg/worker/file_scanner.go deleted file mode 100644 index b4f907a..0000000 --- a/pkg/worker/file_scanner.go +++ /dev/null @@ -1,83 +0,0 @@ -package worker - -import ( - "context" - "io/fs" - "mime" - "path/filepath" - - "git.sr.ht/~gabrielgio/img/pkg/database/repository" - "git.sr.ht/~gabrielgio/img/pkg/fileop" -) - -type ( - FileScanner struct { - root string - repository repository.MediaRepository - } -) - -var _ ChanProcessor[string] = &FileScanner{} - -func NewFileScanner(root string, repository repository.MediaRepository) *FileScanner { - return &FileScanner{ - root: root, - repository: repository, - } -} - -func (f *FileScanner) Query(ctx context.Context) (<-chan string, error) { - c := make(chan string) - go func() { - defer close(c) - _ = filepath.Walk(f.root, func(path string, info fs.FileInfo, err error) error { - select { - case <-ctx.Done(): - return filepath.SkipAll - default: - } - - if info == nil { - return nil - } - - if info.IsDir() && filepath.Base(info.Name())[0] == '.' { - return filepath.SkipDir - } - - if info.IsDir() { - return nil - } - - c <- path - return nil - }) - }() - return c, nil -} - -func (f *FileScanner) Process(ctx context.Context, path string) error { - mimetype := mime.TypeByExtension(filepath.Ext(path)) - supported := fileop.IsMimeTypeSupported(mimetype) - if !supported { - return nil - } - - hash := fileop.GetHashFromPath(path) - - exists, err := f.repository.Exists(ctx, hash) - if err != nil { - return err - } - - if exists { - return nil - } - - return f.repository.Create(ctx, &repository.CreateMedia{ - Name: filepath.Base(path), - Path: path, - PathHash: hash, - MIMEType: mimetype, - }) -} -- cgit v1.2.3