-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
179 lines (147 loc) · 3.94 KB
/
process.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package internal
import (
"fmt"
"io/fs"
"path/filepath"
"github.com/bogem/id3v2/v2"
mapset "github.com/deckarep/golang-set/v2"
"github.com/jarxorg/wfs"
"github.com/rs/zerolog"
)
type MetadataOverride struct {
Artist string
Album string
AlbumArtist string
}
func (m *MetadataOverride) active() bool {
return m.Artist != "" || m.Album != "" || m.AlbumArtist != ""
}
type ProcessorConfig struct {
Logger zerolog.Logger
InputFS fs.FS
OutputFS wfs.WriteFileFS
OutputRoot string
Extensions mapset.Set[string]
Overrides MetadataOverride
}
func NewProcessor(conf ProcessorConfig) (*Processor, error) {
if conf.Overrides.active() && conf.OutputRoot == "" {
return nil, fmt.Errorf("must supply output root if overrides in use")
}
return &Processor{
log: conf.Logger,
inputFS: conf.InputFS,
outputFS: conf.OutputFS,
outputRoot: conf.OutputRoot,
extensions: conf.Extensions,
overrides: conf.Overrides,
}, nil
}
type Processor struct {
log zerolog.Logger
inputFS fs.FS
outputFS wfs.WriteFileFS
outputRoot string
extensions mapset.Set[string]
overrides MetadataOverride
}
func (p *Processor) Process() error {
inputFiles, err := p.getFileList(p.inputFS)
if err != nil {
return err
}
mappings, err := p.getMapping(p.inputFS, inputFiles)
if err != nil {
return err
}
if err := p.executeCopy(p.inputFS, p.outputFS, mappings); err != nil {
return err
}
if p.overrides.active() {
if err := p.overwriteTags(mappings); err != nil {
return err
}
}
return nil
}
func (p *Processor) getFileList(fsys fs.FS) ([]string, error) {
foundFiles := []string{}
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !p.extensions.Contains(filepath.Ext(path)) {
p.log.Debug().Str("path", path).Msg("skipping due to extension filter")
return nil
}
foundFiles = append(foundFiles, path)
return nil
})
if err != nil {
return nil, err
}
return foundFiles, nil
}
func (p *Processor) getMapping(fsys fs.FS, files []string) (map[string]string, error) {
mapping := map[string]string{}
for _, fl := range files {
destPath, err := computeDestinationPath(fsys, p.overrides, fl)
if err != nil {
return nil, err
}
mapping[fl] = destPath
}
return mapping, nil
}
func (p *Processor) executeCopy(input fs.FS, output wfs.WriteFileFS, mapping map[string]string) error {
for inputPath, outputPath := range mapping {
containing := filepath.Dir(outputPath)
// TODO: make perms configurable
if err := output.MkdirAll(containing, 0755); err != nil {
p.log.Err(err).Str("path", outputPath).Msg("error making containing directory")
return err
}
content, err := fs.ReadFile(input, inputPath)
if err != nil {
p.log.Err(err).Str("path", inputPath).Msg("error reading source file")
return err
}
// TODO: make perms configurable
if _, err := wfs.WriteFile(output, outputPath, content, 0644); err != nil {
p.log.Err(err).Str("path", outputPath).Msg("error writing output")
return err
}
}
return nil
}
func (p *Processor) overwriteTags(mapping map[string]string) error {
for _, outputFragment := range mapping {
outputPath := filepath.Join(p.outputRoot, outputFragment)
if err := p.overwriteSingleTag(outputPath); err != nil {
return err
}
}
return nil
}
func (p *Processor) overwriteSingleTag(path string) error {
tag, err := id3v2.Open(path, id3v2.Options{Parse: true})
if err != nil {
p.log.Err(err).Msg("error opening destination path for tag writing")
return err
}
defer tag.Close()
if p.overrides.Artist != "" {
tag.SetArtist(p.overrides.Artist)
}
if p.overrides.Album != "" {
tag.SetAlbum(p.overrides.Album)
}
if p.overrides.AlbumArtist != "" {
tag.AddTextFrame(tag.CommonID("Band/Orchestra/Accompaniment"), tag.DefaultEncoding(), p.overrides.AlbumArtist)
}
if err := tag.Save(); err != nil {
p.log.Err(err).Msg("error saving tag update")
return err
}
return nil
}