aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/exif_scanner.go
blob: d7865e39737f316d2fe38599868cd8bb9cf5ea6f (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
package worker

import (
	"context"

	"git.sr.ht/~gabrielgio/img/pkg/components/media"
	"git.sr.ht/~gabrielgio/img/pkg/fileop"
)

type (
	EXIFScanner struct {
		repository media.Repository
	}
)

var _ ListProcessor[*media.Media] = &EXIFScanner{}

func NewEXIFScanner(repository media.Repository) *EXIFScanner {
	return &EXIFScanner{
		repository: repository,
	}
}

func (e *EXIFScanner) Query(ctx context.Context) ([]*media.Media, error) {
	medias, err := e.repository.GetEmptyEXIF(ctx, &media.Pagination{
		Page: 0,
		Size: 100,
	})
	if err != nil {
		return nil, err
	}

	return medias, nil
}

func wrapReadExif(ctx context.Context, path string) (*media.MediaEXIF, error) {
	c := make(chan *media.MediaEXIF)
	e := make(chan error)
	go func() {
		defer close(c)
		defer close(e)

		newExif, err := fileop.ReadExif(path)
		if err != nil {
			e <- err
		} else {
			c <- newExif
		}
	}()

	select {
	case <-ctx.Done():
		return nil, ctx.Err()
	case m := <-c:
		return m, nil
	case err := <-e:
		return nil, err
	}
}

func (e *EXIFScanner) Process(ctx context.Context, m *media.Media) error {
	newExif, err := wrapReadExif(ctx, m.Path)
	if err != nil {
		return err
	}

	return e.repository.CreateEXIF(ctx, m.ID, newExif)
}