dont insert if no need

This commit is contained in:
sentriz
2019-05-13 23:28:53 +01:00
parent 5ac21cb852
commit bd95806a23
5 changed files with 82 additions and 40 deletions

View File

@@ -42,10 +42,10 @@ var (
// currentCover because we find a cover anywhere among the tracks during the
// walk and need a reference to it when we update folder and album records
// when we exit a folder
currentCover = db.Cover{}
currentCover = &db.Cover{}
// currentAlbum because we update this record when we exit a folder with
// our new reference to it's cover
currentAlbum = db.Album{}
currentAlbum = &db.Album{}
)
func readTags(fullPath string) (tag.Metadata, error) {
@@ -68,23 +68,23 @@ func updateAlbum(fullPath string, albumArtistID int, title string) {
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
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{
currentAlbum = &db.Album{
Path: directory,
AlbumArtistID: albumArtistID,
Title: title,
}
tx.Save(&currentAlbum)
tx.Save(currentAlbum)
}
func handleCover(fullPath string, stat os.FileInfo) error {
modTime := stat.ModTime()
err := tx.Where("path = ?", fullPath).First(&currentCover).Error
err := tx.Where("path = ?", fullPath).First(currentCover).Error
if !gorm.IsRecordNotFoundError(err) &&
modTime.Before(currentCover.UpdatedAt) {
// we found the record but it hasn't changed
@@ -94,47 +94,47 @@ func handleCover(fullPath string, stat os.FileInfo) error {
if err != nil {
return fmt.Errorf("when reading cover: %v", err)
}
currentCover = db.Cover{
Path: fullPath,
Image: image,
currentCover = &db.Cover{
Path: fullPath,
Image: image,
NewlyInserted: true,
}
tx.Save(&currentCover)
tx.Save(currentCover)
return nil
}
func handleFolder(fullPath string, stat os.FileInfo) error {
// update folder table for browsing by folder
folder := &db.Folder{}
defer currentDirStack.Push(folder)
modTime := stat.ModTime()
var folder db.Folder
err := tx.Where("path = ?", fullPath).First(&folder).Error
err := tx.Where("path = ?", fullPath).First(folder).Error
if !gorm.IsRecordNotFoundError(err) &&
modTime.Before(folder.UpdatedAt) {
// we found the record but it hasn't changed
currentDirStack.Push(&folder)
return nil
}
_, folderName := path.Split(fullPath)
folder.Path = fullPath
folder.ParentID = currentDirStack.PeekID()
folder.Name = folderName
tx.Save(&folder)
currentDirStack.Push(&folder)
tx.Save(folder)
return nil
}
func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
if currentCover.ID != 0 {
currentDir := currentDirStack.Peek()
currentDir := currentDirStack.Peek()
defer currentDirStack.Pop()
if currentCover.NewlyInserted {
currentDir.CoverID = currentCover.ID
tx.Save(currentDir)
currentAlbum.CoverID = currentCover.ID
}
if currentAlbum.ID != 0 {
tx.Save(&currentAlbum)
currentAlbum.CoverID = currentCover.ID
tx.Save(currentAlbum)
}
currentCover = db.Cover{}
currentAlbum = db.Album{}
currentDirStack.Pop()
currentCover = &db.Cover{}
currentAlbum = &db.Album{}
log.Printf("processed folder `%s`\n", fullPath)
return nil
}
@@ -142,9 +142,9 @@ func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
//
// set track basics
var track db.Track
track := &db.Track{}
modTime := stat.ModTime()
err := tx.Where("path = ?", fullPath).First(&track).Error
err := tx.Where("path = ?", fullPath).First(track).Error
if !gorm.IsRecordNotFoundError(err) &&
modTime.Before(track.UpdatedAt) {
// we found the record but it hasn't changed
@@ -170,24 +170,23 @@ func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
track.FolderID = currentDirStack.PeekID()
//
// set album artist basics
var albumArtist db.AlbumArtist
albumArtist := &db.AlbumArtist{}
err = tx.Where("name = ?", tags.AlbumArtist()).
First(&albumArtist).
First(albumArtist).
Error
if gorm.IsRecordNotFoundError(err) {
albumArtist.Name = tags.AlbumArtist()
tx.Save(&albumArtist)
tx.Save(albumArtist)
}
track.AlbumArtistID = albumArtist.ID
//
// set temporary album's basics - will be updated with
// cover after the tracks
// inserted when we exit the folder
// 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
tx.Save(&track)
tx.Save(track)
return nil
}

View File

@@ -56,8 +56,9 @@ type Track struct {
type Cover struct {
IDBase
CrudBase
Image []byte
Path string `gorm:"not null;unique_index"`
Image []byte
Path string `gorm:"not null;unique_index"`
NewlyInserted bool `gorm:"-"`
}
// User represents the users table

View File

@@ -1,6 +1,7 @@
package handler
import (
"fmt"
"net/http"
"github.com/sentriz/gonic/db"
@@ -12,6 +13,7 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
// for this, so we're going to return root directories as "artists"
var folders []*db.Folder
c.DB.Where("parent_id = ?", 1).Find(&folders)
fmt.Println(folders, "++++++++")
var indexMap = make(map[rune]*subsonic.Index)
var indexes []*subsonic.Index
for _, folder := range folders {
@@ -60,10 +62,11 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
}
for _, folder := range folders {
sub.Directory.Children = append(sub.Directory.Children, &subsonic.Child{
Parent: cFolder.ID,
ID: folder.ID,
Title: folder.Name,
IsDir: true,
Parent: cFolder.ID,
ID: folder.ID,
Title: folder.Name,
IsDir: true,
CoverID: folder.CoverID,
})
}
respond(w, r, sub)

View File

@@ -64,7 +64,7 @@ func (c *Controller) GetArtist(w http.ResponseWriter, r *http.Request) {
Created: album.CreatedAt,
Artist: artist.Name,
ArtistID: artist.ID,
CoverID: album.ID,
CoverID: album.CoverID,
})
}
respond(w, r, sub)
@@ -85,7 +85,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
sub.Album = &subsonic.Album{
ID: album.ID,
Name: album.Title,
CoverID: album.ID,
CoverID: album.CoverID,
Created: album.CreatedAt,
Artist: album.AlbumArtist.Name,
}
@@ -103,7 +103,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
Album: album.Title,
AlbumID: album.ID,
ArtistID: album.AlbumArtist.ID, // album artist
CoverID: album.ID,
CoverID: album.CoverID,
Type: "music",
})
}
@@ -136,7 +136,7 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
ID: album.ID,
Name: album.Title,
Created: album.CreatedAt,
CoverID: album.ID,
CoverID: album.CoverID,
Artist: album.AlbumArtist.Name,
ArtistID: album.AlbumArtist.ID,
})

39
scanback Normal file
View File

@@ -0,0 +1,39 @@
func handleFolderForFolder(fullPath string, stat os.FileInfo) {
// update folder table for browsing by folder
modTime := stat.ModTime()
var folder db.Folder
err := tx.Where("path = ?", fullPath).First(&folder).Error
if !gorm.IsRecordNotFoundError(err) &&
modTime.Before(folder.UpdatedAt) {
// we found the record but it hasn't changed
currentDirStack.Push(&folder)
return
}
_, folderName := path.Split(fullPath)
folder.Path = fullPath
folder.ParentID = currentDirStack.PeekID()
folder.Name = folderName
tx.Save(&folder)
currentDirStack.Push(&folder)
}
func handleFolderForAlbum(fullPath string, stat os.FileInfo) {
// 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) {
// we found the record
// TODO: think about mod time here
return
}
currentAlbum = db.Album{Path: fullPath}
fmt.Println("SAVING", fullPath, err)
tx.Save(&currentAlbum)
}
func handleFolder(fullPath string, stat os.FileInfo) error {
handleFolderForAlbum(fullPath, stat)
handleFolderForFolder(fullPath, stat)
return nil
}