diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-01 17:55:50 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-01 17:55:50 +0200 |
commit | 6e84441dab0a2b89869e33d7e89d14189d9b67c0 (patch) | |
tree | e015839d495bcfc7619f4efd08f97a1ba603fd82 /pkg/worker/thumbnail_scanner.go | |
parent | 3f0dc691e2248cc21edd2e74a62b8f28ce95559e (diff) | |
download | lens-6e84441dab0a2b89869e33d7e89d14189d9b67c0.tar.gz lens-6e84441dab0a2b89869e33d7e89d14189d9b67c0.tar.bz2 lens-6e84441dab0a2b89869e33d7e89d14189d9b67c0.zip |
feat: Add thumbnailer
Diffstat (limited to 'pkg/worker/thumbnail_scanner.go')
-rw-r--r-- | pkg/worker/thumbnail_scanner.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/pkg/worker/thumbnail_scanner.go b/pkg/worker/thumbnail_scanner.go new file mode 100644 index 0000000..cc201b8 --- /dev/null +++ b/pkg/worker/thumbnail_scanner.go @@ -0,0 +1,62 @@ +package worker + +import ( + "context" + "math" + "os" + "path" + + "git.sr.ht/~gabrielgio/img/pkg/database/repository" + "git.sr.ht/~gabrielgio/img/pkg/fileop" +) + +type ( + ThumbnailScanner struct { + repository repository.MediaRepository + cachePath string + } +) + +var _ BatchProcessor[*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) 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 err + } + } else { + err := fileop.EncodeImageThumbnail(media.Path, output, 1080, math.MaxInt) + if err != nil { + return err + } + } + + return t.repository.CreateThumbnail(ctx, media.ID, &repository.MediaThumbnail{ + Path: output, + }) +} |