From bd95806a236ce25795664d597c1871c39c9b88f1 Mon Sep 17 00:00:00 2001 From: sentriz Date: Mon, 13 May 2019 23:28:53 +0100 Subject: [PATCH] dont insert if no need --- cmd/scanner/main.go | 59 ++++++++++++++++---------------- db/model.go | 5 +-- handler/handler_sub_by_folder.go | 11 +++--- handler/handler_sub_by_tags.go | 8 ++--- scanback | 39 +++++++++++++++++++++ 5 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 scanback diff --git a/cmd/scanner/main.go b/cmd/scanner/main.go index d0f109d..8a98736 100644 --- a/cmd/scanner/main.go +++ b/cmd/scanner/main.go @@ -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(¤tAlbum).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(¤tAlbum) + tx.Save(currentAlbum) } func handleCover(fullPath string, stat os.FileInfo) error { modTime := stat.ModTime() - err := tx.Where("path = ?", fullPath).First(¤tCover).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(¤tCover) + 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(¤tAlbum) + 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 } diff --git a/db/model.go b/db/model.go index 6ca26d8..30d83fc 100644 --- a/db/model.go +++ b/db/model.go @@ -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 diff --git a/handler/handler_sub_by_folder.go b/handler/handler_sub_by_folder.go index 5ae7197..b91dbe4 100644 --- a/handler/handler_sub_by_folder.go +++ b/handler/handler_sub_by_folder.go @@ -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) diff --git a/handler/handler_sub_by_tags.go b/handler/handler_sub_by_tags.go index ad1dc4b..6f82a5c 100644 --- a/handler/handler_sub_by_tags.go +++ b/handler/handler_sub_by_tags.go @@ -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, }) diff --git a/scanback b/scanback new file mode 100644 index 0000000..3a18582 --- /dev/null +++ b/scanback @@ -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 +}