This commit is contained in:
sentriz
2019-05-19 23:28:05 +01:00
parent 5c657d9630
commit ad571ed7ab
45 changed files with 787 additions and 492 deletions

View File

@@ -1,245 +1,46 @@
// this scanner tries to scan with a single unsorted walk of the music
// directory - which means you can come across the cover of an album/folder
// before the tracks (and therefore the album) which is an issue because
// when inserting into the album table, we need a reference to the cover.
// to solve this we're using godirwalk's PostChildrenCallback and some globals
//
// Album -> needs a CoverID
// -> needs a FolderID (American Football)
// Folder -> needs a CoverID
// -> needs a ParentID
// Track -> needs an AlbumID
// -> needs a FolderID
package main
import (
"fmt"
"io/ioutil"
"flag"
"log"
"os"
"path"
"time"
"github.com/dhowden/tag"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/karrick/godirwalk"
"github.com/peterbourgon/ff"
"github.com/sentriz/gonic/db"
"github.com/sentriz/gonic/scanner"
)
var (
orm *gorm.DB
tx *gorm.DB
// seenPaths is used to keep every path we've seen so that
// we can remove old tracks, folders, and covers by path when we
// are in the cleanDatabase stage
seenPaths = make(map[string]bool)
// currentDirStack is used for inserting to the folders (subsonic browse
// by folder) which helps us work out a folder's parent
currentDirStack = make(dirStack, 0)
// 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{}
// currentAlbum because we update this record when we exit a folder with
// our new reference to it's cover
currentAlbum = &db.Album{}
const (
programName = "gonic"
programVar = "GONIC"
)
func readTags(fullPath string) (tag.Metadata, error) {
trackData, err := os.Open(fullPath)
if err != nil {
return nil, fmt.Errorf("when tags from disk: %v", err)
}
defer trackData.Close()
tags, err := tag.ReadFrom(trackData)
if err != nil {
return nil, err
}
return tags, nil
}
func updateAlbum(fullPath string, album *db.Album) {
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,
Title: album.Title,
AlbumArtistID: album.AlbumArtistID,
Year: album.Year,
}
tx.Save(currentAlbum)
}
func handleCover(fullPath string, stat os.FileInfo) error {
modTime := stat.ModTime()
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
return nil
}
image, err := ioutil.ReadFile(fullPath)
if err != nil {
return fmt.Errorf("when reading cover: %v", err)
}
currentCover = &db.Cover{
Path: fullPath,
Image: image,
NewlyInserted: true,
}
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()
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
return nil
}
_, folderName := path.Split(fullPath)
folder.Path = fullPath
folder.ParentID = currentDirStack.PeekID()
folder.Name = folderName
tx.Save(folder)
return nil
}
func handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
currentDir := currentDirStack.Peek()
defer currentDirStack.Pop()
var dirShouldSave bool
if currentAlbum.ID != 0 {
currentAlbum.CoverID = currentCover.ID
tx.Save(currentAlbum)
currentDir.HasTracks = true
dirShouldSave = true
}
if currentCover.NewlyInserted {
currentDir.CoverID = currentCover.ID
dirShouldSave = true
}
if dirShouldSave {
tx.Save(currentDir)
}
currentCover = &db.Cover{}
currentAlbum = &db.Album{}
log.Printf("processed folder `%s`\n", fullPath)
return nil
}
func handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
//
// set track basics
track := &db.Track{}
modTime := stat.ModTime()
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
return nil
}
tags, err := readTags(fullPath)
if err != nil {
return fmt.Errorf("when reading tags: %v", err)
}
trackNumber, totalTracks := tags.Track()
discNumber, totalDiscs := tags.Disc()
track.Path = fullPath
track.Title = tags.Title()
track.Artist = tags.Artist()
track.DiscNumber = discNumber
track.TotalDiscs = totalDiscs
track.TotalTracks = totalTracks
track.TrackNumber = trackNumber
track.Year = tags.Year()
track.Suffix = exten
track.ContentType = mime
track.Size = int(stat.Size())
track.FolderID = currentDirStack.PeekID()
//
// set album artist basics
albumArtist := &db.AlbumArtist{}
err = tx.Where("name = ?", tags.AlbumArtist()).
First(albumArtist).
Error
if gorm.IsRecordNotFoundError(err) {
albumArtist.Name = tags.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
updateAlbum(fullPath, &db.Album{
AlbumArtistID: albumArtist.ID,
Title: tags.Album(),
Year: tags.Year(),
})
//
// update the track with our new album and finally save
track.AlbumID = currentAlbum.ID
tx.Save(track)
return nil
}
func handleItem(fullPath string, info *godirwalk.Dirent) error {
// seenPaths = append(seenPaths, fullPath)
seenPaths[fullPath] = true
stat, err := os.Stat(fullPath)
if err != nil {
return fmt.Errorf("error stating: %v", err)
}
if info.IsDir() {
return handleFolder(fullPath, stat)
}
if isCover(fullPath) {
return handleCover(fullPath, stat)
}
if mime, exten, ok := isAudio(fullPath); ok {
return handleTrack(fullPath, stat, mime, exten)
}
return nil
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("usage: %s <path to music>", os.Args[0])
}
orm = db.New()
orm.SetLogger(log.New(os.Stdout, "gorm ", 0))
tx = orm.Begin()
createDatabase()
startTime := time.Now()
err := godirwalk.Walk(os.Args[1], &godirwalk.Options{
Callback: handleItem,
PostChildrenCallback: handleFolderCompletion,
Unsorted: true,
})
set := flag.NewFlagSet(programName, flag.ExitOnError)
musicPath := set.String(
"music-path", "",
"path to music")
dbPath := set.String(
"db-path", "gonic.db",
"path to database (optional)")
err := ff.Parse(set, os.Args[1:])
if err != nil {
log.Fatalf("error when walking: %v\n", err)
log.Fatalf("error parsing args: %v\n", err)
}
log.Printf("scanned in %s\n", time.Since(startTime))
startTime = time.Now()
cleanDatabase()
log.Printf("cleaned in %s\n", time.Since(startTime))
tx.Commit()
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
log.Fatal("please provide a valid music directory")
}
db, err := gorm.Open("sqlite3", *dbPath)
if err != nil {
log.Fatalf("error opening database: %v\n", err)
}
db.SetLogger(log.New(os.Stdout, "gorm ", 0))
s := scanner.New(
db,
*musicPath,
)
s.MigrateDB()
s.Start()
}

View File

@@ -1,162 +1,59 @@
package main
import (
"html/template"
"flag"
"log"
"net/http"
"time"
"os"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/securecookie"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/wader/gormstore"
"github.com/peterbourgon/ff"
"github.com/sentriz/gonic/db"
"github.com/sentriz/gonic/handler"
"github.com/sentriz/gonic/server"
)
type middleware func(next http.HandlerFunc) http.HandlerFunc
func newChain(wares ...middleware) middleware {
return func(final http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
last := final
for i := len(wares) - 1; i >= 0; i-- {
last = wares[i](last)
}
last(w, r)
}
}
}
func extendFromBox(tmpl *template.Template, box *packr.Box, key string) *template.Template {
strT, err := box.FindString(key)
if err != nil {
log.Fatalf("error when reading template from box: %v", err)
}
if tmpl == nil {
tmpl = template.New("layout")
} else {
tmpl = template.Must(tmpl.Clone())
}
newT, err := tmpl.Parse(strT)
if err != nil {
log.Fatalf("error when parsing template template: %v", err)
}
return newT
}
func setSubsonicRoutes(cont handler.Controller, mux *http.ServeMux) {
withWare := newChain(
cont.WithLogging,
cont.WithCORS,
cont.WithValidSubsonicArgs,
)
// common
mux.HandleFunc("/rest/download", withWare(cont.Stream))
mux.HandleFunc("/rest/download.view", withWare(cont.Stream))
mux.HandleFunc("/rest/stream", withWare(cont.Stream))
mux.HandleFunc("/rest/stream.view", withWare(cont.Stream))
mux.HandleFunc("/rest/getCoverArt", withWare(cont.GetCoverArt))
mux.HandleFunc("/rest/getCoverArt.view", withWare(cont.GetCoverArt))
mux.HandleFunc("/rest/getLicense", withWare(cont.GetLicence))
mux.HandleFunc("/rest/getLicense.view", withWare(cont.GetLicence))
mux.HandleFunc("/rest/ping", withWare(cont.Ping))
mux.HandleFunc("/rest/ping.view", withWare(cont.Ping))
mux.HandleFunc("/rest/scrobble", withWare(cont.Scrobble))
mux.HandleFunc("/rest/scrobble.view", withWare(cont.Scrobble))
mux.HandleFunc("/rest/getMusicFolders", withWare(cont.GetMusicFolders))
mux.HandleFunc("/rest/getMusicFolders.view", withWare(cont.GetMusicFolders))
// browse by tag
mux.HandleFunc("/rest/getAlbum", withWare(cont.GetAlbum))
mux.HandleFunc("/rest/getAlbum.view", withWare(cont.GetAlbum))
mux.HandleFunc("/rest/getAlbumList2", withWare(cont.GetAlbumListTwo))
mux.HandleFunc("/rest/getAlbumList2.view", withWare(cont.GetAlbumListTwo))
mux.HandleFunc("/rest/getArtist", withWare(cont.GetArtist))
mux.HandleFunc("/rest/getArtist.view", withWare(cont.GetArtist))
mux.HandleFunc("/rest/getArtists", withWare(cont.GetArtists))
mux.HandleFunc("/rest/getArtists.view", withWare(cont.GetArtists))
// browse by folder
mux.HandleFunc("/rest/getIndexes", withWare(cont.GetIndexes))
mux.HandleFunc("/rest/getIndexes.view", withWare(cont.GetIndexes))
mux.HandleFunc("/rest/getMusicDirectory", withWare(cont.GetMusicDirectory))
mux.HandleFunc("/rest/getMusicDirectory.view", withWare(cont.GetMusicDirectory))
mux.HandleFunc("/rest/getAlbumList", withWare(cont.GetAlbumList))
mux.HandleFunc("/rest/getAlbumList.view", withWare(cont.GetAlbumList))
}
func setAdminRoutes(cont handler.Controller, mux *http.ServeMux) {
sessionKey := []byte(cont.GetSetting("session_key"))
if len(sessionKey) == 0 {
sessionKey = securecookie.GenerateRandomKey(32)
cont.SetSetting("session_key", string(sessionKey))
}
// create gormstore (and cleanup) for backend sessions
cont.SStore = gormstore.New(cont.DB, []byte(sessionKey))
go cont.SStore.PeriodicCleanup(1*time.Hour, nil)
// using packr to bundle templates and static files
box := packr.New("templates", "../../templates")
layoutT := extendFromBox(nil, box, "layout.tmpl")
userT := extendFromBox(layoutT, box, "user.tmpl")
cont.Templates = map[string]*template.Template{
"login": extendFromBox(layoutT, box, "pages/login.tmpl"),
"home": extendFromBox(userT, box, "pages/home.tmpl"),
"change_own_password": extendFromBox(userT, box, "pages/change_own_password.tmpl"),
"change_password": extendFromBox(userT, box, "pages/change_password.tmpl"),
"delete_user": extendFromBox(userT, box, "pages/delete_user.tmpl"),
"create_user": extendFromBox(userT, box, "pages/create_user.tmpl"),
"update_lastfm_api_key": extendFromBox(userT, box, "pages/update_lastfm_api_key.tmpl"),
}
withPublicWare := newChain(
cont.WithLogging,
cont.WithSession,
)
withUserWare := newChain(
withPublicWare,
cont.WithUserSession,
)
withAdminWare := newChain(
withUserWare,
cont.WithAdminSession,
)
server := http.FileServer(packr.New("static", "../../static"))
mux.Handle("/admin/static/", http.StripPrefix("/admin/static/", server))
mux.HandleFunc("/admin/login", withPublicWare(cont.ServeLogin))
mux.HandleFunc("/admin/login_do", withPublicWare(cont.ServeLoginDo))
mux.HandleFunc("/admin/logout", withUserWare(cont.ServeLogout))
mux.HandleFunc("/admin/home", withUserWare(cont.ServeHome))
mux.HandleFunc("/admin/change_own_password", withUserWare(cont.ServeChangeOwnPassword))
mux.HandleFunc("/admin/change_own_password_do", withUserWare(cont.ServeChangeOwnPasswordDo))
mux.HandleFunc("/admin/link_lastfm_do", withUserWare(cont.ServeLinkLastFMDo))
mux.HandleFunc("/admin/unlink_lastfm_do", withUserWare(cont.ServeUnlinkLastFMDo))
mux.HandleFunc("/admin/change_password", withAdminWare(cont.ServeChangePassword))
mux.HandleFunc("/admin/change_password_do", withAdminWare(cont.ServeChangePasswordDo))
mux.HandleFunc("/admin/delete_user", withAdminWare(cont.ServeDeleteUser))
mux.HandleFunc("/admin/delete_user_do", withAdminWare(cont.ServeDeleteUserDo))
mux.HandleFunc("/admin/create_user", withAdminWare(cont.ServeCreateUser))
mux.HandleFunc("/admin/create_user_do", withAdminWare(cont.ServeCreateUserDo))
mux.HandleFunc("/admin/update_lastfm_api_key", withAdminWare(cont.ServeUpdateLastFMAPIKey))
mux.HandleFunc("/admin/update_lastfm_api_key_do", withAdminWare(cont.ServeUpdateLastFMAPIKeyDo))
}
const (
programName = "gonic"
programVar = "GONIC"
)
func main() {
address := "0.0.0.0:6969"
mux := http.NewServeMux()
// create a new controller and pass a copy to both routes.
// they will add more fields to their copy if they need them
baseController := handler.Controller{DB: db.New()}
setSubsonicRoutes(baseController, mux)
setAdminRoutes(baseController, mux)
server := &http.Server{
Addr: address,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
log.Printf("starting server at `%s`\n", address)
err := server.ListenAndServe()
set := flag.NewFlagSet(programName, flag.ExitOnError)
listenAddr := set.String(
"listen-addr", "0.0.0.0:6969",
"listen address (optional)")
musicPath := set.String(
"music-path", "",
"path to music")
dbPath := set.String(
"db-path", "gonic.db",
"path to database (optional)")
_ = set.String(
"config-path", "",
"path to config (optional)")
err := ff.Parse(set, os.Args[1:],
ff.WithConfigFileFlag("config-path"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarPrefix(programVar),
)
if err != nil {
log.Printf("when starting server: %v\n", err)
log.Fatalf("error parsing args: %v\n", err)
}
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
log.Fatal("please provide a valid music directory")
}
db, err := gorm.Open("sqlite3", *dbPath)
if err != nil {
log.Fatalf("error opening database: %v\n", err)
}
s := server.New(
db,
*musicPath,
*listenAddr,
)
log.Printf("starting server at %s", *listenAddr)
err = s.ListenAndServe()
if err != nil {
log.Fatalf("error starting server: %v\n", err)
}
}