aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/scanner/thumbnail_scanner.go
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-07-04 19:01:17 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-07-04 19:01:17 +0200
commit05a8dbf46792adfef007a0ffbcb654026db036fa (patch)
tree386658904377c695747c3ffe4a288915b0a89917 /pkg/worker/scanner/thumbnail_scanner.go
parent311ab744fe1bf278b18c25892497271988399e9a (diff)
downloadlens-05a8dbf46792adfef007a0ffbcb654026db036fa.tar.gz
lens-05a8dbf46792adfef007a0ffbcb654026db036fa.tar.bz2
lens-05a8dbf46792adfef007a0ffbcb654026db036fa.zip
feat: Add use based file scanner
Diffstat (limited to 'pkg/worker/scanner/thumbnail_scanner.go')
-rw-r--r--pkg/worker/scanner/thumbnail_scanner.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/pkg/worker/scanner/thumbnail_scanner.go b/pkg/worker/scanner/thumbnail_scanner.go
new file mode 100644
index 0000000..02fd4dd
--- /dev/null
+++ b/pkg/worker/scanner/thumbnail_scanner.go
@@ -0,0 +1,64 @@
+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.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 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,
+ })
+}