Skip to content

Commit

Permalink
improve layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed Sep 27, 2024
1 parent 19043d6 commit 1f8647f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 8 additions & 1 deletion utils/filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"regexp"
"runtime"
"strings"

"github.com/d-fi/GoFi/logger"
Expand All @@ -12,7 +13,7 @@ func SanitizeFileName(name string) string {
logger.Debug("Sanitizing file name: %s", name)

// Define a regex to match invalid characters
reg := regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F]`)
reg := regexp.MustCompile(`[\/\?<>\\:\*\|"\x00-\x1F]`)

// Replace invalid characters with underscores
safeName := reg.ReplaceAllString(name, "_")
Expand All @@ -22,5 +23,11 @@ func SanitizeFileName(name string) string {
safeName = strings.Trim(safeName, ". ")
logger.Debug("Trimmed leading and trailing periods and spaces: %s", safeName)

// On Windows, filenames cannot end with a period
if strings.HasSuffix(safeName, ".") && strings.ToLower(runtime.GOOS) == "windows" {
safeName = safeName[:len(safeName)-1] + "_"
logger.Debug("Adjusted filename for Windows: %s", safeName)
}

return safeName
}
8 changes: 1 addition & 7 deletions utils/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ func SaveLayout(props SaveLayoutProps) string {
}
}

// Use relative path if it starts with '{'
if strings.HasPrefix(props.Path, "{") {
props.Path = "./" + props.Path
logger.Debug("Updated path to be relative: %s", props.Path)
}

// Find keys inside {}
re := regexp.MustCompile(`\{([^}]*)\}`)
matches := re.FindAllStringSubmatch(props.Path, -1)
Expand All @@ -87,7 +81,7 @@ func SaveLayout(props SaveLayoutProps) string {
value = ""
}

if key == "TRACK_NUMBER" || key == "TRACK_POSITION" || key == "NO_TRACK_NUMBER" {
if key == "TRACK_NUMBER" || key == "TRACK_POSITION" || key == "NO_TRACK_NUMBER" || strings.HasSuffix(key, "TRACK_NUMBER") || strings.HasSuffix(key, "TRACK_POSITION") {
if value != "" {
num := atoiOrZero(fmt.Sprintf("%v", value))
formattedNum := fmt.Sprintf("%0*d", props.MinimumIntegerDigits, num)
Expand Down

0 comments on commit 1f8647f

Please sign in to comment.