update album per track

This commit is contained in:
sentriz
2019-05-12 19:34:38 +01:00
parent 9992113e25
commit 5ac21cb852

View File

@@ -15,6 +15,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path" "path"
@@ -60,22 +61,50 @@ func readTags(fullPath string) (tag.Metadata, error) {
return tags, nil return tags, nil
} }
func updateAlbum(fullPath string, albumArtistID int, title string) {
if currentAlbum.ID != 0 {
return
}
directory, _ := path.Split(fullPath)
// update album table (the currentAlbum record will be updated when
// we exit this folder)
err := tx.Where("path = ?", directory).First(&currentAlbum).Error
if !gorm.IsRecordNotFoundError(err) {
// we found the record
// TODO: think about mod time here
return
}
currentAlbum = db.Album{
Path: directory,
AlbumArtistID: albumArtistID,
Title: title,
}
tx.Save(&currentAlbum)
}
func handleCover(fullPath string, stat os.FileInfo) error { func handleCover(fullPath string, stat os.FileInfo) error {
modTime := stat.ModTime() modTime := stat.ModTime()
err := tx.Where("path = ?", fullPath).First(&currentCover).Error err := tx.Where("path = ?", fullPath).First(&currentCover).Error
if !gorm.IsRecordNotFoundError(err) && if !gorm.IsRecordNotFoundError(err) &&
modTime.Before(currentCover.UpdatedAt) { modTime.Before(currentCover.UpdatedAt) {
// we found the record but it hasn't changed
return nil return nil
} }
currentCover = db.Cover{Path: fullPath} image, err := ioutil.ReadFile(fullPath)
if err != nil {
return fmt.Errorf("when reading cover: %v", err)
}
currentCover = db.Cover{
Path: fullPath,
Image: image,
}
tx.Save(&currentCover) tx.Save(&currentCover)
return nil return nil
} }
func handleFolder(fullPath string, stat os.FileInfo) error { func handleFolder(fullPath string, stat os.FileInfo) error {
modTime := stat.ModTime()
//
// update folder table for browsing by folder // update folder table for browsing by folder
modTime := stat.ModTime()
var folder db.Folder var folder db.Folder
err := tx.Where("path = ?", fullPath).First(&folder).Error err := tx.Where("path = ?", fullPath).First(&folder).Error
if !gorm.IsRecordNotFoundError(err) && if !gorm.IsRecordNotFoundError(err) &&
@@ -90,14 +119,6 @@ func handleFolder(fullPath string, stat os.FileInfo) error {
folder.Name = folderName folder.Name = folderName
tx.Save(&folder) tx.Save(&folder)
currentDirStack.Push(&folder) currentDirStack.Push(&folder)
//
// update album table (the currentAlbum record will be updated when
// we exit this folder)
err = tx.Where("path = ?", fullPath).First(&currentAlbum).Error
if gorm.IsRecordNotFoundError(err) {
currentAlbum = db.Album{Path: fullPath}
tx.Save(&currentAlbum)
}
return nil return nil
} }
@@ -108,14 +129,18 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
tx.Save(currentDir) tx.Save(currentDir)
currentAlbum.CoverID = currentCover.ID currentAlbum.CoverID = currentCover.ID
} }
tx.Save(&currentAlbum) if currentAlbum.ID != 0 {
tx.Save(&currentAlbum)
}
currentCover = db.Cover{} currentCover = db.Cover{}
currentAlbum = db.Album{}
currentDirStack.Pop() currentDirStack.Pop()
log.Printf("processed folder `%s`\n", fullPath) log.Printf("processed folder `%s`\n", fullPath)
return nil return nil
} }
func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error { func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
//
// set track basics // set track basics
var track db.Track var track db.Track
modTime := stat.ModTime() modTime := stat.ModTime()
@@ -143,6 +168,7 @@ func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
track.ContentType = mime track.ContentType = mime
track.Size = int(stat.Size()) track.Size = int(stat.Size())
track.FolderID = currentDirStack.PeekID() track.FolderID = currentDirStack.PeekID()
//
// set album artist basics // set album artist basics
var albumArtist db.AlbumArtist var albumArtist db.AlbumArtist
err = tx.Where("name = ?", tags.AlbumArtist()). err = tx.Where("name = ?", tags.AlbumArtist()).
@@ -153,16 +179,20 @@ func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
tx.Save(&albumArtist) tx.Save(&albumArtist)
} }
track.AlbumArtistID = albumArtist.ID track.AlbumArtistID = albumArtist.ID
//
// set temporary album's basics - will be updated with
// cover after the tracks
// inserted when we exit the folder
updateAlbum(fullPath, albumArtist.ID, tags.Album())
//
// update the track with our new album and finally save
track.AlbumID = currentAlbum.ID track.AlbumID = currentAlbum.ID
tx.Save(&track) tx.Save(&track)
// update the current album's metadata - it will be
// inserted when we exit the folder
currentAlbum.AlbumArtistID = albumArtist.ID
currentAlbum.Title = tags.Album()
return nil return nil
} }
func handleItem(fullPath string, info *godirwalk.Dirent) error { func handleItem(fullPath string, info *godirwalk.Dirent) error {
// seenPaths = append(seenPaths, fullPath)
seenPaths[fullPath] = true seenPaths[fullPath] = true
stat, err := os.Stat(fullPath) stat, err := os.Stat(fullPath)
if err != nil { if err != nil {