Skip to content

Commit

Permalink
update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed Sep 27, 2024
1 parent e5d9fde commit 19043d6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
21 changes: 21 additions & 0 deletions utils/dotprop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import "strings"

// GetNestedValue retrieves a value from a nested map given a dot-separated key.
func GetNestedValue(data map[string]interface{}, key string) (interface{}, bool) {
keys := strings.Split(key, ".")
var current interface{} = data
for _, k := range keys {
if m, ok := current.(map[string]interface{}); ok {
if val, exists := m[k]; exists {
current = val
} else {
return nil, false
}
} else {
return nil, false
}
}
return current, true
}
6 changes: 3 additions & 3 deletions utils/filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func SanitizeFileName(name string) string {
safeName := reg.ReplaceAllString(name, "_")
logger.Debug("Replaced invalid characters in file name: %s", safeName)

// Trim trailing periods and spaces which are also problematic
safeName = strings.TrimRight(safeName, ". ")
logger.Debug("Trimmed trailing periods and spaces: %s", safeName)
// Trim leading and trailing periods and spaces which are also problematic
safeName = strings.Trim(safeName, ". ")
logger.Debug("Trimmed leading and trailing periods and spaces: %s", safeName)

return safeName
}
5 changes: 4 additions & 1 deletion utils/filename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func TestSanitizeFileName(t *testing.T) {
{".", ""},
{" ", ""},
{"...", ""},
{"..filename..", "..filename"},
{"..filename..", "filename"},
{"filename...", "filename"},
{"...filename", "filename"},
{"...file...name...", "file...name"},
}

for _, test := range tests {
Expand Down
73 changes: 56 additions & 17 deletions utils/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ type SaveLayoutProps struct {
TrackNumber bool
}

// atoiOrZero converts a string to an int, returning 0 if conversion fails.
func atoiOrZero(s string) int {
num, err := strconv.Atoi(s)
if err != nil {
return 0
}
return num
}

// SaveLayout formats the file path using placeholders and metadata from the track and album.
func SaveLayout(props SaveLayoutProps) string {
logger.Debug("Starting save layout formatting for path: %s", props.Path)
Expand All @@ -37,6 +46,22 @@ func SaveLayout(props SaveLayoutProps) string {
albumInfo[k] = v
}

// Adjust ALB_TITLE if necessary
trackDiskNumber, okTrackDisk := props.Track["DISK_NUMBER"]
albumNumberDisk, okAlbumDisk := props.Album["NUMBER_DISK"]
albumAlbTitle, okAlbumTitle := albumInfo["ALB_TITLE"]

if okTrackDisk && okAlbumDisk && okAlbumTitle {
numDisks := atoiOrZero(fmt.Sprintf("%v", albumNumberDisk))
if numDisks > 1 {
albumTitleStr := fmt.Sprintf("%v", albumAlbTitle)
if !strings.Contains(albumTitleStr, "Disc") {
discNumber := atoiOrZero(fmt.Sprintf("%v", trackDiskNumber))
albumInfo["ALB_TITLE"] = fmt.Sprintf("%s (Disc %02d)", albumTitleStr, discNumber)
}
}
}

// Use relative path if it starts with '{'
if strings.HasPrefix(props.Path, "{") {
props.Path = "./" + props.Path
Expand All @@ -51,40 +76,54 @@ func SaveLayout(props SaveLayoutProps) string {
key := match[1]
logger.Debug("Processing key: %s", key)

valueAlbum, okAlbum := albumInfo[key]
valueTrack, okTrack := props.Track[key]

var value string
if okAlbum {
value = fmt.Sprintf("%v", valueAlbum)
logger.Debug("Found value from album: %s = %s", key, value)
} else if okTrack {
value = fmt.Sprintf("%v", valueTrack)
logger.Debug("Found value from track: %s = %s", key, value)
var value interface{}
if val, ok := GetNestedValue(albumInfo, key); ok {
value = val
logger.Debug("Found value from album: %s = %v", key, value)
} else if val, ok := GetNestedValue(props.Track, key); ok {
value = val
logger.Debug("Found value from track: %s = %v", key, value)
} else {
value = ""
}

if key == "TRACK_NUMBER" || key == "TRACK_POSITION" || key == "NO_TRACK_NUMBER" {
if value != "" {
num, _ := strconv.Atoi(value)
props.Path = strings.Replace(props.Path, "{"+key+"}", fmt.Sprintf("%0*d", props.MinimumIntegerDigits, num), -1)
num := atoiOrZero(fmt.Sprintf("%v", value))
formattedNum := fmt.Sprintf("%0*d", props.MinimumIntegerDigits, num)
props.Path = strings.ReplaceAll(props.Path, "{"+key+"}", formattedNum)
logger.Debug("Formatted track number for key %s: %s", key, props.Path)
} else {
props.Path = strings.Replace(props.Path, "{"+key+"}", "", -1)
props.Path = strings.ReplaceAll(props.Path, "{"+key+"}", "")
logger.Debug("Key %s had no value; replaced with empty string.", key)
}
props.TrackNumber = false
} else {
props.Path = strings.Replace(props.Path, "{"+key+"}", SanitizeFileName(value), -1)
sanitizedValue := SanitizeFileName(fmt.Sprintf("%v", value))
props.Path = strings.ReplaceAll(props.Path, "{"+key+"}", sanitizedValue)
logger.Debug("Replaced key %s with sanitized value: %s", key, props.Path)
}
}

if props.TrackNumber {
if trackNum, exists := props.Track["TRACK_NUMBER"]; exists {
trackNumber := fmt.Sprintf("%0*d", props.MinimumIntegerDigits, trackNum)
props.Path = filepath.Join(filepath.Dir(props.Path), trackNumber+" - "+filepath.Base(props.Path))
var position interface{}
if pos, exists := props.Track["TRACK_POSITION"]; exists {
position = pos
} else if num, exists := props.Track["TRACK_NUMBER"]; exists {
position = num
}
if position != nil {
num := atoiOrZero(fmt.Sprintf("%v", position))
trackNumber := fmt.Sprintf("%0*d", props.MinimumIntegerDigits, num)
dir := filepath.Dir(props.Path)
base := filepath.Base(props.Path)
props.Path = filepath.Join(dir, trackNumber+" - "+base)
logger.Debug("Appended track number to path: %s", props.Path)
} else {
props.Path = filepath.Join(props.Path)
}
} else {
props.Path = filepath.Join(props.Path)
}

// Remove any remaining problematic characters
Expand Down

0 comments on commit 19043d6

Please sign in to comment.