dont insert if no need
This commit is contained in:
@@ -42,10 +42,10 @@ var (
|
|||||||
// currentCover because we find a cover anywhere among the tracks during the
|
// 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
|
// walk and need a reference to it when we update folder and album records
|
||||||
// when we exit a folder
|
// when we exit a folder
|
||||||
currentCover = db.Cover{}
|
currentCover = &db.Cover{}
|
||||||
// currentAlbum because we update this record when we exit a folder with
|
// currentAlbum because we update this record when we exit a folder with
|
||||||
// our new reference to it's cover
|
// our new reference to it's cover
|
||||||
currentAlbum = db.Album{}
|
currentAlbum = &db.Album{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func readTags(fullPath string) (tag.Metadata, error) {
|
func readTags(fullPath string) (tag.Metadata, error) {
|
||||||
@@ -68,23 +68,23 @@ func updateAlbum(fullPath string, albumArtistID int, title string) {
|
|||||||
directory, _ := path.Split(fullPath)
|
directory, _ := path.Split(fullPath)
|
||||||
// update album table (the currentAlbum record will be updated when
|
// update album table (the currentAlbum record will be updated when
|
||||||
// we exit this folder)
|
// we exit this folder)
|
||||||
err := tx.Where("path = ?", directory).First(¤tAlbum).Error
|
err := tx.Where("path = ?", directory).First(currentAlbum).Error
|
||||||
if !gorm.IsRecordNotFoundError(err) {
|
if !gorm.IsRecordNotFoundError(err) {
|
||||||
// we found the record
|
// we found the record
|
||||||
// TODO: think about mod time here
|
// TODO: think about mod time here
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
currentAlbum = db.Album{
|
currentAlbum = &db.Album{
|
||||||
Path: directory,
|
Path: directory,
|
||||||
AlbumArtistID: albumArtistID,
|
AlbumArtistID: albumArtistID,
|
||||||
Title: title,
|
Title: title,
|
||||||
}
|
}
|
||||||
tx.Save(¤tAlbum)
|
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(¤tCover).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
|
// we found the record but it hasn't changed
|
||||||
@@ -94,47 +94,47 @@ func handleCover(fullPath string, stat os.FileInfo) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("when reading cover: %v", err)
|
return fmt.Errorf("when reading cover: %v", err)
|
||||||
}
|
}
|
||||||
currentCover = db.Cover{
|
currentCover = &db.Cover{
|
||||||
Path: fullPath,
|
Path: fullPath,
|
||||||
Image: image,
|
Image: image,
|
||||||
|
NewlyInserted: true,
|
||||||
}
|
}
|
||||||
tx.Save(¤tCover)
|
tx.Save(currentCover)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFolder(fullPath string, stat os.FileInfo) error {
|
func handleFolder(fullPath string, stat os.FileInfo) error {
|
||||||
// update folder table for browsing by folder
|
// update folder table for browsing by folder
|
||||||
|
folder := &db.Folder{}
|
||||||
|
defer currentDirStack.Push(folder)
|
||||||
modTime := stat.ModTime()
|
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) &&
|
if !gorm.IsRecordNotFoundError(err) &&
|
||||||
modTime.Before(folder.UpdatedAt) {
|
modTime.Before(folder.UpdatedAt) {
|
||||||
// we found the record but it hasn't changed
|
// we found the record but it hasn't changed
|
||||||
currentDirStack.Push(&folder)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, folderName := path.Split(fullPath)
|
_, folderName := path.Split(fullPath)
|
||||||
folder.Path = fullPath
|
folder.Path = fullPath
|
||||||
folder.ParentID = currentDirStack.PeekID()
|
folder.ParentID = currentDirStack.PeekID()
|
||||||
folder.Name = folderName
|
folder.Name = folderName
|
||||||
tx.Save(&folder)
|
tx.Save(folder)
|
||||||
currentDirStack.Push(&folder)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
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
|
currentDir.CoverID = currentCover.ID
|
||||||
tx.Save(currentDir)
|
tx.Save(currentDir)
|
||||||
currentAlbum.CoverID = currentCover.ID
|
|
||||||
}
|
}
|
||||||
if currentAlbum.ID != 0 {
|
if currentAlbum.ID != 0 {
|
||||||
tx.Save(¤tAlbum)
|
currentAlbum.CoverID = currentCover.ID
|
||||||
|
tx.Save(currentAlbum)
|
||||||
}
|
}
|
||||||
currentCover = db.Cover{}
|
currentCover = &db.Cover{}
|
||||||
currentAlbum = db.Album{}
|
currentAlbum = &db.Album{}
|
||||||
currentDirStack.Pop()
|
|
||||||
log.Printf("processed folder `%s`\n", fullPath)
|
log.Printf("processed folder `%s`\n", fullPath)
|
||||||
return nil
|
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 {
|
func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
|
||||||
//
|
//
|
||||||
// set track basics
|
// set track basics
|
||||||
var track db.Track
|
track := &db.Track{}
|
||||||
modTime := stat.ModTime()
|
modTime := stat.ModTime()
|
||||||
err := tx.Where("path = ?", fullPath).First(&track).Error
|
err := tx.Where("path = ?", fullPath).First(track).Error
|
||||||
if !gorm.IsRecordNotFoundError(err) &&
|
if !gorm.IsRecordNotFoundError(err) &&
|
||||||
modTime.Before(track.UpdatedAt) {
|
modTime.Before(track.UpdatedAt) {
|
||||||
// we found the record but it hasn't changed
|
// 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()
|
track.FolderID = currentDirStack.PeekID()
|
||||||
//
|
//
|
||||||
// set album artist basics
|
// set album artist basics
|
||||||
var albumArtist db.AlbumArtist
|
albumArtist := &db.AlbumArtist{}
|
||||||
err = tx.Where("name = ?", tags.AlbumArtist()).
|
err = tx.Where("name = ?", tags.AlbumArtist()).
|
||||||
First(&albumArtist).
|
First(albumArtist).
|
||||||
Error
|
Error
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
albumArtist.Name = tags.AlbumArtist()
|
albumArtist.Name = tags.AlbumArtist()
|
||||||
tx.Save(&albumArtist)
|
tx.Save(albumArtist)
|
||||||
}
|
}
|
||||||
track.AlbumArtistID = albumArtist.ID
|
track.AlbumArtistID = albumArtist.ID
|
||||||
//
|
//
|
||||||
// set temporary album's basics - will be updated with
|
// set temporary album's basics - will be updated with
|
||||||
// cover after the tracks
|
// cover after the tracks inserted when we exit the folder
|
||||||
// inserted when we exit the folder
|
|
||||||
updateAlbum(fullPath, albumArtist.ID, tags.Album())
|
updateAlbum(fullPath, albumArtist.ID, tags.Album())
|
||||||
//
|
//
|
||||||
// update the track with our new album and finally save
|
// update the track with our new album and finally save
|
||||||
track.AlbumID = currentAlbum.ID
|
track.AlbumID = currentAlbum.ID
|
||||||
tx.Save(&track)
|
tx.Save(track)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ type Track struct {
|
|||||||
type Cover struct {
|
type Cover struct {
|
||||||
IDBase
|
IDBase
|
||||||
CrudBase
|
CrudBase
|
||||||
Image []byte
|
Image []byte
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null;unique_index"`
|
||||||
|
NewlyInserted bool `gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// User represents the users table
|
// User represents the users table
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/sentriz/gonic/db"
|
"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"
|
// for this, so we're going to return root directories as "artists"
|
||||||
var folders []*db.Folder
|
var folders []*db.Folder
|
||||||
c.DB.Where("parent_id = ?", 1).Find(&folders)
|
c.DB.Where("parent_id = ?", 1).Find(&folders)
|
||||||
|
fmt.Println(folders, "++++++++")
|
||||||
var indexMap = make(map[rune]*subsonic.Index)
|
var indexMap = make(map[rune]*subsonic.Index)
|
||||||
var indexes []*subsonic.Index
|
var indexes []*subsonic.Index
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
@@ -60,10 +62,11 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
sub.Directory.Children = append(sub.Directory.Children, &subsonic.Child{
|
sub.Directory.Children = append(sub.Directory.Children, &subsonic.Child{
|
||||||
Parent: cFolder.ID,
|
Parent: cFolder.ID,
|
||||||
ID: folder.ID,
|
ID: folder.ID,
|
||||||
Title: folder.Name,
|
Title: folder.Name,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
|
CoverID: folder.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
respond(w, r, sub)
|
respond(w, r, sub)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func (c *Controller) GetArtist(w http.ResponseWriter, r *http.Request) {
|
|||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: artist.Name,
|
Artist: artist.Name,
|
||||||
ArtistID: artist.ID,
|
ArtistID: artist.ID,
|
||||||
CoverID: album.ID,
|
CoverID: album.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
respond(w, r, sub)
|
respond(w, r, sub)
|
||||||
@@ -85,7 +85,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
sub.Album = &subsonic.Album{
|
sub.Album = &subsonic.Album{
|
||||||
ID: album.ID,
|
ID: album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
CoverID: album.ID,
|
CoverID: album.CoverID,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
Album: album.Title,
|
Album: album.Title,
|
||||||
AlbumID: album.ID,
|
AlbumID: album.ID,
|
||||||
ArtistID: album.AlbumArtist.ID, // album artist
|
ArtistID: album.AlbumArtist.ID, // album artist
|
||||||
CoverID: album.ID,
|
CoverID: album.CoverID,
|
||||||
Type: "music",
|
Type: "music",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -136,7 +136,7 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
|||||||
ID: album.ID,
|
ID: album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
CoverID: album.ID,
|
CoverID: album.CoverID,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
ArtistID: album.AlbumArtist.ID,
|
ArtistID: album.AlbumArtist.ID,
|
||||||
})
|
})
|
||||||
|
|||||||
39
scanback
Normal file
39
scanback
Normal 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(¤tAlbum).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(¤tAlbum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleFolder(fullPath string, stat os.FileInfo) error {
|
||||||
|
handleFolderForAlbum(fullPath, stat)
|
||||||
|
handleFolderForFolder(fullPath, stat)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user