1package scanner
2
3import (
4 "context"
5
6 "git.sr.ht/~gabrielgio/img/pkg/coroutine"
7 "git.sr.ht/~gabrielgio/img/pkg/database/repository"
8 "git.sr.ht/~gabrielgio/img/pkg/fileop"
9 "git.sr.ht/~gabrielgio/img/pkg/worker"
10)
11
12type (
13 EXIFScanner struct {
14 repository repository.MediaRepository
15 }
16)
17
18var _ worker.ListProcessor[*repository.Media] = &EXIFScanner{}
19
20func NewEXIFScanner(repository repository.MediaRepository) *EXIFScanner {
21 return &EXIFScanner{
22 repository: repository,
23 }
24}
25
26func (e *EXIFScanner) Query(ctx context.Context) ([]*repository.Media, error) {
27 return e.repository.ListEmptyEXIF(ctx, &repository.Pagination{
28 Page: 0,
29 Size: 100,
30 })
31}
32
33func (t *EXIFScanner) OnFail(ctx context.Context, media *repository.Media, _ error) {
34 _ = t.repository.CreateEXIF(ctx, media.ID, &repository.MediaEXIF{})
35}
36
37func (e *EXIFScanner) Process(ctx context.Context, m *repository.Media) error {
38 exif, err := coroutine.WrapProcess(ctx, func() (*repository.MediaEXIF, error) { return fileop.ReadExif(m.Path) })
39 if err != nil {
40 return err
41 }
42
43 return e.repository.CreateEXIF(ctx, m.ID, exif)
44}