package scanner import ( "context" "fmt" "math" "os" "path" "git.sr.ht/~gabrielgio/img/pkg/database/repository" "git.sr.ht/~gabrielgio/img/pkg/fileop" "git.sr.ht/~gabrielgio/img/pkg/worker" ) type ( ThumbnailScanner struct { repository repository.MediaRepository cachePath string } ) var _ worker.ListProcessor[*repository.Media] = &EXIFScanner{} func NewThumbnailScanner(cachePath string, repository repository.MediaRepository) *ThumbnailScanner { return &ThumbnailScanner{ repository: repository, cachePath: cachePath, } } func (t *ThumbnailScanner) Query(ctx context.Context) ([]*repository.Media, error) { return t.repository.ListEmptyThumbnail(ctx, &repository.Pagination{ Page: 0, Size: 100, }) } func (t *ThumbnailScanner) OnFail(ctx context.Context, media *repository.Media, _ error) { _ = t.repository.CreateThumbnail(ctx, media.ID, &repository.MediaThumbnail{ Path: "", }) } func (t *ThumbnailScanner) Process(ctx context.Context, media *repository.Media) error { split := media.PathHash[:2] filename := media.PathHash[2:] folder := path.Join(t.cachePath, split) output := path.Join(folder, filename+".jpeg") err := os.MkdirAll(folder, os.ModePerm) if err != nil { return err } if media.IsVideo() { err := fileop.EncodeVideoThumbnail(media.Path, output, 1080, 1080) if err != nil { return fmt.Errorf("Error thumbnail video %d; %w", media.ID, err) } } else { err := fileop.EncodeImageThumbnail(media.Path, output, 1080, math.MinInt32) if err != nil { return fmt.Errorf("Error thumbnail image %d; %w", media.ID, err) } } return t.repository.CreateThumbnail(ctx, media.ID, &repository.MediaThumbnail{ Path: output, }) }