aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/scanner/file_scanner.go
blob: 7c19a3dfe9573278aa703c3402d1620f62b53c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package scanner

import (
	"context"
	"io/fs"
	"mime"
	"path/filepath"

	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
	"git.sr.ht/~gabrielgio/img/pkg/fileop"
	"git.sr.ht/~gabrielgio/img/pkg/list"
	"git.sr.ht/~gabrielgio/img/pkg/worker"
)

type (
	FileScanner struct {
		mediaRepository repository.MediaRepository
		userRepository  repository.UserRepository
	}
)

var _ worker.ChanProcessor[string] = &FileScanner{}

func NewFileScanner(
	mediaRepository repository.MediaRepository,
	userRepository repository.UserRepository,
) *FileScanner {
	return &FileScanner{
		mediaRepository: mediaRepository,
		userRepository:  userRepository,
	}
}

func (f *FileScanner) Query(ctx context.Context) (<-chan string, error) {
	c := make(chan string)

	users, err := f.userRepository.List(ctx)
	if err != nil {
		return nil, err
	}

	// TODO: de duplicate file paths
	paths := list.Map(users, func(u *repository.User) string { return u.Path })

	go func(paths []string) {
		defer close(c)
		for _, p := range paths {
			_ = filepath.Walk(p, 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
			})
		}
	}(paths)
	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.mediaRepository.Exists(ctx, hash)
	if err != nil {
		return err
	}

	if exists {
		return nil
	}

	return f.mediaRepository.Create(ctx, &repository.CreateMedia{
		Name:     filepath.Base(path),
		Path:     path,
		PathHash: hash,
		MIMEType: mimetype,
	})
}