mirror of
https://github.com/thebaer/cdr.git
synced 2024-11-15 01:31:01 +00:00
Move track information into struct
This adds a NewTrack func that creates the struct, and we now use this in RenameTrack().
This commit is contained in:
parent
97c9e0d14f
commit
3125d14d23
36
sanitize.go
36
sanitize.go
@ -13,29 +13,41 @@ import (
|
|||||||
|
|
||||||
var trackNameReg = regexp.MustCompile("^([0-9]{2}).+")
|
var trackNameReg = regexp.MustCompile("^([0-9]{2}).+")
|
||||||
|
|
||||||
// RenameTrack takes a filename, opens it, reads the metadata, and returns both
|
func NewTrack(file string) *Track {
|
||||||
// the old and new filename.
|
|
||||||
func RenameTrack(file string) string {
|
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error loading file: %v", err)
|
fmt.Printf("error loading file: %v", err)
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fName := f.Name()
|
m, err := tag.ReadFrom(f)
|
||||||
fMatch := trackNameReg.FindStringSubmatch(fName)
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Track{
|
||||||
|
Title: m.Title(),
|
||||||
|
Artist: m.Artist(),
|
||||||
|
Filename: f.Name(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenameTrack takes a filename, opens it, reads the metadata, and returns both
|
||||||
|
// the old and new filename.
|
||||||
|
func RenameTrack(file string) string {
|
||||||
|
t := NewTrack(file)
|
||||||
|
|
||||||
|
// Extract playlist track number from filename
|
||||||
|
fMatch := trackNameReg.FindStringSubmatch(t.Filename)
|
||||||
if len(fMatch) < 2 {
|
if len(fMatch) < 2 {
|
||||||
log.Fatal("Unexpect filename format")
|
log.Fatal("Unexpect filename format")
|
||||||
}
|
}
|
||||||
trackNum := fMatch[1]
|
trackNum := fMatch[1]
|
||||||
ext := fName[strings.LastIndex(fName, "."):]
|
|
||||||
|
|
||||||
m, err := tag.ReadFrom(f)
|
ext := t.Filename[strings.LastIndex(t.Filename, "."):]
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
return fmt.Sprintf("%s-%s-%s%s", trackNum, Sanitize(t.Artist), Sanitize(t.Title), ext)
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s-%s-%s%s", trackNum, Sanitize(m.Artist()), Sanitize(m.Title()), ext)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sanitize takes a string and removes problematic characters from it.
|
// Sanitize takes a string and removes problematic characters from it.
|
||||||
|
Loading…
Reference in New Issue
Block a user