lens @ c2d666b43477ea7042b574ad940c508216cb0e83

 1package worker
 2
 3import (
 4	"context"
 5	"fmt"
 6	"math"
 7	"os"
 8	"path"
 9
10	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
11	"git.sr.ht/~gabrielgio/img/pkg/fileop"
12)
13
14type (
15	ThumbnailScanner struct {
16		repository repository.MediaRepository
17		cachePath  string
18	}
19)
20
21var _ BatchProcessor[*repository.Media] = &EXIFScanner{}
22
23func NewThumbnailScanner(cachePath string, repository repository.MediaRepository) *ThumbnailScanner {
24	return &ThumbnailScanner{
25		repository: repository,
26		cachePath:  cachePath,
27	}
28}
29
30func (t *ThumbnailScanner) Query(ctx context.Context) ([]*repository.Media, error) {
31	return t.repository.ListEmptyThumbnail(ctx, &repository.Pagination{
32		Page: 0,
33		Size: 100,
34	})
35}
36
37func (t *ThumbnailScanner) Process(ctx context.Context, media *repository.Media) error {
38	split := media.PathHash[:2]
39	filename := media.PathHash[2:]
40	folder := path.Join(t.cachePath, split)
41	output := path.Join(folder, filename+".jpeg")
42
43	err := os.MkdirAll(folder, os.ModePerm)
44	if err != nil {
45		return err
46	}
47
48	if media.IsVideo() {
49		err := fileop.EncodeVideoThumbnail(media.Path, output, 1080, 1080)
50		if err != nil {
51			return fmt.Errorf("Error thumbnail video %d; %w", media.ID, err)
52		}
53	} else {
54		err := fileop.EncodeImageThumbnail(media.Path, output, 1080, math.MinInt32)
55		if err != nil {
56			return fmt.Errorf("Error thumbnail image %d; %w", media.ID, err)
57		}
58	}
59
60	return t.repository.CreateThumbnail(ctx, media.ID, &repository.MediaThumbnail{
61		Path: output,
62	})
63}