lens @ 6e84441dab0a2b89869e33d7e89d14189d9b67c0

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