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

import (
	"context"

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

type (
	EXIFScanner struct {
		repository repository.MediaRepository
	}
)

var _ BatchProcessor[*repository.Media] = &EXIFScanner{}

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

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

	return medias, nil
}

func (e *EXIFScanner) Process(ctx context.Context, m *repository.Media) error {
	exif, err := coroutine.WrapProcess(ctx, func() (*repository.MediaEXIF, error) { return fileop.ReadExif(m.Path) })
	if err != nil {
		return err
	}

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