2024-08-02 21:48:26 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/admin"
|
|
|
|
"arimelody-web/global"
|
|
|
|
"arimelody-web/music/model"
|
|
|
|
db "arimelody-web/music/controller"
|
|
|
|
"arimelody-web/templates"
|
2024-08-05 00:23:17 +00:00
|
|
|
)
|
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
type (
|
|
|
|
Track struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Lyrics string `json:"lyrics"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Credit struct {
|
|
|
|
*model.Artist
|
|
|
|
Role string `json:"role"`
|
|
|
|
Primary bool `json:"primary"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Release struct {
|
2024-09-03 07:07:45 +00:00
|
|
|
*model.Release
|
2024-09-01 23:15:23 +00:00
|
|
|
Tracks []Track `json:"tracks"`
|
|
|
|
Credits []Credit `json:"credits"`
|
|
|
|
Links map[string]string `json:"links"`
|
|
|
|
}
|
|
|
|
)
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func ServeRelease(release *model.Release) http.Handler {
|
2024-08-02 21:48:26 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-05 15:49:49 +00:00
|
|
|
// only allow authorised users to view hidden releases
|
|
|
|
authorised := admin.GetSession(r) != nil
|
|
|
|
if !authorised && !release.Visible {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
response := Release{
|
|
|
|
Release: release,
|
|
|
|
Tracks: []Track{},
|
|
|
|
Credits: []Credit{},
|
|
|
|
Links: make(map[string]string),
|
2024-09-01 03:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if authorised || release.IsReleased() {
|
2024-09-01 23:15:23 +00:00
|
|
|
// get credits
|
|
|
|
credits, err := db.GetReleaseCredits(global.DB, release.ID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-09-01 23:15:23 +00:00
|
|
|
fmt.Printf("FATAL: Failed to serve release %s: Credits: %s\n", release.ID, err)
|
2024-09-01 03:43:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 23:15:23 +00:00
|
|
|
for _, credit := range credits {
|
|
|
|
artist, err := db.GetArtist(global.DB, credit.Artist.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to serve release %s: Artists: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Credits = append(response.Credits, Credit{
|
|
|
|
Artist: artist,
|
|
|
|
Role: credit.Role,
|
|
|
|
Primary: credit.Primary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// get tracks
|
|
|
|
tracks, err := db.GetReleaseTracks(global.DB, release.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to serve release %s: Tracks: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, track := range tracks {
|
|
|
|
response.Tracks = append(response.Tracks, Track{
|
|
|
|
Title: track.Title,
|
|
|
|
Description: track.Description,
|
|
|
|
Lyrics: track.Lyrics,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// get links
|
|
|
|
links, err := db.GetReleaseLinks(global.DB, release.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to serve release %s: Links: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, link := range links {
|
|
|
|
response.Links[link.Name] = link.URL
|
|
|
|
}
|
2024-08-05 15:49:49 +00:00
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2024-09-01 23:15:23 +00:00
|
|
|
err := json.NewEncoder(w).Encode(response)
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func ServeGateway(release *model.Release) http.Handler {
|
2024-08-02 21:48:26 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-05 15:49:49 +00:00
|
|
|
// only allow authorised users to view hidden releases
|
|
|
|
authorised := admin.GetSession(r) != nil
|
2024-09-01 03:43:32 +00:00
|
|
|
if !authorised && !release.Visible {
|
2024-08-05 15:49:49 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
response := *release
|
2024-08-03 22:24:15 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
if authorised || release.IsReleased() {
|
2024-09-03 07:07:45 +00:00
|
|
|
response.Tracks = release.Tracks
|
|
|
|
response.Credits = release.Credits
|
|
|
|
response.Links = release.Links
|
2024-09-01 03:43:32 +00:00
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
err := templates.Pages["music-gateway"].Execute(w, response)
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering music gateway for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-08-02 21:48:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|