aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/file_scanner.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/worker/file_scanner.go')
-rw-r--r--pkg/worker/file_scanner.go25
1 files changed, 11 insertions, 14 deletions
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,
})
}