From 4c0b401aace49bcb222e10a0314bc7e159a71b24 Mon Sep 17 00:00:00 2001 From: Vladislav Ponomarev Date: Wed, 17 Jul 2024 19:40:48 +0700 Subject: [PATCH] =?UTF-8?q?Fix=20bug=20with=20'=D0=B9'=20letter=20and=20pr?= =?UTF-8?q?event=20blank=20filenames?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 8 +++----- src/umom/processors/mp3_filename_processor.go | 19 +++++++++++++++++-- src/umom/processors/mp3_tags_processor.go | 1 + .../processors/mp3_tags_processor_test.go | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36ab453..2ec9fc5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,8 +6,6 @@ name: Go CI/CD Pipeline branches: - main pull_request: - branches: - - main jobs: editorconfig: @@ -21,7 +19,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - uses: DavidAnson/markdownlint-cli2-action@v16 with: globs: '**/*.md' @@ -30,7 +28,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v5 with: @@ -59,7 +57,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 diff --git a/src/umom/processors/mp3_filename_processor.go b/src/umom/processors/mp3_filename_processor.go index 34c5326..fe5e7cd 100644 --- a/src/umom/processors/mp3_filename_processor.go +++ b/src/umom/processors/mp3_filename_processor.go @@ -4,6 +4,7 @@ import ( "log" "os" "path/filepath" + "unicode/utf8" id3v2 "github.com/bogem/id3v2/v2" ) @@ -22,17 +23,31 @@ func ProcessMP3FileName(path string) (newFilePath string, err error) { title := mp3File.Title() mp3File.Close() + if artist == "" || title == "" { + log.Printf("Skipping renaming since %s does not have artist or title tags.\n", path) + return path, nil + } + newFileName := artist + " - " + title + ".mp3" dir := filepath.Dir(path) + originalFileName := filepath.Base(path) newFilePath = filepath.Join(dir, newFileName) // Skip remaming if the new file name is the same as the existing one - if path == newFilePath { + // In Go, string comparison is based on byte comparison by default, not on + // Unicode code points. This can lead to unexpected behavior when dealing + // with non-ASCII characters. + + // Convert strings to runes + r1, _ := utf8.DecodeRuneInString(originalFileName) + r2, _ := utf8.DecodeRuneInString(newFileName) + + if r1 == r2 { // log.Printf("Skipping renaming since %s already has the correct name.\n", path) return path, nil } - log.Printf("Renaming %s to %s\n", path, newFileName) + log.Printf("Renaming %q to %q\n", originalFileName, newFileName) // Rename the file if err := os.Rename(path, newFilePath); err != nil { diff --git a/src/umom/processors/mp3_tags_processor.go b/src/umom/processors/mp3_tags_processor.go index 90ec902..6d6250f 100644 --- a/src/umom/processors/mp3_tags_processor.go +++ b/src/umom/processors/mp3_tags_processor.go @@ -68,6 +68,7 @@ func normalizeTags(tag *id3v2.Tag) error { return nil } +// SUGGEST: should I add here \t\n\r\f\v characters? var forbiddenChars = regexp.MustCompile(`[<>:"/\\|?*]`) // Windows forbidden characters // Converts tags to UTF-16 encoding diff --git a/src/umom/processors/mp3_tags_processor_test.go b/src/umom/processors/mp3_tags_processor_test.go index 05de8b4..584bd48 100644 --- a/src/umom/processors/mp3_tags_processor_test.go +++ b/src/umom/processors/mp3_tags_processor_test.go @@ -28,7 +28,7 @@ func TestProcessMP3FileTags(t *testing.T) { tag.SetTitle(" Xtal ") comment := id3v2.CommentFrame{ - Encoding: id3v2.EncodingUTF8, + Encoding: id3v2.EncodingUTF16, Language: "eng", Description: "My opinion", Text: "I like this song!",