backend: Refactor playlits

This commit is contained in:
sentriz
2020-02-09 15:09:45 +00:00
parent 819af935cd
commit d9663e5b0a
8 changed files with 125 additions and 136 deletions

View File

@@ -22,13 +22,11 @@ func (c *Controller) ServeLogin(r *http.Request) *Response {
func (c *Controller) ServeHome(r *http.Request) *Response {
data := &templateData{}
//
// stats box
// ** begin stats box
c.DB.Table("artists").Count(&data.ArtistCount)
c.DB.Table("albums").Count(&data.AlbumCount)
c.DB.Table("tracks").Count(&data.TrackCount)
//
// lastfm box
// ** begin lastfm box
scheme := firstExisting(
"http", // fallback
r.Header.Get("X-Forwarded-Proto"),
@@ -42,11 +40,9 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
)
data.RequestRoot = fmt.Sprintf("%s://%s", scheme, host)
data.CurrentLastFMAPIKey = c.DB.GetSetting("lastfm_api_key")
//
// users box
// ** begin users box
c.DB.Find(&data.AllUsers)
//
// recent folders box
// ** begin recent folders box
c.DB.
Where("tag_artist_id IS NOT NULL").
Order("modified_at DESC").
@@ -57,17 +53,10 @@ func (c *Controller) ServeHome(r *http.Request) *Response {
i, _ := strconv.ParseInt(tStr, 10, 64)
data.LastScanTime = time.Unix(i, 0)
}
//
// playlists box
// ** begin playlists box
user := r.Context().Value(CtxUser).(*model.User)
c.DB.
Select("*, count(items.id) as track_count").
Joins(`
LEFT JOIN playlist_items items
ON items.playlist_id = playlists.id
`).
Where("user_id = ?", user.ID).
Group("playlists.id").
Limit(20).
Find(&data.Playlists)
//

View File

@@ -12,11 +12,11 @@ import (
"senan.xyz/g/gonic/model"
)
func playlistParseLine(c *Controller, playlistID int, path string) error {
func playlistParseLine(c *Controller, path string) (int, error) {
if strings.HasPrefix(path, "#") || strings.TrimSpace(path) == "" {
return nil
return 0, nil
}
track := &model.Track{}
var track model.Track
query := c.DB.Raw(`
SELECT tracks.id FROM TRACKS
JOIN albums ON tracks.album_id = albums.id
@@ -25,15 +25,12 @@ func playlistParseLine(c *Controller, playlistID int, path string) error {
err := query.First(&track).Error
switch {
case gorm.IsRecordNotFoundError(err):
return fmt.Errorf("couldn't match track %q", path)
return 0, fmt.Errorf("couldn't match track %q", path)
case err != nil:
return errors.Wrap(err, "while matching")
return 0, errors.Wrap(err, "while matching")
default:
return track.ID, nil
}
c.DB.Create(&model.PlaylistItem{
PlaylistID: playlistID,
TrackID: track.ID,
})
return nil
}
func playlistParseUpload(c *Controller, userID int, header *multipart.FileHeader) ([]string, bool) {
@@ -49,23 +46,28 @@ func playlistParseUpload(c *Controller, userID int, header *multipart.FileHeader
if !(contentType == "audio/x-mpegurl" || contentType == "application/octet-stream") {
return []string{fmt.Sprintf("invalid content-type %q", contentType)}, false
}
var trackIDs []int
var errors []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
trackID, err := playlistParseLine(c, scanner.Text())
if err != nil {
// trim length of error to not overflow cookie flash
errors = append(errors, fmt.Sprintf("%.100s", err.Error()))
}
if trackID != 0 {
trackIDs = append(trackIDs, trackID)
}
}
if err := scanner.Err(); err != nil {
return []string{fmt.Sprintf("iterating playlist file: %v", err)}, true
}
playlist := &model.Playlist{}
c.DB.FirstOrCreate(playlist, model.Playlist{
Name: playlistName,
UserID: userID,
})
c.DB.Delete(&model.PlaylistItem{}, "playlist_id = ?", playlist.ID)
var errors []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
path := scanner.Text()
if err := playlistParseLine(c, playlist.ID, path); err != nil {
// trim length of error to not overflow cookie flash
errors = append(errors, fmt.Sprintf("%.100s", err.Error()))
}
}
if err := scanner.Err(); err != nil {
return []string{fmt.Sprintf("scanning line of playlist: %v", err)}, true
}
playlist.SetItems(trackIDs)
c.DB.Save(playlist)
return errors, true
}