aboutsummaryrefslogtreecommitdiff
path: root/pkg/worker/file_scanner.go
blob: aa79035b77c5fb9724a149bbb0e29450583f0baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package worker

import (
	"context"
	"crypto/md5"
	"encoding/hex"
	"io/fs"
	"mime"
	"path/filepath"
	"strings"

	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
)

type (
	FileScanner struct {
		root       string
		repository repository.MediaRepository
	}
)

var _ ChanProcessor[string] = &FileScanner{}

func NewFileScanner(root string, repository repository.MediaRepository) *FileScanner {
	return &FileScanner{
		root:       root,
		repository: repository,
	}
}

func (f *FileScanner) Query(ctx context.Context) (<-chan string, error) {
	c := make(chan string)
	go func() {
		defer close(c)
		_ = filepath.Walk(f.root, func(path string, info fs.FileInfo, err error) error {
			select {
			case <-ctx.Done():
				return filepath.SkipAll
			default:
			}

			if info == nil {
				return nil
			}

			if info.IsDir() && filepath.Base(info.Name())[0] == '.' {
				return filepath.SkipDir
			}

			if info.IsDir() {
				return nil
			}

			c <- path
			return nil
		})
	}()
	return c, nil
}

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") {
		return nil
	}

	hash := md5.Sum([]byte(path))
	str := hex.EncodeToString(hash[:])
	name := filepath.Base(path)

	exists, errResp := f.repository.Exists(ctx, str)
	if errResp != nil {
		return errResp
	}

	if exists {
		return nil
	}

	return f.repository.Create(ctx, &repository.CreateMedia{
		Name:     name,
		Path:     path,
		PathHash: str,
		MIMEType: m,
	})
}