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