-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.go
118 lines (98 loc) · 2.82 KB
/
traverse.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package mediasort
import (
"context"
"io/fs"
"path/filepath"
"regexp"
"strings"
"github.com/dtrejod/goexif/internal/ilog"
"github.com/dtrejod/goexif/internal/mediatype"
"go.uber.org/zap"
)
type traverser struct {
sourceDirectory string
stopWalkOnError bool
allowedFileTypes []string
blocklist []*regexp.Regexp
useInputMagicSignature bool
fileHandler *metadataFileHandler
progressTracker *progressTracker
extVisitorFunc mediatype.VisitorFunc[map[string]struct{}]
}
// Run implements Sorter
func (t *traverser) Run(ctx context.Context) error {
ilog.FromContext(ctx).Info("Performing pre-scan for media files...", zap.String("directory", t.sourceDirectory))
if err := filepath.WalkDir(t.sourceDirectory, t.traverseFunc(ctx, true)); err != nil {
return err
}
ilog.FromContext(ctx).Info("Sorting media files in directory...", zap.String("directory", t.sourceDirectory))
if err := filepath.WalkDir(t.sourceDirectory, t.traverseFunc(ctx, false)); err != nil {
return err
}
ilog.FromContext(ctx).Info("Succesfully sorted media files.")
return nil
}
func (t *traverser) traverseFunc(ctx context.Context, isPreRun bool) fs.WalkDirFunc {
return func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
logger := ilog.FromContext(ctx).With(zap.String("path", path))
if info.IsDir() {
if t.skipDir(path) {
logger.Debug("Directory matches blocklist, so skipping entire directory...")
return fs.SkipDir
}
return nil
}
if t.skipDir(path) {
logger.Debug("Path in blocklist, so skipping...")
return fs.SkipDir
}
srcMedia, err := mediatype.ID(path, t.useInputMagicSignature)
if err != nil {
logger.Debug("Could not identify file as media file.", zap.Error(err))
return nil
}
visistor := mediatype.FormatWithVisitor[map[string]struct{}](srcMedia)
aliases, err := visistor.Accept(ctx, t.extVisitorFunc)
if err != nil {
logger.Warn("Could not identify file from known media aliases.", zap.Error(err))
return nil
}
logger.Debug("Checking file.")
if t.skipFile(aliases) {
logger.Debug("File did not matchfile types allowlist, so skipping...")
return nil
}
if isPreRun {
t.progressTracker.handle(ctx, true)
return nil
}
t.progressTracker.handle(ctx, false)
if err := t.fileHandler.handle(ctx, srcMedia); err != nil {
logger.Warn("Failed to handle file.", zap.Error(err))
if t.stopWalkOnError {
return err
}
}
return nil
}
}
func (t *traverser) skipDir(path string) bool {
for _, d := range t.blocklist {
if d.MatchString(strings.ToLower(path)) {
return true
}
}
return false
}
func (t *traverser) skipFile(srcMediaAlises map[string]struct{}) bool {
for _, allowedType := range t.allowedFileTypes {
_, ok := srcMediaAlises[allowedType]
if ok {
return false
}
}
return true
}