Allow naming without an artist folder and per-folder naming rules
The artist folder can be left empty for Album/01 Title (no artist tag needed), and folders such as Video Games can have their own formats. Files in a rule folder are only renamed inside it; other files are never moved into it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d8ac25a55d
commit
b11d0806c1
7 files changed
+462
-100
No files matched your search
+194
-55
@@ -18,6 +18,7 @@ import (
|
||||
// track format (with a separate format for multi-disc albums), built from
|
||||
// tokens such as {Artist Name}, {Album Title} and {track:00}.
|
||||
|
||||
// An empty ArtistFolder means no artist folder: Album/01 Title.
|
||||
type NamingConfig struct {
|
||||
ArtistFolder string `json:"artist_folder"`
|
||||
AlbumFolder string `json:"album_folder"`
|
||||
@@ -25,6 +26,45 @@ type NamingConfig struct {
|
||||
MultiDiscFormat string `json:"multi_disc_format"`
|
||||
Colon string `json:"colon"` // smart, delete, dash, space_dash, space_dash_space
|
||||
Illegal string `json:"illegal"` // replace or remove
|
||||
// Folders give part of a library its own formats, such as soundtracks in
|
||||
// "Video Games" named Album/01 Title. Files inside such a folder stay
|
||||
// inside it, and other files are never moved into it.
|
||||
Folders []FolderNaming `json:"folders"`
|
||||
}
|
||||
|
||||
// FolderNaming is the naming for one folder of a library (relative to its root).
|
||||
type FolderNaming struct {
|
||||
Folder string `json:"folder"`
|
||||
ArtistFolder string `json:"artist_folder"`
|
||||
AlbumFolder string `json:"album_folder"`
|
||||
TrackFormat string `json:"track_format"`
|
||||
MultiDiscFormat string `json:"multi_disc_format"`
|
||||
}
|
||||
|
||||
// formats are the four path formats in effect for one file.
|
||||
type formats struct{ artist, album, track, multiDisc string }
|
||||
|
||||
func (c NamingConfig) formats() formats {
|
||||
return formats{c.ArtistFolder, c.AlbumFolder, c.TrackFormat, c.MultiDiscFormat}
|
||||
}
|
||||
|
||||
func (f FolderNaming) formats() formats {
|
||||
return formats{f.ArtistFolder, f.AlbumFolder, f.TrackFormat, f.MultiDiscFormat}
|
||||
}
|
||||
|
||||
// folderOf returns the folder rule a library path falls under, if any.
|
||||
func (c NamingConfig) folderOf(rel string) (FolderNaming, bool) {
|
||||
for _, f := range c.Folders {
|
||||
if inFolder(rel, f.Folder) {
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
return FolderNaming{}, false
|
||||
}
|
||||
|
||||
// inFolder reports whether rel is inside folder (compared case-insensitively).
|
||||
func inFolder(rel, folder string) bool {
|
||||
return strings.HasPrefix(strings.ToLower(rel)+"/", strings.ToLower(folder)+"/") && len(rel) > len(folder)
|
||||
}
|
||||
|
||||
func DefaultNaming() NamingConfig {
|
||||
@@ -35,9 +75,14 @@ func DefaultNaming() NamingConfig {
|
||||
MultiDiscFormat: "{Medium Format} {medium:00}/{track:00} {Track Title}",
|
||||
Colon: "smart",
|
||||
Illegal: "replace",
|
||||
Folders: []FolderNaming{},
|
||||
}
|
||||
}
|
||||
|
||||
// SoundtrackFolder is the suggested folder rule for game and film soundtracks.
|
||||
var SoundtrackFolder = FolderNaming{Folder: "Video Games", AlbumFolder: "{Album Title}",
|
||||
TrackFormat: "{track:00} {Track Title}", MultiDiscFormat: "{Medium Format} {medium:00}/{track:00} {Track Title}"}
|
||||
|
||||
// NamingPresets are offered in the UI.
|
||||
var NamingPresets = []struct {
|
||||
Name string `json:"name"`
|
||||
@@ -51,6 +96,8 @@ var NamingPresets = []struct {
|
||||
MultiDiscFormat: "{Artist Name} - {Album Title} - {medium:00}-{track:00} - {Track Title}", Colon: "smart", Illegal: "replace"}},
|
||||
{"Artist/Album/1-01 Title (disc in name)", NamingConfig{ArtistFolder: "{Artist Name}", AlbumFolder: "{Album Title}",
|
||||
TrackFormat: "{track:00} {Track Title}", MultiDiscFormat: "{medium:0}-{track:00} {Track Title}", Colon: "smart", Illegal: "replace"}},
|
||||
{"Album/01 Title (no artist folder)", NamingConfig{AlbumFolder: "{Album Title}",
|
||||
TrackFormat: "{track:00} {Track Title}", MultiDiscFormat: "{Medium Format} {medium:00}/{track:00} {Track Title}", Colon: "smart", Illegal: "replace"}},
|
||||
}
|
||||
|
||||
// NamingOf returns a pair's naming, converting the settings of earlier
|
||||
@@ -58,9 +105,7 @@ var NamingPresets = []struct {
|
||||
func NamingOf(p store.Pair) NamingConfig {
|
||||
c := DefaultNaming()
|
||||
if len(p.OrganizeConfig) > 0 && string(p.OrganizeConfig) != "{}" {
|
||||
var stored NamingConfig
|
||||
if json.Unmarshal(p.OrganizeConfig, &stored) == nil {
|
||||
fill(&stored, c)
|
||||
if stored, err := parseNaming(p.OrganizeConfig); err == nil {
|
||||
return stored
|
||||
}
|
||||
}
|
||||
@@ -91,9 +136,29 @@ func NamingOf(p store.Pair) NamingConfig {
|
||||
return c
|
||||
}
|
||||
|
||||
// parseNaming reads stored settings. Fields missing from the JSON take the
|
||||
// defaults; an artist folder that is present but empty stays empty.
|
||||
func parseNaming(raw []byte) (NamingConfig, error) {
|
||||
c := DefaultNaming()
|
||||
if err := json.Unmarshal(raw, &c); err != nil {
|
||||
return c, err
|
||||
}
|
||||
fill(&c, DefaultNaming())
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func fill(c *NamingConfig, d NamingConfig) {
|
||||
if strings.TrimSpace(c.ArtistFolder) == "" {
|
||||
c.ArtistFolder = d.ArtistFolder
|
||||
c.ArtistFolder = strings.TrimSpace(c.ArtistFolder)
|
||||
if c.Folders == nil {
|
||||
c.Folders = []FolderNaming{}
|
||||
}
|
||||
for i := range c.Folders {
|
||||
f := &c.Folders[i]
|
||||
f.Folder = strings.Trim(strings.TrimSpace(f.Folder), "/")
|
||||
f.ArtistFolder = strings.TrimSpace(f.ArtistFolder)
|
||||
if strings.TrimSpace(f.MultiDiscFormat) == "" {
|
||||
f.MultiDiscFormat = "{Medium Format} {medium:00}/" + f.TrackFormat
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(c.AlbumFolder) == "" {
|
||||
c.AlbumFolder = d.AlbumFolder
|
||||
@@ -171,42 +236,32 @@ func styleOf(t string) (sep string, cased string) {
|
||||
|
||||
// ValidateNaming checks the formats.
|
||||
func ValidateNaming(c NamingConfig) error {
|
||||
check := func(label, f string, folder bool) error {
|
||||
if strings.TrimSpace(f) == "" {
|
||||
return fmt.Errorf("%s is empty", label)
|
||||
}
|
||||
if strings.Contains(f, "..") || strings.Contains(f, "\\") || strings.HasPrefix(f, "/") || strings.HasSuffix(f, "/") {
|
||||
return fmt.Errorf("%s must be a relative name without .. or backslashes", label)
|
||||
}
|
||||
if folder && strings.Contains(f, "/") {
|
||||
return fmt.Errorf("%s must be a single folder name (no /)", label)
|
||||
}
|
||||
for _, m := range tokenPat.FindAllStringSubmatch(f, -1) {
|
||||
if numberTok.MatchString(m[1]) {
|
||||
continue
|
||||
}
|
||||
if !knownTokens[tokenKey(m[1])] {
|
||||
return fmt.Errorf("%s: unknown token {%s}", label, m[1])
|
||||
}
|
||||
}
|
||||
if strings.Count(f, "{") != strings.Count(f, "}") {
|
||||
return fmt.Errorf("%s has an unclosed {", label)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := check("Artist folder format", c.ArtistFolder, true); err != nil {
|
||||
if err := checkFormats("", c.formats()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := check("Album folder format", c.AlbumFolder, true); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range []struct{ label, v string }{{"Track format", c.TrackFormat}, {"Multi-disc track format", c.MultiDiscFormat}} {
|
||||
if err := check(f.label, f.v, false); err != nil {
|
||||
seen := map[string]bool{}
|
||||
for _, f := range c.Folders {
|
||||
label := fmt.Sprintf("Folder %q", f.Folder)
|
||||
switch {
|
||||
case f.Folder == "":
|
||||
return errors.New("a folder rule has no folder")
|
||||
case strings.Contains(f.Folder, "\\") || strings.HasPrefix(f.Folder, "/") || filepath.Clean(f.Folder) != f.Folder ||
|
||||
f.Folder == "." || strings.HasPrefix(f.Folder, ".."):
|
||||
return fmt.Errorf("%s must be a folder inside the library, like Video Games", label)
|
||||
case strings.HasPrefix(filepath.Base(f.Folder), "."):
|
||||
return fmt.Errorf("%s: hidden folders can't have naming rules", label)
|
||||
case seen[strings.ToLower(f.Folder)]:
|
||||
return fmt.Errorf("%s is listed twice", label)
|
||||
}
|
||||
seen[strings.ToLower(f.Folder)] = true
|
||||
for _, o := range c.Folders {
|
||||
if inFolder(f.Folder, o.Folder) {
|
||||
return fmt.Errorf("%s is inside %q; folder rules can't be nested", label, o.Folder)
|
||||
}
|
||||
}
|
||||
if err := checkFormats(label+": ", f.formats()); err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.Contains(tokenKeys(f.v), "|tracktitle|") && !strings.Contains(tokenKeys(f.v), "|trackcleantitle|") {
|
||||
return fmt.Errorf("%s must contain {Track Title}", f.label)
|
||||
}
|
||||
}
|
||||
switch c.Colon {
|
||||
case "smart", "delete", "dash", "space_dash", "space_dash_space":
|
||||
@@ -219,6 +274,51 @@ func ValidateNaming(c NamingConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkFormats validates one set of formats. The artist folder may be empty.
|
||||
func checkFormats(prefix string, f formats) error {
|
||||
if f.artist != "" {
|
||||
if err := checkFormat(prefix+"Artist folder format", f.artist, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := checkFormat(prefix+"Album folder format", f.album, true); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range []struct{ label, v string }{{"Track format", f.track}, {"Multi-disc track format", f.multiDisc}} {
|
||||
if err := checkFormat(prefix+t.label, t.v, false); err != nil {
|
||||
return err
|
||||
}
|
||||
if !strings.Contains(tokenKeys(t.v), "|tracktitle|") && !strings.Contains(tokenKeys(t.v), "|trackcleantitle|") {
|
||||
return fmt.Errorf("%s%s must contain {Track Title}", prefix, t.label)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkFormat(label, f string, folder bool) error {
|
||||
if strings.TrimSpace(f) == "" {
|
||||
return fmt.Errorf("%s is empty", label)
|
||||
}
|
||||
if strings.Contains(f, "..") || strings.Contains(f, "\\") || strings.HasPrefix(f, "/") || strings.HasSuffix(f, "/") {
|
||||
return fmt.Errorf("%s must be a relative name without .. or backslashes", label)
|
||||
}
|
||||
if folder && strings.Contains(f, "/") {
|
||||
return fmt.Errorf("%s must be a single folder name (no /)", label)
|
||||
}
|
||||
for _, m := range tokenPat.FindAllStringSubmatch(f, -1) {
|
||||
if numberTok.MatchString(m[1]) {
|
||||
continue
|
||||
}
|
||||
if !knownTokens[tokenKey(m[1])] {
|
||||
return fmt.Errorf("%s: unknown token {%s}", label, m[1])
|
||||
}
|
||||
}
|
||||
if strings.Count(f, "{") != strings.Count(f, "}") {
|
||||
return fmt.Errorf("%s has an unclosed {", label)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tokenKeys(f string) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("|")
|
||||
@@ -350,30 +450,62 @@ func valuesOf(f map[string]string) songValues {
|
||||
|
||||
func (v songValues) multiDisc() bool { return v.discTotal > 1 || v.disc > 1 }
|
||||
|
||||
// renderPath builds the organized relative path of a song. It returns the
|
||||
// missing tag when a required one is empty.
|
||||
func renderPath(c NamingConfig, ext string, f map[string]string) (string, string) {
|
||||
// renderPath builds the organized relative path of a song at rel. Songs in
|
||||
// a folder with its own naming stay in that folder. It returns why the song
|
||||
// can't be renamed instead when a tag is missing or the new path would enter
|
||||
// a folder that has its own naming.
|
||||
func renderPath(c NamingConfig, rel string, f map[string]string) (string, string) {
|
||||
ext := filepath.Ext(rel)
|
||||
if rule, ok := c.folderOf(rel); ok {
|
||||
to, why := renderFormats(c, rule.formats(), ext, f)
|
||||
if why != "" {
|
||||
return "", why
|
||||
}
|
||||
// Keep the folder as it is spelled on disk.
|
||||
return filepath.Join(rel[:len(rule.Folder)], to), ""
|
||||
}
|
||||
to, why := renderFormats(c, c.formats(), ext, f)
|
||||
if why != "" {
|
||||
return "", why
|
||||
}
|
||||
for _, rule := range c.Folders {
|
||||
if inFolder(to, rule.Folder) {
|
||||
return "", "would move into " + rule.Folder + ", which has its own naming"
|
||||
}
|
||||
}
|
||||
return to, ""
|
||||
}
|
||||
|
||||
var artistTokens = []string{"|artistname|", "|artistnamethe|", "|artistcleanname|", "|artistnamefirstcharacter|"}
|
||||
|
||||
func renderFormats(c NamingConfig, fm formats, ext string, f map[string]string) (string, string) {
|
||||
v := valuesOf(f)
|
||||
if v.artist == "" {
|
||||
return "", "artist or album artist"
|
||||
track := fm.track
|
||||
if v.multiDisc() {
|
||||
track = fm.multiDisc
|
||||
}
|
||||
used := tokenKeys(fm.artist + fm.album + track)
|
||||
for _, t := range artistTokens {
|
||||
if v.artist == "" && strings.Contains(used, t) {
|
||||
return "", "missing artist or album artist tag"
|
||||
}
|
||||
}
|
||||
if v.album == "" {
|
||||
return "", "album"
|
||||
return "", "missing album tag"
|
||||
}
|
||||
if v.title == "" {
|
||||
return "", "title"
|
||||
}
|
||||
track := c.TrackFormat
|
||||
if v.multiDisc() {
|
||||
track = c.MultiDiscFormat
|
||||
return "", "missing title tag"
|
||||
}
|
||||
var parts []string
|
||||
for _, f := range []string{c.ArtistFolder, c.AlbumFolder, track} {
|
||||
for _, f := range []string{fm.artist, fm.album, track} {
|
||||
if f == "" {
|
||||
continue // no artist folder
|
||||
}
|
||||
for _, part := range strings.Split(f, "/") {
|
||||
out := tokenPat.ReplaceAllStringFunc(part, func(tok string) string { return renderToken(tok[1:len(tok)-1], v, c) })
|
||||
out = finishComponent(out)
|
||||
if out == "" {
|
||||
return "", "tags for " + part
|
||||
return "", "missing tags for " + part
|
||||
}
|
||||
parts = append(parts, out)
|
||||
}
|
||||
@@ -465,12 +597,19 @@ func NamingExamples(c NamingConfig) []NamingExample {
|
||||
tags.Track: "7", tags.Date: "1986"}},
|
||||
}
|
||||
out := []NamingExample{}
|
||||
for _, s := range samples {
|
||||
p, missing := renderPath(c, s.ext, s.f)
|
||||
if missing != "" {
|
||||
p = "(skipped: missing " + missing + ")"
|
||||
add := func(label, rel string, f map[string]string) {
|
||||
p, why := renderPath(c, rel, f)
|
||||
if why != "" {
|
||||
p = "(skipped: " + why + ")"
|
||||
}
|
||||
out = append(out, NamingExample{Label: s.label, Path: p})
|
||||
out = append(out, NamingExample{Label: label, Path: p})
|
||||
}
|
||||
for _, s := range samples {
|
||||
add(s.label, "x"+s.ext, s.f)
|
||||
}
|
||||
for _, rule := range c.Folders {
|
||||
add("In "+rule.Folder, rule.Folder+"/x.flac", map[string]string{tags.Artist: "Koji Kondo", tags.Album: "Super Mario Galaxy",
|
||||
tags.Title: "Gusty Garden Galaxy", tags.Track: "3", tags.Date: "2007"})
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user