mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-19 13:27:42 +03:00
Only read tag when the track was changed
This commit is contained in:
parent
be28ce1178
commit
0e492a053c
@ -7,12 +7,13 @@ import (
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/persistence"
|
||||
"github.com/deluan/gosonic/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Scanner interface {
|
||||
ScanLibrary(path string) (int, error)
|
||||
ScanLibrary(lastModifiedSince time.Time, path string) (int, error)
|
||||
MediaFiles() map[string]*domain.MediaFile
|
||||
Albums() map[string]*domain.Album
|
||||
Artists() map[string]*domain.Artist
|
||||
@ -47,7 +48,7 @@ type Importer struct {
|
||||
}
|
||||
|
||||
func (i *Importer) Run() {
|
||||
if total, err := i.scanner.ScanLibrary(i.mediaFolder); err != nil {
|
||||
if total, err := i.scanner.ScanLibrary(i.lastModifiedSince(), i.mediaFolder); err != nil {
|
||||
beego.Error("Error importing iTunes Library:", err)
|
||||
return
|
||||
} else {
|
||||
@ -62,6 +63,16 @@ func (i *Importer) Run() {
|
||||
beego.Info("Finished importing tracks from iTunes Library")
|
||||
}
|
||||
|
||||
func (i *Importer) lastModifiedSince() time.Time {
|
||||
ms, err := i.propertyRepo.Get(consts.LastScan)
|
||||
if err != nil {
|
||||
beego.Warn("Couldn't read LastScan:", err)
|
||||
return time.Time{}
|
||||
}
|
||||
s, _ := strconv.ParseInt(ms, 10, 64)
|
||||
return time.Unix(0, s*int64(time.Millisecond))
|
||||
}
|
||||
|
||||
func (i *Importer) importLibrary() (err error) {
|
||||
indexGroups := utils.ParseIndexGroups(beego.AppConfig.String("indexGroups"))
|
||||
artistIndex := make(map[string]tempIndex)
|
||||
@ -97,7 +108,7 @@ func (i *Importer) importLibrary() (err error) {
|
||||
beego.Info("Total MediaFiles in database:", c)
|
||||
|
||||
if err == nil {
|
||||
millis := time.Now().UnixNano() / 1000000
|
||||
millis := time.Now().UnixNano() / int64(time.Millisecond)
|
||||
i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
|
||||
beego.Info("LastScan timestamp:", millis)
|
||||
}
|
||||
|
@ -12,16 +12,19 @@ import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ItunesScanner struct {
|
||||
mediaFiles map[string]*domain.MediaFile
|
||||
albums map[string]*domain.Album
|
||||
artists map[string]*domain.Artist
|
||||
mediaFiles map[string]*domain.MediaFile
|
||||
albums map[string]*domain.Album
|
||||
artists map[string]*domain.Artist
|
||||
lastModifiedSince time.Time
|
||||
}
|
||||
|
||||
func (s *ItunesScanner) ScanLibrary(path string) (int, error) {
|
||||
func (s *ItunesScanner) ScanLibrary(lastModifiedSince time.Time, path string) (int, error) {
|
||||
beego.Info("Starting iTunes import from:", path)
|
||||
beego.Info("Checking for updates since", lastModifiedSince.String())
|
||||
xml, _ := os.Open(path)
|
||||
l, err := itl.ReadFromXML(xml)
|
||||
if err != nil {
|
||||
@ -29,6 +32,7 @@ func (s *ItunesScanner) ScanLibrary(path string) (int, error) {
|
||||
}
|
||||
beego.Info("Loaded", len(l.Tracks), "tracks")
|
||||
|
||||
s.lastModifiedSince = lastModifiedSince
|
||||
s.mediaFiles = make(map[string]*domain.MediaFile)
|
||||
s.albums = make(map[string]*domain.Album)
|
||||
s.artists = make(map[string]*domain.Artist)
|
||||
@ -39,6 +43,8 @@ func (s *ItunesScanner) ScanLibrary(path string) (int, error) {
|
||||
ar := s.collectArtists(&t)
|
||||
mf := s.collectMediaFiles(&t)
|
||||
s.collectAlbums(&t, mf, ar)
|
||||
} else {
|
||||
beego.Trace("Skipped", t.Location, " - kind:", t.Kind)
|
||||
}
|
||||
i++
|
||||
if i%1000 == 0 {
|
||||
@ -87,7 +93,10 @@ func (s *ItunesScanner) collectMediaFiles(t *itl.Track) *domain.MediaFile {
|
||||
path = strings.TrimPrefix(unescape(path), "file://")
|
||||
mf.Path = path
|
||||
mf.Suffix = strings.TrimPrefix(filepath.Ext(path), ".")
|
||||
mf.HasCoverArt = hasCoverArt(path)
|
||||
|
||||
if mf.UpdatedAt.After(s.lastModifiedSince) {
|
||||
mf.HasCoverArt = hasCoverArt(path)
|
||||
}
|
||||
|
||||
mf.CreatedAt = t.DateAdded
|
||||
mf.UpdatedAt = t.DateModified
|
||||
|
Loading…
x
Reference in New Issue
Block a user