2024-08-02 21:48:26 +00:00
|
|
|
package view
|
2024-03-18 10:34:43 +00:00
|
|
|
|
2024-03-18 20:19:04 +00:00
|
|
|
import (
|
2024-08-01 00:39:18 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"arimelody.me/arimelody.me/global"
|
2024-08-02 21:48:26 +00:00
|
|
|
"arimelody.me/arimelody.me/music/model"
|
2024-03-18 20:19:04 +00:00
|
|
|
)
|
|
|
|
|
2024-08-01 00:39:18 +00:00
|
|
|
// HTTP HANDLER METHODS
|
2024-03-18 20:19:04 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
func Handler() http.Handler {
|
|
|
|
mux := http.NewServeMux()
|
2024-08-01 00:39:18 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-01 02:54:15 +00:00
|
|
|
if r.URL.Path == "/" {
|
2024-08-02 21:48:26 +00:00
|
|
|
ServeCatalog().ServeHTTP(w, r)
|
2024-08-01 02:54:15 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
ServeGateway().ServeHTTP(w, r)
|
|
|
|
}))
|
2024-08-01 02:54:15 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
return mux
|
|
|
|
}
|
2024-08-01 00:39:18 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
func ServeCatalog() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-05 15:49:49 +00:00
|
|
|
releases := []model.Release{}
|
|
|
|
for _, r := range global.Releases {
|
|
|
|
if r.Visible {
|
|
|
|
release := *r
|
|
|
|
if !release.IsReleased() {
|
|
|
|
release.ReleaseType = model.Upcoming
|
|
|
|
}
|
|
|
|
releases = append(releases, release)
|
2024-08-02 21:48:26 +00:00
|
|
|
}
|
2024-08-01 00:39:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
global.ServeTemplate("music.html", releases).ServeHTTP(w, r)
|
2024-08-01 00:39:18 +00:00
|
|
|
})
|
2024-03-18 20:19:04 +00:00
|
|
|
}
|
2024-08-01 02:54:15 +00:00
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
/*
|
2024-08-01 02:54:15 +00:00
|
|
|
func ServeArtwork() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(r.URL.Path, ".png") {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseID := r.URL.Path[1:len(r.URL.Path) - 4]
|
|
|
|
var release = GetRelease(releaseID)
|
|
|
|
if release == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only allow authorised users to view unreleased releases
|
|
|
|
authorised := r.Context().Value("role") != nil && r.Context().Value("role") == "admin"
|
|
|
|
if !release.IsReleased() && !authorised {
|
|
|
|
admin.MustAuthorise(ServeArtwork()).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := filepath.Join("data", "music-artwork", releaseID + ".png")
|
|
|
|
info, err := os.Stat(fp)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length := info.Size()
|
|
|
|
|
|
|
|
file, err := os.Open(fp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var bytes = make([]byte, length)
|
|
|
|
file.Read(bytes)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "image/png")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(bytes)
|
|
|
|
})
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
*/
|