2024-08-02 21:48:26 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-08-03 23:23:55 +00:00
|
|
|
"html/template"
|
2024-08-02 21:48:26 +00:00
|
|
|
"net/http"
|
2024-08-03 23:23:55 +00:00
|
|
|
"strings"
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
"arimelody.me/arimelody.me/admin"
|
|
|
|
"arimelody.me/arimelody.me/global"
|
2024-08-03 22:24:15 +00:00
|
|
|
"arimelody.me/arimelody.me/music/model"
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
type (
|
|
|
|
gatewayTrack struct {
|
|
|
|
*model.Track
|
|
|
|
Lyrics template.HTML
|
|
|
|
Number int
|
|
|
|
}
|
|
|
|
|
|
|
|
gatewayRelease struct {
|
|
|
|
*model.Release
|
2024-08-05 15:49:49 +00:00
|
|
|
Tracks []gatewayTrack
|
|
|
|
Authorised bool
|
2024-08-05 00:23:17 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
// HTTP HANDLERS
|
|
|
|
|
|
|
|
func ServeRelease() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseID := r.URL.Path[1:]
|
2024-08-05 15:49:49 +00:00
|
|
|
var releaseRef = global.GetRelease(releaseID)
|
|
|
|
if releaseRef == nil {
|
2024-08-02 21:48:26 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-08-05 15:49:49 +00:00
|
|
|
var release = *releaseRef
|
2024-08-02 21:48:26 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if !authorised && !release.IsReleased() {
|
|
|
|
release.Tracks = nil
|
|
|
|
release.Credits = nil
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
err := json.NewEncoder(w).Encode(release)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServeGateway() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.Redirect(w, r, "/music", http.StatusPermanentRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := r.URL.Path[1:]
|
|
|
|
release := global.GetRelease(id)
|
|
|
|
if release == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-05 15:49:49 +00:00
|
|
|
// only allow authorised users to view hidden releases
|
|
|
|
authorised := admin.GetSession(r) != nil
|
|
|
|
if !release.Visible && !authorised {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
tracks := []gatewayTrack{}
|
2024-08-03 22:24:15 +00:00
|
|
|
for i, track := range release.Tracks {
|
2024-08-31 00:30:30 +00:00
|
|
|
tracks = append(tracks, gatewayTrack{
|
2024-08-03 22:24:15 +00:00
|
|
|
Track: track,
|
2024-08-03 23:23:55 +00:00
|
|
|
Lyrics: template.HTML(strings.Replace(track.Lyrics, "\n", "<br>", -1)),
|
2024-08-31 00:30:30 +00:00
|
|
|
Number: i + 1,
|
|
|
|
})
|
2024-08-03 22:24:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
lrw := global.LoggingResponseWriter{ResponseWriter: w, Code: http.StatusOK}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-08-05 15:49:49 +00:00
|
|
|
global.ServeTemplate("music-gateway.html", gatewayRelease{release, tracks, authorised}).ServeHTTP(&lrw, r)
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
if lrw.Code != http.StatusOK {
|
|
|
|
fmt.Printf("Error rendering music gateway for %s\n", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|