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
|
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,
})
}
|