add some podcast nit changes and make podcasts mandatory

This commit is contained in:
Alex McGrath
2021-01-11 11:50:44 +00:00
committed by Senan Kelly
parent 9c4286b0e2
commit 37fca3a087
16 changed files with 332 additions and 267 deletions

View File

@@ -13,8 +13,8 @@ import (
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/jukebox"
"go.senan.xyz/gonic/server/scrobble"
"go.senan.xyz/gonic/server/podcasts"
"go.senan.xyz/gonic/server/scrobble"
)
type CtxKey int

View File

@@ -38,11 +38,8 @@ func (c *Controller) ServePing(r *http.Request) *spec.Response {
func (c *Controller) ServeScrobble(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
if id.Type == specid.Podcast || id.Type == specid.PodcastEpisode {
return spec.NewError(10, "please provide a valid track id")
if err != nil || id.Type != specid.Track {
return spec.NewError(10, "please provide an valid `id` track parameter")
}
// fetch user to get lastfm session
user := r.Context().Value(CtxUser).(*db.User)
@@ -109,8 +106,8 @@ func (c *Controller) ServeGetUser(r *http.Request) *spec.Response {
Username: user.Name,
AdminRole: user.IsAdmin,
JukeboxRole: true,
PodcastRole: true,
ScrobblingEnabled: hasLastFM || hasListenBrainz,
PodcastRole: c.Podcasts.PodcastBasePath != "",
Folder: []int{1},
}
return sub

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/mmcdole/gofeed"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
@@ -12,21 +13,19 @@ import (
func (c *Controller) ServeGetPodcasts(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
isIncludeEpisodes := true
if ie, err := params.GetBool("includeEpisodes"); !ie && err == nil {
isIncludeEpisodes = false
isIncludeEpisodes := params.GetOrBool("includeEpisodes", true)
user := r.Context().Value(CtxUser).(*db.User)
id, _ := params.GetID("id")
podcasts, err := c.Podcasts.GetPodcastOrAll(user.ID, id.Value, isIncludeEpisodes)
if err != nil {
return spec.NewError(10, "failed get podcast(s): %s", err)
}
sub := spec.NewResponse()
user := r.Context().Value(CtxUser).(*db.User)
id, err := params.GetID("id")
if err != nil {
sub.Podcasts, err = c.Podcasts.GetAllPodcasts(user.ID, isIncludeEpisodes)
if err != nil {
return spec.NewError(10, "Failed to retrieve podcasts: %s", err)
}
return sub
sub.Podcasts = &spec.Podcasts{}
for _, podcast := range podcasts {
channel := spec.NewPodcastChannel(podcast)
sub.Podcasts.List = append(sub.Podcasts.List, channel)
}
sub.Podcasts, _ = c.Podcasts.GetPodcast(id.Value, user.ID, isIncludeEpisodes)
return sub
}
@@ -34,10 +33,10 @@ func (c *Controller) ServeDownloadPodcastEpisode(r *http.Request) *spec.Response
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.PodcastEpisode {
return spec.NewError(10, "Please provide a valid podcast episode id")
return spec.NewError(10, "please provide a valid podcast episode id")
}
if err := c.Podcasts.DownloadEpisode(id.Value); err != nil {
return spec.NewError(10, "Failed to download episode: %s", err)
return spec.NewError(10, "failed to download episode: %s", err)
}
return spec.NewResponse()
}
@@ -49,20 +48,18 @@ func (c *Controller) ServeCreatePodcastChannel(r *http.Request) *spec.Response {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(rssURL)
if err != nil {
return spec.NewError(10, "Failed to parse feed: %s", err)
return spec.NewError(10, "failed to parse feed: %s", err)
}
_, err = c.Podcasts.AddNewPodcast(feed, user.ID)
if err != nil {
return spec.NewError(10, "Failed to add feed: %s", err)
if _, err = c.Podcasts.AddNewPodcast(feed, user.ID); err != nil {
return spec.NewError(10, "failed to add feed: %s", err)
}
return spec.NewResponse()
}
func (c *Controller) ServeRefreshPodcasts(r *http.Request) *spec.Response {
user := r.Context().Value(CtxUser).(*db.User)
err := c.Podcasts.RefreshPodcasts(user.ID, false)
if err != nil {
return spec.NewError(10, "Failed to refresh feeds: %s", err)
if err := c.Podcasts.RefreshPodcastsForUser(user.ID); err != nil {
return spec.NewError(10, "failed to refresh feeds: %s", err)
}
return spec.NewResponse()
}
@@ -72,11 +69,10 @@ func (c *Controller) ServeDeletePodcastChannel(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.Podcast {
return spec.NewError(10, "Please provide a valid podcast ID")
return spec.NewError(10, "please provide a valid podcast id")
}
err = c.Podcasts.DeletePodcast(user.ID, id.Value)
if err != nil {
return spec.NewError(10, "Failed to delete podcast: %s", err)
if err := c.Podcasts.DeletePodcast(user.ID, id.Value); err != nil {
return spec.NewError(10, "failed to delete podcast: %s", err)
}
return spec.NewResponse()
}
@@ -85,11 +81,10 @@ func (c *Controller) ServeDeletePodcastEpisode(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetID("id")
if err != nil || id.Type != specid.PodcastEpisode {
return spec.NewError(10, "Please provide a valid podcast episode ID")
return spec.NewError(10, "please provide a valid podcast episode id")
}
err = c.Podcasts.DeletePodcastEpisode(id.Value)
if err != nil {
return spec.NewError(10, "Failed to delete podcast: %s", err)
if err := c.Podcasts.DeletePodcastEpisode(id.Value); err != nil {
return spec.NewError(10, "failed to delete podcast: %s", err)
}
return spec.NewResponse()
}

View File

@@ -11,7 +11,6 @@ import (
"time"
"github.com/disintegration/imaging"
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
@@ -75,50 +74,71 @@ var (
)
func coverGetPath(dbc *db.DB, musicPath, podcastPath string, id specid.ID) (string, error) {
var err error
coverPath := ""
switch id.Type {
case specid.Album:
folder := &db.Album{}
err = dbc.DB.
Select("id, left_path, right_path, cover").
First(folder, id.Value).
Error
coverPath = path.Join(
musicPath,
folder.LeftPath,
folder.RightPath,
folder.Cover,
)
if folder.Cover == "" {
return "", errCoverEmpty
}
return coverGetPathAlbum(dbc, musicPath, id.Value)
case specid.Podcast:
podcast := &db.Podcast{}
err = dbc.First(podcast, id.Value).Error
if podcast.ImagePath == "" {
return "", errCoverEmpty
}
coverPath = path.Join(podcastPath, podcast.ImagePath)
return coverGetPathPodcast(dbc, podcastPath, id.Value)
case specid.PodcastEpisode:
podcastEp := &db.PodcastEpisode{}
err = dbc.First(podcastEp, id.Value).Error
if gorm.IsRecordNotFoundError(err) {
return "", errCoverNotFound
}
podcast := &db.Podcast{}
err = dbc.First(podcast, podcastEp.PodcastID).Error
if podcast.ImagePath == "" {
return "", errCoverEmpty
}
coverPath = path.Join(podcastPath, podcast.ImagePath)
return coverGetPathPodcastEpisode(dbc, podcastPath, id.Value)
default:
}
if gorm.IsRecordNotFoundError(err) {
return "", errCoverNotFound
}
return coverPath, nil
}
func coverGetPathAlbum(dbc *db.DB, musicPath string, id int) (string, error) {
folder := &db.Album{}
err := dbc.DB.
Select("id, left_path, right_path, cover").
First(folder, id).
Error
if err != nil {
return "", fmt.Errorf("select album: %w", err)
}
if folder.Cover == "" {
return "", errCoverEmpty
}
return path.Join(
musicPath,
folder.LeftPath,
folder.RightPath,
folder.Cover,
), nil
}
func coverGetPathPodcast(dbc *db.DB, podcastPath string, id int) (string, error) {
podcast := &db.Podcast{}
err := dbc.
First(podcast, id).
Error
if err != nil {
return "", fmt.Errorf("select podcast: %w", err)
}
if podcast.ImagePath == "" {
return "", errCoverEmpty
}
return path.Join(podcastPath, podcast.ImagePath), nil
}
func coverGetPathPodcastEpisode(dbc *db.DB, podcastPath string, id int) (string, error) {
episode := &db.PodcastEpisode{}
err := dbc.
First(episode, id).
Error
if err != nil {
return "", fmt.Errorf("select episode: %w", err)
}
podcast := &db.Podcast{}
err = dbc.
First(podcast, episode.PodcastID).
Error
if err != nil {
return "", fmt.Errorf("select podcast: %w", err)
}
if podcast.ImagePath == "" {
return "", errCoverEmpty
}
return path.Join(podcastPath, podcast.ImagePath), nil
}
func coverScaleAndSave(absPath, cachePath string, size int) error {
@@ -176,19 +196,20 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
}
var audioFile db.AudioFile
var audioPath string
if id.Type == specid.Track {
switch id.Type {
case specid.Track:
track, _ := streamGetTrack(c.DB, id.Value)
audioFile = track
audioPath = path.Join(c.MusicPath, track.RelPath())
if err != nil {
return spec.NewError(70, "track with id `%s` was not found", id)
}
} else if id.Type == specid.PodcastEpisode {
case specid.PodcastEpisode:
podcast, err := streamGetPodcast(c.DB, id.Value)
audioFile = podcast
audioPath = path.Join(c.Podcasts.PodcastBasePath, podcast.Path)
if err != nil {
return spec.NewError(70, "track with id `%s` was not found", id)
return spec.NewError(70, "podcast with id `%s` was not found", id)
}
}
@@ -196,8 +217,8 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
return spec.NewError(70, "media with id `%d` was not found", id.Value)
}
user := r.Context().Value(CtxUser).(*db.User)
if id.Type == specid.Track {
defer streamUpdateStats(c.DB, user.ID, audioFile.(*db.Track).Album.ID)
if track, ok := audioFile.(*db.Track); ok {
defer streamUpdateStats(c.DB, user.ID, track.Album.ID)
}
pref := streamGetTransPref(c.DB, user.ID, params.GetOr("c", ""))
//
@@ -242,14 +263,15 @@ func (c *Controller) ServeDownload(w http.ResponseWriter, r *http.Request) *spec
}
var filePath string
var audioFile db.AudioFile
if id.Type == specid.Track {
switch id.Type {
case specid.Track:
track, _ := streamGetTrack(c.DB, id.Value)
audioFile = track
filePath = path.Join(c.MusicPath, track.RelPath())
if err != nil {
return spec.NewError(70, "track with id `%s` was not found", id)
}
} else if id.Type == specid.PodcastEpisode {
case specid.PodcastEpisode:
podcast, err := streamGetPodcast(c.DB, id.Value)
audioFile = podcast
filePath = path.Join(c.Podcasts.PodcastBasePath, podcast.Path)

View File

@@ -0,0 +1,45 @@
package spec
import "go.senan.xyz/gonic/server/db"
func NewPodcastChannel(p *db.Podcast) *PodcastChannel {
ret := &PodcastChannel{
ID: p.SID(),
OriginalImageURL: p.ImageURL,
Title: p.Title,
Description: p.Description,
URL: p.URL,
CoverArt: p.SID(),
Status: "skipped",
}
for _, episode := range p.Episodes {
specEpisode := NewPodcastEpisode(p, episode)
ret.Episode = append(ret.Episode, specEpisode)
}
return ret
}
func NewPodcastEpisode(p *db.Podcast, e *db.PodcastEpisode) *PodcastEpisode {
if e == nil {
return nil
}
return &PodcastEpisode{
ID: e.SID(),
StreamID: e.SID(),
ContentType: e.MIME(),
ChannelID: p.SID(),
Title: e.Title,
Description: e.Description,
Status: e.Status,
CoverArt: p.SID(),
PublishDate: *e.PublishDate,
Genre: "Podcast",
Duration: e.Length,
Year: e.PublishDate.Year(),
Suffix: e.Ext(),
BitRate: e.Bitrate,
IsDir: false,
Path: e.Path,
Size: e.Size,
}
}

View File

@@ -288,24 +288,24 @@ type JukeboxPlaylist struct {
}
type Podcasts struct {
List []PodcastChannel `xml:"channel" json:"channel"`
List []*PodcastChannel `xml:"channel" json:"channel"`
}
type PodcastChannel struct {
ID specid.ID `xml:"id,attr" json:"id"`
ID *specid.ID `xml:"id,attr" json:"id"`
URL string `xml:"url,attr" json:"url"`
Title string `xml:"title,attr" json:"title"`
Description string `xml:"description,attr" json:"description"`
CoverArt specid.ID `xml:"coverArt,attr" json:"coverArt,omitempty"`
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt,omitempty"`
OriginalImageURL string `xml:"originalImageUrl,attr" json:"originalImageUrl,omitempty"`
Status string `xml:"status,attr" json:"status"`
Episode []PodcastEpisode `xml:"episode" json:"episode,omitempty"`
Episode []*PodcastEpisode `xml:"episode" json:"episode,omitempty"`
}
type PodcastEpisode struct {
ID specid.ID `xml:"id,attr" json:"id"`
StreamID specid.ID `xml:"streamId,attr" json:"streamId"`
ChannelID specid.ID `xml:"channelId,attr" json:"channelId"`
ID *specid.ID `xml:"id,attr" json:"id"`
StreamID *specid.ID `xml:"streamId,attr" json:"streamId"`
ChannelID *specid.ID `xml:"channelId,attr" json:"channelId"`
Title string `xml:"title,attr" json:"title"`
Description string `xml:"description,attr" json:"description"`
PublishDate time.Time `xml:"publishDate,attr" json:"publishDate"`
@@ -314,7 +314,7 @@ type PodcastEpisode struct {
IsDir bool `xml:"isDir,attr" json:"isDir"`
Year int `xml:"year,attr" json:"year"`
Genre string `xml:"genre,attr" json:"genre"`
CoverArt specid.ID `xml:"coverArt,attr" json:"coverArt"`
CoverArt *specid.ID `xml:"coverArt,attr" json:"coverArt"`
Size int `xml:"size,attr" json:"size"`
ContentType string `xml:"contentType,attr" json:"contentType"`
Suffix string `xml:"suffix,attr" json:"suffix"`