-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(subsonic): add internet radio support
* Initial commit of internet radio support. * Added first test for internet radio. * Refactor to prepare for more test cases. * Added a few more tests. Realized that I was not calling as admin so added ability to mock admin. * Added more internet radio tests. Added proper JSON unmarshaling for ID. * More test cases. Fixed some accidental tabs in files. * Fixed some more tabs. * lint fixes * Changed placeholder for homepage URL to fit into box. * Finished out internet radio test cases. Found a few bad error codes in internet radio AND podcasts (mea culpa). * Realized that delete via website was not checking properly if id existed. Fixed. gofmt
- Loading branch information
1 parent
2afc63f
commit 7ab378a
Showing
14 changed files
with
743 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ gonicembed | |
.vscode | ||
*.swp | ||
.tags* | ||
*.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package ctrlsubsonic | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"go.senan.xyz/gonic/db" | ||
"go.senan.xyz/gonic/server/ctrlsubsonic/params" | ||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec" | ||
) | ||
|
||
func (c *Controller) ServeGetInternetRadioStations(r *http.Request) *spec.Response { | ||
var stations []*db.InternetRadioStation | ||
if err := c.DB.Find(&stations).Error; err != nil { | ||
return spec.NewError(0, "find stations: %v", err) | ||
} | ||
sub := spec.NewResponse() | ||
sub.InternetRadioStations = &spec.InternetRadioStations{ | ||
List: make([]*spec.InternetRadioStation, len(stations)), | ||
} | ||
for i, station := range stations { | ||
sub.InternetRadioStations.List[i] = spec.NewInternetRadioStation(station) | ||
} | ||
return sub | ||
} | ||
|
||
func (c *Controller) ServeCreateInternetRadioStation(r *http.Request) *spec.Response { | ||
user := r.Context().Value(CtxUser).(*db.User) | ||
if !user.IsAdmin { | ||
return spec.NewError(50, "user not admin") | ||
} | ||
|
||
params := r.Context().Value(CtxParams).(params.Params) | ||
|
||
streamURL, err := params.Get("streamUrl") | ||
if err != nil { | ||
return spec.NewError(10, "no stream URL provided: %v", err) | ||
} | ||
if _, err := url.ParseRequestURI(streamURL); err != nil { | ||
return spec.NewError(70, "bad stream URL provided: %v", err) | ||
} | ||
name, err := params.Get("name") | ||
if err != nil { | ||
return spec.NewError(10, "no name provided: %v", err) | ||
} | ||
homepageURL, err := params.Get("homepageUrl") | ||
if err == nil { | ||
if _, err := url.ParseRequestURI(homepageURL); err != nil { | ||
return spec.NewError(70, "bad homepage URL provided: %v", err) | ||
} | ||
} | ||
|
||
var station db.InternetRadioStation | ||
station.StreamURL = streamURL | ||
station.Name = name | ||
station.HomepageURL = homepageURL | ||
|
||
if err := c.DB.Save(&station).Error; err != nil { | ||
return spec.NewError(0, "save station: %v", err) | ||
} | ||
|
||
return spec.NewResponse() | ||
} | ||
|
||
func (c *Controller) ServeUpdateInternetRadioStation(r *http.Request) *spec.Response { | ||
user := r.Context().Value(CtxUser).(*db.User) | ||
if !user.IsAdmin { | ||
return spec.NewError(50, "user not admin") | ||
} | ||
params := r.Context().Value(CtxParams).(params.Params) | ||
|
||
stationID, err := params.GetID("id") | ||
if err != nil { | ||
return spec.NewError(10, "no id provided: %v", err) | ||
} | ||
streamURL, err := params.Get("streamUrl") | ||
if err != nil { | ||
return spec.NewError(10, "no stream URL provided: %v", err) | ||
} | ||
if _, err = url.ParseRequestURI(streamURL); err != nil { | ||
return spec.NewError(70, "bad stream URL provided: %v", err) | ||
} | ||
name, err := params.Get("name") | ||
if err != nil { | ||
return spec.NewError(10, "no name provided: %v", err) | ||
} | ||
homepageURL, err := params.Get("homepageUrl") | ||
if err == nil { | ||
if _, err := url.ParseRequestURI(homepageURL); err != nil { | ||
return spec.NewError(70, "bad homepage URL provided: %v", err) | ||
} | ||
} | ||
|
||
var station db.InternetRadioStation | ||
if err := c.DB.Where("id=?", stationID.Value).First(&station).Error; err != nil { | ||
return spec.NewError(70, "id not found: %v", err) | ||
} | ||
|
||
station.StreamURL = streamURL | ||
station.Name = name | ||
station.HomepageURL = homepageURL | ||
|
||
if err := c.DB.Save(&station).Error; err != nil { | ||
return spec.NewError(0, "save station: %v", err) | ||
} | ||
return spec.NewResponse() | ||
} | ||
|
||
func (c *Controller) ServeDeleteInternetRadioStation(r *http.Request) *spec.Response { | ||
user := r.Context().Value(CtxUser).(*db.User) | ||
if !user.IsAdmin { | ||
return spec.NewError(50, "user not admin") | ||
} | ||
params := r.Context().Value(CtxParams).(params.Params) | ||
|
||
stationID, err := params.GetID("id") | ||
if err != nil { | ||
return spec.NewError(10, "no id provided: %v", err) | ||
} | ||
|
||
var station db.InternetRadioStation | ||
if err := c.DB.Where("id=?", stationID.Value).First(&station).Error; err != nil { | ||
return spec.NewError(70, "id not found: %v", err) | ||
} | ||
|
||
if err := c.DB.Delete(&station).Error; err != nil { | ||
return spec.NewError(70, "id not found: %v", err) | ||
} | ||
|
||
return spec.NewResponse() | ||
} |
Oops, something went wrong.