Skip to content

Commit

Permalink
Fix bug with 'й' letter and prevent blank filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayurifag committed Jul 17, 2024
1 parent 4082e5c commit 4c0b401
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ name: Go CI/CD Pipeline
branches:
- main
pull_request:
branches:
- main

jobs:
editorconfig:
Expand All @@ -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'
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/umom/processors/mp3_filename_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"
"os"
"path/filepath"
"unicode/utf8"

id3v2 "github.com/bogem/id3v2/v2"
)
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/umom/processors/mp3_tags_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/umom/processors/mp3_tags_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down

0 comments on commit 4c0b401

Please sign in to comment.