clean up scanner order and surface
This commit is contained in:
6
db/db.go
6
db/db.go
@@ -55,6 +55,12 @@ func (db *DB) GetUserFromName(name string) *model.User {
|
|||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) WithTx(cb func(tx *gorm.DB)) {
|
||||||
|
tx := db.Begin()
|
||||||
|
defer tx.Commit()
|
||||||
|
cb(tx)
|
||||||
|
}
|
||||||
|
|
||||||
func New(path string) (*DB, error) {
|
func New(path string) (*DB, error) {
|
||||||
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
||||||
db, err := gorm.Open("sqlite3", pathAndArgs)
|
db, err := gorm.Open("sqlite3", pathAndArgs)
|
||||||
|
|||||||
@@ -39,39 +39,6 @@ func SetScanning(status bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var coverFilenames = map[string]struct{}{
|
|
||||||
"cover.png": {},
|
|
||||||
"cover.jpg": {},
|
|
||||||
"cover.jpeg": {},
|
|
||||||
"folder.png": {},
|
|
||||||
"folder.jpg": {},
|
|
||||||
"folder.jpeg": {},
|
|
||||||
"album.png": {},
|
|
||||||
"album.jpg": {},
|
|
||||||
"album.jpeg": {},
|
|
||||||
"front.png": {},
|
|
||||||
"front.jpg": {},
|
|
||||||
"front.jpeg": {},
|
|
||||||
}
|
|
||||||
|
|
||||||
// decoded converts a string to it's latin equivalent. it
|
|
||||||
// will be used by the model's *UDec fields, and is only set
|
|
||||||
// if it differs from the original.
|
|
||||||
// the fields are used for searching
|
|
||||||
func decoded(in string) string {
|
|
||||||
result := unidecode.Unidecode(in)
|
|
||||||
if result == in {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func withTx(db *db.DB, cb func(tx *gorm.DB)) {
|
|
||||||
tx := db.Begin()
|
|
||||||
defer tx.Commit()
|
|
||||||
cb(tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Scanner struct {
|
type Scanner struct {
|
||||||
db *db.DB
|
db *db.DB
|
||||||
musicPath string
|
musicPath string
|
||||||
@@ -131,7 +98,7 @@ func (s *Scanner) Start() error {
|
|||||||
start = time.Now()
|
start = time.Now()
|
||||||
var deleted uint
|
var deleted uint
|
||||||
// delete tracks not on filesystem
|
// delete tracks not on filesystem
|
||||||
withTx(s.db, func(tx *gorm.DB) {
|
s.db.WithTx(func(tx *gorm.DB) {
|
||||||
var tracks []*model.Track
|
var tracks []*model.Track
|
||||||
tx.
|
tx.
|
||||||
Select("id").
|
Select("id").
|
||||||
@@ -145,7 +112,7 @@ func (s *Scanner) Start() error {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
// delete folders not on filesystem
|
// delete folders not on filesystem
|
||||||
withTx(s.db, func(tx *gorm.DB) {
|
s.db.WithTx(func(tx *gorm.DB) {
|
||||||
var folders []*model.Album
|
var folders []*model.Album
|
||||||
tx.
|
tx.
|
||||||
Select("id").
|
Select("id").
|
||||||
@@ -190,6 +157,23 @@ type item struct {
|
|||||||
stat os.FileInfo
|
stat os.FileInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var coverFilenames = map[string]struct{}{
|
||||||
|
"cover.png": {},
|
||||||
|
"cover.jpg": {},
|
||||||
|
"cover.jpeg": {},
|
||||||
|
"folder.png": {},
|
||||||
|
"folder.jpg": {},
|
||||||
|
"folder.jpeg": {},
|
||||||
|
"album.png": {},
|
||||||
|
"album.jpg": {},
|
||||||
|
"album.jpeg": {},
|
||||||
|
"front.png": {},
|
||||||
|
"front.jpg": {},
|
||||||
|
"front.jpeg": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
// ## begin callbacks
|
||||||
|
|
||||||
func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error {
|
func (s *Scanner) callbackItem(fullPath string, info *godirwalk.Dirent) error {
|
||||||
stat, err := os.Stat(fullPath)
|
stat, err := os.Stat(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -242,6 +226,20 @@ func (s *Scanner) callbackPost(fullPath string, info *godirwalk.Dirent) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// decoded converts a string to it's latin equivalent. it
|
||||||
|
// will be used by the model's *UDec fields, and is only set
|
||||||
|
// if it differs from the original.
|
||||||
|
// the fields are used for searching
|
||||||
|
func decoded(in string) string {
|
||||||
|
result := unidecode.Unidecode(in)
|
||||||
|
if result == in {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ## begin handlers
|
||||||
|
|
||||||
func (s *Scanner) handleFolder(it *item) error {
|
func (s *Scanner) handleFolder(it *item) error {
|
||||||
folder := &model.Album{}
|
folder := &model.Album{}
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -299,9 +297,9 @@ func (s *Scanner) handleTrack(it *item) error {
|
|||||||
track.AlbumID = s.curFolders.PeekID()
|
track.AlbumID = s.curFolders.PeekID()
|
||||||
trTags, err := tags.New(it.fullPath)
|
trTags, err := tags.New(it.fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// not returning the error here because we don't
|
// not returning the error here because we don't want
|
||||||
// want the entire walk to stop if we can't read
|
// the entire walk to stop if we can't read the tags
|
||||||
// the tags of a single file
|
// of a single file
|
||||||
log.Printf("error reading tags `%s`: %v", it.relPath, err)
|
log.Printf("error reading tags `%s`: %v", it.relPath, err)
|
||||||
s.seenTracksErr++
|
s.seenTracksErr++
|
||||||
return nil
|
return nil
|
||||||
@@ -312,15 +310,15 @@ func (s *Scanner) handleTrack(it *item) error {
|
|||||||
track.TagTrackNumber = trTags.TrackNumber()
|
track.TagTrackNumber = trTags.TrackNumber()
|
||||||
track.TagDiscNumber = trTags.DiscNumber()
|
track.TagDiscNumber = trTags.DiscNumber()
|
||||||
track.Length = trTags.Length() // these two should be calculated
|
track.Length = trTags.Length() // these two should be calculated
|
||||||
track.Bitrate = trTags.Bitrate() // from the file instead of tags
|
track.Bitrate = trTags.Bitrate() // ...from the file instead of tags
|
||||||
//
|
//
|
||||||
// set album artist basics
|
// set album artist basics
|
||||||
artistName := func() string {
|
artistName := func() string {
|
||||||
if ret := trTags.AlbumArtist(); ret != "" {
|
if r := trTags.AlbumArtist(); r != "" {
|
||||||
return ret
|
return r
|
||||||
}
|
}
|
||||||
if ret := trTags.Artist(); ret != "" {
|
if r := trTags.Artist(); r != "" {
|
||||||
return ret
|
return r
|
||||||
}
|
}
|
||||||
return "Unknown Artist"
|
return "Unknown Artist"
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user