move tags to their own package
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/sentriz/gonic/mime"
|
||||
"github.com/sentriz/gonic/model"
|
||||
"github.com/sentriz/gonic/scanner/tags"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -249,7 +250,7 @@ func (s *Scanner) handleTrack(it *item) error {
|
||||
track.Filename = it.filename
|
||||
track.Size = int(it.stat.Size())
|
||||
track.AlbumID = s.curFolderID()
|
||||
tags, err := readTags(it.fullPath)
|
||||
trTags, err := tags.New(it.fullPath)
|
||||
if err != nil {
|
||||
// not returning the error here because we don't
|
||||
// want the entire walk to stop if we can't read
|
||||
@@ -258,21 +259,21 @@ func (s *Scanner) handleTrack(it *item) error {
|
||||
s.seenTracksErr++
|
||||
return nil
|
||||
}
|
||||
track.TagTitle = tags.Title()
|
||||
track.TagTrackArtist = tags.Artist()
|
||||
track.TagTrackNumber = tags.TrackNumber()
|
||||
track.TagDiscNumber = tags.DiscNumber()
|
||||
track.Length = tags.Length() // these two should be calculated
|
||||
track.Bitrate = tags.Bitrate() // from the file instead of tags
|
||||
track.TagTitle = trTags.Title()
|
||||
track.TagTrackArtist = trTags.Artist()
|
||||
track.TagTrackNumber = trTags.TrackNumber()
|
||||
track.TagDiscNumber = trTags.DiscNumber()
|
||||
track.Length = trTags.Length() // these two should be calculated
|
||||
track.Bitrate = trTags.Bitrate() // from the file instead of tags
|
||||
//
|
||||
// set album artist basics
|
||||
artist := &model.Artist{}
|
||||
err = s.tx.
|
||||
Where("name = ?", tags.AlbumArtist()).
|
||||
Where("name = ?", trTags.AlbumArtist()).
|
||||
First(artist).
|
||||
Error
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
artist.Name = tags.AlbumArtist()
|
||||
artist.Name = trTags.AlbumArtist()
|
||||
s.tx.Save(artist)
|
||||
}
|
||||
track.ArtistID = artist.ID
|
||||
@@ -286,8 +287,8 @@ func (s *Scanner) handleTrack(it *item) error {
|
||||
// the folder hasn't been modified or already has it's tags
|
||||
return nil
|
||||
}
|
||||
folder.TagTitle = tags.Album()
|
||||
folder.TagYear = tags.Year()
|
||||
folder.TagTitle = trTags.Album()
|
||||
folder.TagYear = trTags.Year()
|
||||
folder.TagArtistID = artist.ID
|
||||
folder.ReceivedTags = true
|
||||
return nil
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/nicksellen/audiotags"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type tags struct {
|
||||
map_ map[string]string
|
||||
props *audiotags.AudioProperties
|
||||
}
|
||||
|
||||
func readTags(path string) (*tags, error) {
|
||||
map_, props, err := audiotags.Read(path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "audiotags module")
|
||||
}
|
||||
return &tags{
|
||||
map_: map_,
|
||||
props: props,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *tags) firstTag(keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if val, ok := t.map_[key]; ok {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func intSep(in, sep string) int {
|
||||
if in == "" {
|
||||
return 0
|
||||
}
|
||||
start := strings.SplitN(in, sep, 2)[0]
|
||||
out, err := strconv.Atoi(start)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (t *tags) Title() string { return t.firstTag("title") }
|
||||
func (t *tags) Artist() string { return t.firstTag("artist") }
|
||||
func (t *tags) Album() string { return t.firstTag("album") }
|
||||
func (t *tags) AlbumArtist() string { return t.firstTag("albumartist", "album artist") }
|
||||
func (t *tags) Year() int { return intSep(t.firstTag("date", "year"), "-") } // eg. 2019-6-11
|
||||
func (t *tags) TrackNumber() int { return intSep(t.firstTag("tracknumber"), "/") } // eg. 5/12
|
||||
func (t *tags) DiscNumber() int { return intSep(t.firstTag("discnumber"), "/") } // eg. 1/2
|
||||
func (t *tags) Length() int { return t.props.Length }
|
||||
func (t *tags) Bitrate() int { return t.props.Bitrate }
|
||||
41
scanner/tags/tags.go
Normal file
41
scanner/tags/tags.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"github.com/nicksellen/audiotags"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Tags struct {
|
||||
raw map[string]string
|
||||
props *audiotags.AudioProperties
|
||||
}
|
||||
|
||||
func New(path string) (*Tags, error) {
|
||||
raw, props, err := audiotags.Read(path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "audiotags module")
|
||||
}
|
||||
return &Tags{
|
||||
raw: raw,
|
||||
props: props,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *Tags) firstTag(keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if val, ok := t.raw[key]; ok {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *Tags) Title() string { return t.firstTag("title") }
|
||||
func (t *Tags) Artist() string { return t.firstTag("artist") }
|
||||
func (t *Tags) Album() string { return t.firstTag("album") }
|
||||
func (t *Tags) AlbumArtist() string { return t.firstTag("albumartist", "album artist") }
|
||||
func (t *Tags) Year() int { return intSep(t.firstTag("date", "year"), "-") } // eg. 2019-6-11
|
||||
func (t *Tags) TrackNumber() int { return intSep(t.firstTag("tracknumber"), "/") } // eg. 5/12
|
||||
func (t *Tags) DiscNumber() int { return intSep(t.firstTag("discnumber"), "/") } // eg. 1/2
|
||||
func (t *Tags) Length() int { return t.props.Length }
|
||||
func (t *Tags) Bitrate() int { return t.props.Bitrate }
|
||||
18
scanner/tags/utilities.go
Normal file
18
scanner/tags/utilities.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package tags
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func intSep(in, sep string) int {
|
||||
if in == "" {
|
||||
return 0
|
||||
}
|
||||
start := strings.SplitN(in, sep, 2)[0]
|
||||
out, err := strconv.Atoi(start)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user