lens @ 3b06cb010948c241b3c940b7d85e4390f2d93dd0

 1package worker
 2
 3import (
 4	"context"
 5
 6	"git.sr.ht/~gabrielgio/img/pkg/components/media"
 7	"git.sr.ht/~gabrielgio/img/pkg/fileop"
 8)
 9
10type (
11	EXIFScanner struct {
12		repository media.Repository
13	}
14)
15
16var _ BatchProcessor[*media.Media] = &EXIFScanner{}
17
18func NewEXIFScanner(repository media.Repository) *EXIFScanner {
19	return &EXIFScanner{
20		repository: repository,
21	}
22}
23
24func (e *EXIFScanner) Query(ctx context.Context) ([]*media.Media, error) {
25	medias, err := e.repository.GetEmptyEXIF(ctx, &media.Pagination{
26		Page: 0,
27		Size: 100,
28	})
29	if err != nil {
30		return nil, err
31	}
32
33	return medias, nil
34}
35
36func wrapReadExif(ctx context.Context, path string) (*media.MediaEXIF, error) {
37	c := make(chan *media.MediaEXIF)
38	e := make(chan error)
39	go func() {
40		defer close(c)
41		defer close(e)
42
43		newExif, err := fileop.ReadExif(path)
44		if err != nil {
45			e <- err
46		} else {
47			c <- newExif
48		}
49	}()
50
51	select {
52	case <-ctx.Done():
53		return nil, ctx.Err()
54	case m := <-c:
55		return m, nil
56	case err := <-e:
57		return nil, err
58	}
59}
60
61func (e *EXIFScanner) Process(ctx context.Context, m *media.Media) error {
62	newExif, err := wrapReadExif(ctx, m.Path)
63	if err != nil {
64		return err
65	}
66
67	return e.repository.CreateEXIF(ctx, m.ID, newExif)
68}