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

import (
	"context"

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

type (
	EXIFScanner struct {
		repository repository.MediaRepository
	}
)

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

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

func (e *EXIFScanner) Query(ctx context.Context) ([]*repository.Media, error) {
	return e.repository.ListEmptyEXIF(ctx, &repository.Pagination{
		Page: 0,
		Size: 100,
	})
}

func (t *EXIFScanner) OnFail(ctx context.Context, media *repository.Media, _ error) {
	_ = t.repository.CreateEXIF(ctx, media.ID, &repository.MediaEXIF{})
}

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