use rel paths
This commit is contained in:
@@ -3,9 +3,15 @@ package model
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// q: why in tarnation are all the foreign keys pointers to ints?
|
// q: why in tarnation are all the foreign keys pointers to ints?
|
||||||
//
|
|
||||||
// a: so they will be true sqlite null values instead of go zero
|
// a: so they will be true sqlite null values instead of go zero
|
||||||
// values when we save a row without that value
|
// values when we save a row without that value
|
||||||
|
//
|
||||||
|
// q: what in tarnation are the `IsNew`s for?
|
||||||
|
// a: it's a bit of a hack - but we set a models IsNew to true if
|
||||||
|
// we just filled it in for the first time, so when it comes
|
||||||
|
// time to insert them (post children callback) we can check for
|
||||||
|
// that bool being true - since it won't be true if it was already
|
||||||
|
// in the db
|
||||||
|
|
||||||
// Album represents the albums table
|
// Album represents the albums table
|
||||||
type Album struct {
|
type Album struct {
|
||||||
@@ -90,11 +96,11 @@ type Setting struct {
|
|||||||
type Play struct {
|
type Play struct {
|
||||||
IDBase
|
IDBase
|
||||||
User User
|
User User
|
||||||
UserID *int `gorm:"not null;index"`
|
UserID *int `gorm:"not null;index" sql:"type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||||
Album Album
|
Album Album
|
||||||
AlbumID *int `gorm:"not null;index"`
|
AlbumID *int `gorm:"not null;index" sql:"type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||||
Folder Folder
|
Folder Folder
|
||||||
FolderID *int `gorm:"not null;index"`
|
FolderID *int `gorm:"not null;index" sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||||
Time time.Time
|
Time time.Time
|
||||||
Count int
|
Count int
|
||||||
}
|
}
|
||||||
@@ -106,8 +112,8 @@ type Folder struct {
|
|||||||
Name string
|
Name string
|
||||||
Path string `gorm:"not null;unique_index"`
|
Path string `gorm:"not null;unique_index"`
|
||||||
Parent *Folder
|
Parent *Folder
|
||||||
ParentID *int
|
ParentID *int `sql:"type:int REFERENCES folders(id) ON DELETE CASCADE"`
|
||||||
CoverID *int
|
CoverID *int `sql:"type:int REFERENCES covers(id)"`
|
||||||
HasTracks bool `gorm:"not null;index"`
|
HasTracks bool `gorm:"not null;index"`
|
||||||
Cover Cover
|
Cover Cover
|
||||||
IsNew bool `gorm:"-"`
|
IsNew bool `gorm:"-"`
|
||||||
|
|||||||
@@ -14,11 +14,11 @@ package scanner
|
|||||||
// -> needs a FolderID
|
// -> needs a FolderID
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -33,24 +33,34 @@ var (
|
|||||||
IsScanning int32
|
IsScanning int32
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type trackItem struct {
|
||||||
|
mime string
|
||||||
|
ext string
|
||||||
|
}
|
||||||
|
|
||||||
|
type item struct {
|
||||||
|
path string
|
||||||
|
relPath string
|
||||||
|
stat os.FileInfo
|
||||||
|
track *trackItem
|
||||||
|
}
|
||||||
|
|
||||||
type Scanner struct {
|
type Scanner struct {
|
||||||
db *gorm.DB
|
db, tx *gorm.DB
|
||||||
tx *gorm.DB
|
musicPath string
|
||||||
musicPath string
|
seenTracks map[string]bool
|
||||||
seenPaths map[string]bool
|
curFolders folderStack
|
||||||
folderCount uint
|
curTracks []model.Track
|
||||||
curFolders folderStack
|
curCover model.Cover
|
||||||
curTracks []model.Track
|
curAlbum model.Album
|
||||||
curCover model.Cover
|
curAArtist model.AlbumArtist
|
||||||
curAlbum model.Album
|
|
||||||
curAArtist model.AlbumArtist
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db *gorm.DB, musicPath string) *Scanner {
|
func New(db *gorm.DB, musicPath string) *Scanner {
|
||||||
return &Scanner{
|
return &Scanner{
|
||||||
db: db,
|
db: db,
|
||||||
musicPath: musicPath,
|
musicPath: musicPath,
|
||||||
seenPaths: make(map[string]bool),
|
seenTracks: make(map[string]bool),
|
||||||
curFolders: make(folderStack, 0),
|
curFolders: make(folderStack, 0),
|
||||||
curTracks: make([]model.Track, 0),
|
curTracks: make([]model.Track, 0),
|
||||||
curCover: model.Cover{},
|
curCover: model.Cover{},
|
||||||
@@ -59,18 +69,18 @@ func New(db *gorm.DB, musicPath string) *Scanner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) handleCover(fullPath string, stat os.FileInfo) error {
|
func (s *Scanner) handleCover(it *item) error {
|
||||||
err := s.tx.
|
err := s.tx.
|
||||||
Where("path = ?", fullPath).
|
Where("path = ?", it.relPath).
|
||||||
First(&s.curCover).
|
First(&s.curCover).
|
||||||
Error
|
Error
|
||||||
if !gorm.IsRecordNotFoundError(err) &&
|
if !gorm.IsRecordNotFoundError(err) &&
|
||||||
stat.ModTime().Before(s.curCover.UpdatedAt) {
|
it.stat.ModTime().Before(s.curCover.UpdatedAt) {
|
||||||
// we found the record but it hasn't changed
|
// we found the record but it hasn't changed
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
s.curCover.Path = fullPath
|
s.curCover.Path = it.relPath
|
||||||
image, err := ioutil.ReadFile(fullPath)
|
image, err := ioutil.ReadFile(it.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "reading cover")
|
return errors.Wrap(err, "reading cover")
|
||||||
}
|
}
|
||||||
@@ -79,28 +89,94 @@ func (s *Scanner) handleCover(fullPath string, stat os.FileInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) handleFolder(fullPath string, stat os.FileInfo) error {
|
func (s *Scanner) handleFolder(it *item) error {
|
||||||
// TODO:
|
// TODO:
|
||||||
var folder model.Folder
|
var folder model.Folder
|
||||||
err := s.tx.
|
err := s.tx.
|
||||||
Where("path = ?", fullPath).
|
Where("path = ?", it.relPath).
|
||||||
First(&folder).
|
First(&folder).
|
||||||
Error
|
Error
|
||||||
if !gorm.IsRecordNotFoundError(err) &&
|
if !gorm.IsRecordNotFoundError(err) &&
|
||||||
stat.ModTime().Before(folder.UpdatedAt) {
|
it.stat.ModTime().Before(folder.UpdatedAt) {
|
||||||
// we found the record but it hasn't changed
|
// we found the record but it hasn't changed
|
||||||
s.curFolders.Push(folder)
|
s.curFolders.Push(folder)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
folder.Path = fullPath
|
folder.Path = it.relPath
|
||||||
folder.Name = stat.Name()
|
folder.Name = it.stat.Name()
|
||||||
s.tx.Save(&folder)
|
s.tx.Save(&folder)
|
||||||
folder.IsNew = true
|
folder.IsNew = true
|
||||||
s.curFolders.Push(folder)
|
s.curFolders.Push(folder)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) handleFolderCompletion(fullPath string, info *godirwalk.Dirent) error {
|
func (s *Scanner) handleTrack(it *item) error {
|
||||||
|
//
|
||||||
|
// set track basics
|
||||||
|
track := model.Track{}
|
||||||
|
err := s.tx.
|
||||||
|
Where("path = ?", it.relPath).
|
||||||
|
First(&track).
|
||||||
|
Error
|
||||||
|
if !gorm.IsRecordNotFoundError(err) &&
|
||||||
|
it.stat.ModTime().Before(track.UpdatedAt) {
|
||||||
|
// we found the record but it hasn't changed
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tags, err := readTags(it.path)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "reading tags")
|
||||||
|
}
|
||||||
|
trackNumber, totalTracks := tags.Track()
|
||||||
|
discNumber, totalDiscs := tags.Disc()
|
||||||
|
track.DiscNumber = discNumber
|
||||||
|
track.TotalDiscs = totalDiscs
|
||||||
|
track.TotalTracks = totalTracks
|
||||||
|
track.TrackNumber = trackNumber
|
||||||
|
track.Path = it.relPath
|
||||||
|
track.Suffix = it.track.ext
|
||||||
|
track.ContentType = it.track.mime
|
||||||
|
track.Size = int(it.stat.Size())
|
||||||
|
track.Title = tags.Title()
|
||||||
|
track.Artist = tags.Artist()
|
||||||
|
track.Year = tags.Year()
|
||||||
|
track.FolderID = s.curFolders.PeekID()
|
||||||
|
//
|
||||||
|
// set album artist basics
|
||||||
|
err = s.tx.Where("name = ?", tags.AlbumArtist()).
|
||||||
|
First(&s.curAArtist).
|
||||||
|
Error
|
||||||
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
|
s.curAArtist.Name = tags.AlbumArtist()
|
||||||
|
s.tx.Save(&s.curAArtist)
|
||||||
|
}
|
||||||
|
track.AlbumArtistID = s.curAArtist.ID
|
||||||
|
//
|
||||||
|
// set album if this is the first track in the folder
|
||||||
|
if len(s.curTracks) > 0 {
|
||||||
|
s.curTracks = append(s.curTracks, track)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.curTracks = append(s.curTracks, track)
|
||||||
|
//
|
||||||
|
directory, _ := path.Split(it.relPath)
|
||||||
|
err = s.tx.
|
||||||
|
Where("path = ?", directory).
|
||||||
|
First(&s.curAlbum).
|
||||||
|
Error
|
||||||
|
if !gorm.IsRecordNotFoundError(err) {
|
||||||
|
// we found the record
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.curAlbum.Path = directory
|
||||||
|
s.curAlbum.Title = tags.Album()
|
||||||
|
s.curAlbum.Year = tags.Year()
|
||||||
|
s.curAlbum.AlbumArtistID = s.curAArtist.ID
|
||||||
|
s.curAlbum.IsNew = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scanner) handleFolderCompletion(path string, info *godirwalk.Dirent) error {
|
||||||
// in general in this function - if a model is not nil, then it
|
// in general in this function - if a model is not nil, then it
|
||||||
// has at least been looked up. if it has a id of 0, then it is
|
// has at least been looked up. if it has a id of 0, then it is
|
||||||
// a new record and needs to be inserted
|
// a new record and needs to be inserted
|
||||||
@@ -129,91 +205,64 @@ func (s *Scanner) handleFolderCompletion(fullPath string, info *godirwalk.Dirent
|
|||||||
s.curAlbum = model.Album{}
|
s.curAlbum = model.Album{}
|
||||||
s.curAArtist = model.AlbumArtist{}
|
s.curAArtist = model.AlbumArtist{}
|
||||||
//
|
//
|
||||||
log.Printf("processed folder `%s`\n", fullPath)
|
log.Printf("processed folder `%s`\n", path)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scanner) handleTrack(fullPath string, stat os.FileInfo, mime, exten string) error {
|
func (s *Scanner) handleItem(path string, info *godirwalk.Dirent) error {
|
||||||
//
|
stat, err := os.Stat(path)
|
||||||
// set track basics
|
|
||||||
track := model.Track{}
|
|
||||||
modTime := stat.ModTime()
|
|
||||||
err := s.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 {
|
if err != nil {
|
||||||
return fmt.Errorf("when reading tags: %v", err)
|
return errors.Wrap(err, "stating")
|
||||||
}
|
}
|
||||||
trackNumber, totalTracks := tags.Track()
|
relPath, err := filepath.Rel(s.musicPath, path)
|
||||||
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 = s.curFolders.PeekID()
|
|
||||||
//
|
|
||||||
// set album artist basics
|
|
||||||
err = s.tx.Where("name = ?", tags.AlbumArtist()).
|
|
||||||
First(&s.curAArtist).
|
|
||||||
Error
|
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
|
||||||
s.curAArtist.Name = tags.AlbumArtist()
|
|
||||||
s.tx.Save(&s.curAArtist)
|
|
||||||
}
|
|
||||||
track.AlbumArtistID = s.curAArtist.ID
|
|
||||||
//
|
|
||||||
// set album if this is the first track in the folder
|
|
||||||
if len(s.curTracks) > 0 {
|
|
||||||
s.curTracks = append(s.curTracks, track)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
s.curTracks = append(s.curTracks, track)
|
|
||||||
//
|
|
||||||
directory, _ := path.Split(fullPath)
|
|
||||||
err = s.tx.
|
|
||||||
Where("path = ?", directory).
|
|
||||||
First(&s.curAlbum).
|
|
||||||
Error
|
|
||||||
if !gorm.IsRecordNotFoundError(err) {
|
|
||||||
// we found the record
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
s.curAlbum.Path = directory
|
|
||||||
s.curAlbum.Title = tags.Album()
|
|
||||||
s.curAlbum.Year = tags.Year()
|
|
||||||
s.curAlbum.AlbumArtistID = s.curAArtist.ID
|
|
||||||
s.curAlbum.IsNew = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Scanner) handleItem(fullPath string, info *godirwalk.Dirent) error {
|
|
||||||
s.seenPaths[fullPath] = true
|
|
||||||
stat, err := os.Stat(fullPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error stating: %v", err)
|
return errors.Wrap(err, "getting relative path")
|
||||||
|
}
|
||||||
|
it := &item{
|
||||||
|
path: path,
|
||||||
|
relPath: relPath,
|
||||||
|
stat: stat,
|
||||||
}
|
}
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
return s.handleFolder(fullPath, stat)
|
return s.handleFolder(it)
|
||||||
}
|
}
|
||||||
if isCover(fullPath) {
|
if isCover(path) {
|
||||||
return s.handleCover(fullPath, stat)
|
return s.handleCover(it)
|
||||||
}
|
}
|
||||||
if mime, exten, ok := isAudio(fullPath); ok {
|
if mime, ext, ok := isTrack(path); ok {
|
||||||
return s.handleTrack(fullPath, stat, mime, exten)
|
s.seenTracks[relPath] = true
|
||||||
|
it.track = &trackItem{mime: mime, ext: ext}
|
||||||
|
return s.handleTrack(it)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scanner) startScan() error {
|
||||||
|
defer logElapsed(time.Now(), "scanning")
|
||||||
|
err := godirwalk.Walk(s.musicPath, &godirwalk.Options{
|
||||||
|
Callback: s.handleItem,
|
||||||
|
PostChildrenCallback: s.handleFolderCompletion,
|
||||||
|
Unsorted: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "walking filesystem")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scanner) startClean() error {
|
||||||
|
defer logElapsed(time.Now(), "cleaning database")
|
||||||
|
var tracks []model.Track
|
||||||
|
s.tx.
|
||||||
|
Select("id, path").
|
||||||
|
Find(&tracks)
|
||||||
|
for _, track := range tracks {
|
||||||
|
_, ok := s.seenTracks[track.Path]
|
||||||
|
if ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.tx.Delete(&track)
|
||||||
|
log.Println("removed track", track.Path)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -247,46 +296,14 @@ func (s *Scanner) Start() error {
|
|||||||
}
|
}
|
||||||
atomic.StoreInt32(&IsScanning, 1)
|
atomic.StoreInt32(&IsScanning, 1)
|
||||||
defer atomic.StoreInt32(&IsScanning, 0)
|
defer atomic.StoreInt32(&IsScanning, 0)
|
||||||
defer logElapsed(time.Now(), "scanning")
|
|
||||||
s.db.Exec("PRAGMA foreign_keys = ON")
|
s.db.Exec("PRAGMA foreign_keys = ON")
|
||||||
s.tx = s.db.Begin()
|
s.tx = s.db.Begin()
|
||||||
defer s.tx.Commit()
|
defer s.tx.Commit()
|
||||||
//
|
if err := s.startScan(); err != nil {
|
||||||
// start scan logic
|
return errors.Wrap(err, "start scan")
|
||||||
err := godirwalk.Walk(s.musicPath, &godirwalk.Options{
|
}
|
||||||
Callback: s.handleItem,
|
if err := s.startClean(); err != nil {
|
||||||
PostChildrenCallback: s.handleFolderCompletion,
|
return errors.Wrap(err, "start clean")
|
||||||
Unsorted: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "walking filesystem")
|
|
||||||
}
|
}
|
||||||
////
|
|
||||||
//// start cleaning logic
|
|
||||||
//log.Println("cleaning database")
|
|
||||||
//var tracks []*model.Track
|
|
||||||
//s.tx.Select("id, path").Find(&tracks)
|
|
||||||
//for _, track := range tracks {
|
|
||||||
// _, ok := s.seenPaths[track.Path]
|
|
||||||
// if ok {
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// s.tx.Delete(&track)
|
|
||||||
// log.Println("removed", track.Path)
|
|
||||||
//}
|
|
||||||
//// delete albums without tracks
|
|
||||||
//s.tx.Exec(`
|
|
||||||
//DELETE FROM albums
|
|
||||||
//WHERE (SELECT count(id)
|
|
||||||
//FROM tracks
|
|
||||||
//WHERE album_id = albums.id) = 0;
|
|
||||||
//`)
|
|
||||||
//// delete artists without tracks
|
|
||||||
//s.tx.Exec(`
|
|
||||||
//DELETE FROM album_artists
|
|
||||||
//WHERE (SELECT count(id)
|
|
||||||
//FROM albums
|
|
||||||
//WHERE album_artist_id = album_artists.id) = 0;
|
|
||||||
//`)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/dhowden/tag"
|
"github.com/dhowden/tag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var audioExtensions = map[string]string{
|
var trackExtensions = map[string]string{
|
||||||
"mp3": "audio/mpeg",
|
"mp3": "audio/mpeg",
|
||||||
"flac": "audio/x-flac",
|
"flac": "audio/x-flac",
|
||||||
"aac": "audio/x-aac",
|
"aac": "audio/x-aac",
|
||||||
@@ -20,13 +20,13 @@ var audioExtensions = map[string]string{
|
|||||||
"ogg": "audio/ogg",
|
"ogg": "audio/ogg",
|
||||||
}
|
}
|
||||||
|
|
||||||
func isAudio(fullPath string) (string, string, bool) {
|
func isTrack(fullPath string) (string, string, bool) {
|
||||||
exten := filepath.Ext(fullPath)[1:]
|
ext := filepath.Ext(fullPath)[1:]
|
||||||
mine, ok := audioExtensions[exten]
|
mine, ok := trackExtensions[ext]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", "", false
|
return "", "", false
|
||||||
}
|
}
|
||||||
return mine, exten, true
|
return mine, ext, true
|
||||||
}
|
}
|
||||||
|
|
||||||
var coverFilenames = map[string]bool{
|
var coverFilenames = map[string]bool{
|
||||||
@@ -50,8 +50,8 @@ func isCover(fullPath string) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func readTags(fullPath string) (tag.Metadata, error) {
|
func readTags(path string) (tag.Metadata, error) {
|
||||||
trackData, err := os.Open(fullPath)
|
trackData, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("when tags from disk: %v", err)
|
return nil, fmt.Errorf("when tags from disk: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func (c *Controller) GetIndexes(w http.ResponseWriter, r *http.Request) {
|
|||||||
indexes = append(indexes, index)
|
indexes = append(indexes, index)
|
||||||
}
|
}
|
||||||
index.Artists = append(index.Artists, &subsonic.Artist{
|
index.Artists = append(index.Artists, &subsonic.Artist{
|
||||||
ID: folder.ID,
|
ID: *folder.ID,
|
||||||
Name: folder.Name,
|
Name: folder.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -58,11 +58,11 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
Find(&folders)
|
Find(&folders)
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
childrenObj = append(childrenObj, &subsonic.Child{
|
childrenObj = append(childrenObj, &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,
|
CoverID: *folder.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
@@ -80,14 +80,14 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
track.Suffix = "mp3"
|
track.Suffix = "mp3"
|
||||||
}
|
}
|
||||||
childrenObj = append(childrenObj, &subsonic.Child{
|
childrenObj = append(childrenObj, &subsonic.Child{
|
||||||
ID: track.ID,
|
ID: *track.ID,
|
||||||
Album: track.Album.Title,
|
Album: track.Album.Title,
|
||||||
Artist: track.Artist,
|
Artist: track.Artist,
|
||||||
ContentType: track.ContentType,
|
ContentType: track.ContentType,
|
||||||
CoverID: cFolder.CoverID,
|
CoverID: *cFolder.CoverID,
|
||||||
Duration: 0,
|
Duration: 0,
|
||||||
IsDir: false,
|
IsDir: false,
|
||||||
Parent: cFolder.ID,
|
Parent: *cFolder.ID,
|
||||||
Path: track.Path,
|
Path: track.Path,
|
||||||
Size: track.Size,
|
Size: track.Size,
|
||||||
Suffix: track.Suffix,
|
Suffix: track.Suffix,
|
||||||
@@ -100,8 +100,8 @@ func (c *Controller) GetMusicDirectory(w http.ResponseWriter, r *http.Request) {
|
|||||||
// respond section
|
// respond section
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Directory = &subsonic.Directory{
|
sub.Directory = &subsonic.Directory{
|
||||||
ID: cFolder.ID,
|
ID: *cFolder.ID,
|
||||||
Parent: cFolder.ParentID,
|
Parent: *cFolder.ParentID,
|
||||||
Name: cFolder.Name,
|
Name: cFolder.Name,
|
||||||
Children: childrenObj,
|
Children: childrenObj,
|
||||||
}
|
}
|
||||||
@@ -162,11 +162,11 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
|||||||
listObj := []*subsonic.Album{}
|
listObj := []*subsonic.Album{}
|
||||||
for _, folder := range folders {
|
for _, folder := range folders {
|
||||||
listObj = append(listObj, &subsonic.Album{
|
listObj = append(listObj, &subsonic.Album{
|
||||||
ID: folder.ID,
|
ID: *folder.ID,
|
||||||
Title: folder.Name,
|
Title: folder.Name,
|
||||||
Album: folder.Name,
|
Album: folder.Name,
|
||||||
CoverID: folder.CoverID,
|
CoverID: *folder.CoverID,
|
||||||
ParentID: folder.ParentID,
|
ParentID: *folder.ParentID,
|
||||||
IsDir: true,
|
IsDir: true,
|
||||||
Artist: folder.Parent.Name,
|
Artist: folder.Parent.Name,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func (c *Controller) GetArtists(w http.ResponseWriter, r *http.Request) {
|
|||||||
indexes.List = append(indexes.List, index)
|
indexes.List = append(indexes.List, index)
|
||||||
}
|
}
|
||||||
index.Artists = append(index.Artists, &subsonic.Artist{
|
index.Artists = append(index.Artists, &subsonic.Artist{
|
||||||
ID: artist.ID,
|
ID: *artist.ID,
|
||||||
Name: artist.Name,
|
Name: artist.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -49,17 +49,17 @@ func (c *Controller) GetArtist(w http.ResponseWriter, r *http.Request) {
|
|||||||
albumsObj := []*subsonic.Album{}
|
albumsObj := []*subsonic.Album{}
|
||||||
for _, album := range artist.Albums {
|
for _, album := range artist.Albums {
|
||||||
albumsObj = append(albumsObj, &subsonic.Album{
|
albumsObj = append(albumsObj, &subsonic.Album{
|
||||||
ID: album.ID,
|
ID: *album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: artist.Name,
|
Artist: artist.Name,
|
||||||
ArtistID: artist.ID,
|
ArtistID: *artist.ID,
|
||||||
CoverID: album.CoverID,
|
CoverID: *album.CoverID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Artist = &subsonic.Artist{
|
sub.Artist = &subsonic.Artist{
|
||||||
ID: artist.ID,
|
ID: *artist.ID,
|
||||||
Name: artist.Name,
|
Name: artist.Name,
|
||||||
Albums: albumsObj,
|
Albums: albumsObj,
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,7 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
tracksObj := []*subsonic.Track{}
|
tracksObj := []*subsonic.Track{}
|
||||||
for _, track := range album.Tracks {
|
for _, track := range album.Tracks {
|
||||||
tracksObj = append(tracksObj, &subsonic.Track{
|
tracksObj = append(tracksObj, &subsonic.Track{
|
||||||
ID: track.ID,
|
ID: *track.ID,
|
||||||
Title: track.Title,
|
Title: track.Title,
|
||||||
Artist: track.Artist, // track artist
|
Artist: track.Artist, // track artist
|
||||||
TrackNo: track.TrackNumber,
|
TrackNo: track.TrackNumber,
|
||||||
@@ -90,17 +90,17 @@ func (c *Controller) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
Created: track.CreatedAt,
|
Created: track.CreatedAt,
|
||||||
Size: track.Size,
|
Size: track.Size,
|
||||||
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.CoverID,
|
CoverID: *album.CoverID,
|
||||||
Type: "music",
|
Type: "music",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
sub.Album = &subsonic.Album{
|
sub.Album = &subsonic.Album{
|
||||||
ID: album.ID,
|
ID: *album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
CoverID: album.CoverID,
|
CoverID: *album.CoverID,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
Tracks: tracksObj,
|
Tracks: tracksObj,
|
||||||
@@ -164,12 +164,12 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
|
|||||||
listObj := []*subsonic.Album{}
|
listObj := []*subsonic.Album{}
|
||||||
for _, album := range albums {
|
for _, album := range albums {
|
||||||
listObj = append(listObj, &subsonic.Album{
|
listObj = append(listObj, &subsonic.Album{
|
||||||
ID: album.ID,
|
ID: *album.ID,
|
||||||
Name: album.Title,
|
Name: album.Title,
|
||||||
Created: album.CreatedAt,
|
Created: album.CreatedAt,
|
||||||
CoverID: album.CoverID,
|
CoverID: *album.CoverID,
|
||||||
Artist: album.AlbumArtist.Name,
|
Artist: album.AlbumArtist.Name,
|
||||||
ArtistID: album.AlbumArtist.ID,
|
ArtistID: *album.AlbumArtist.ID,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sub := subsonic.NewResponse()
|
sub := subsonic.NewResponse()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@@ -40,13 +41,14 @@ func (c *Controller) Stream(w http.ResponseWriter, r *http.Request) {
|
|||||||
respondError(w, r, 70, fmt.Sprintf("media with id `%d` was not found", id))
|
respondError(w, r, 70, fmt.Sprintf("media with id `%d` was not found", id))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
file, err := os.Open(track.Path)
|
absPath := path.Join(c.MusicPath, track.Path)
|
||||||
|
file, err := os.Open(absPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondError(w, r, 0, fmt.Sprintf("error while streaming media: %v", err))
|
respondError(w, r, 0, fmt.Sprintf("error while streaming media: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stat, _ := file.Stat()
|
stat, _ := file.Stat()
|
||||||
http.ServeContent(w, r, track.Path, stat.ModTime(), file)
|
http.ServeContent(w, r, absPath, stat.ModTime(), file)
|
||||||
//
|
//
|
||||||
// after we've served the file, mark the album as played
|
// after we've served the file, mark the album as played
|
||||||
user := r.Context().Value(contextUserKey).(*model.User)
|
user := r.Context().Value(contextUserKey).(*model.User)
|
||||||
@@ -111,7 +113,7 @@ func (c *Controller) Scrobble(w http.ResponseWriter, r *http.Request) {
|
|||||||
&track,
|
&track,
|
||||||
// clients will provide time in miliseconds, so use that or
|
// clients will provide time in miliseconds, so use that or
|
||||||
// instead convert UnixNano to miliseconds
|
// instead convert UnixNano to miliseconds
|
||||||
getIntParamOr(r, "time", int(time.Now().UnixNano() / 1e6)),
|
getIntParamOr(r, "time", int(time.Now().UnixNano()/1e6)),
|
||||||
getStrParamOr(r, "submission", "true") != "false",
|
getStrParamOr(r, "submission", "true") != "false",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
// take username from sesion and add the user row to the context
|
// take username from sesion and add the user row to the context
|
||||||
user := c.GetUserFromName(username)
|
user := c.GetUserFromName(username)
|
||||||
if user.ID == 0 {
|
if *user.ID == 0 {
|
||||||
// the username in the client's session no longer relates to a
|
// the username in the client's session no longer relates to a
|
||||||
// user in the database (maybe the user was deleted)
|
// user in the database (maybe the user was deleted)
|
||||||
session.Options.MaxAge = -1
|
session.Options.MaxAge = -1
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func (c *Controller) WithValidSubsonicArgs(next http.HandlerFunc) http.HandlerFu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.GetUserFromName(username)
|
user := c.GetUserFromName(username)
|
||||||
if user.ID == 0 {
|
if *user.ID == 0 {
|
||||||
// the user does not exist
|
// the user does not exist
|
||||||
respondError(w, r, 40, "invalid username")
|
respondError(w, r, 40, "invalid username")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user