From 6e84441dab0a2b89869e33d7e89d14189d9b67c0 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 1 Jul 2023 17:55:50 +0200 Subject: feat: Add thumbnailer --- pkg/worker/exif_scanner.go | 7 +---- pkg/worker/file_scanner.go | 25 ++++++++--------- pkg/worker/thumbnail_scanner.go | 62 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 pkg/worker/thumbnail_scanner.go (limited to 'pkg/worker') diff --git a/pkg/worker/exif_scanner.go b/pkg/worker/exif_scanner.go index 97790a0..5ea1810 100644 --- a/pkg/worker/exif_scanner.go +++ b/pkg/worker/exif_scanner.go @@ -23,15 +23,10 @@ func NewEXIFScanner(repository repository.MediaRepository) *EXIFScanner { } func (e *EXIFScanner) Query(ctx context.Context) ([]*repository.Media, error) { - medias, err := e.repository.GetEmptyEXIF(ctx, &repository.Pagination{ + return e.repository.ListEmptyEXIF(ctx, &repository.Pagination{ Page: 0, Size: 100, }) - if err != nil { - return nil, err - } - - return medias, nil } func (e *EXIFScanner) Process(ctx context.Context, m *repository.Media) error { diff --git a/pkg/worker/file_scanner.go b/pkg/worker/file_scanner.go index aa79035..b4f907a 100644 --- a/pkg/worker/file_scanner.go +++ b/pkg/worker/file_scanner.go @@ -2,14 +2,12 @@ package worker import ( "context" - "crypto/md5" - "encoding/hex" "io/fs" "mime" "path/filepath" - "strings" "git.sr.ht/~gabrielgio/img/pkg/database/repository" + "git.sr.ht/~gabrielgio/img/pkg/fileop" ) type ( @@ -59,18 +57,17 @@ func (f *FileScanner) Query(ctx context.Context) (<-chan string, error) { } func (f *FileScanner) Process(ctx context.Context, path string) error { - m := mime.TypeByExtension(filepath.Ext(path)) - if !strings.HasPrefix(m, "video") && !strings.HasPrefix(m, "image") { + mimetype := mime.TypeByExtension(filepath.Ext(path)) + supported := fileop.IsMimeTypeSupported(mimetype) + if !supported { return nil } - hash := md5.Sum([]byte(path)) - str := hex.EncodeToString(hash[:]) - name := filepath.Base(path) + hash := fileop.GetHashFromPath(path) - exists, errResp := f.repository.Exists(ctx, str) - if errResp != nil { - return errResp + exists, err := f.repository.Exists(ctx, hash) + if err != nil { + return err } if exists { @@ -78,9 +75,9 @@ func (f *FileScanner) Process(ctx context.Context, path string) error { } return f.repository.Create(ctx, &repository.CreateMedia{ - Name: name, + Name: filepath.Base(path), Path: path, - PathHash: str, - MIMEType: m, + PathHash: hash, + MIMEType: mimetype, }) } 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, + }) +} -- cgit v1.2.3