MORE REFACTORING!! + some improvements
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
151b2d8fd9
commit
cba791deba
17 changed files with 376 additions and 223 deletions
113
music/music.go
Normal file
113
music/music.go
Normal file
|
@ -0,0 +1,113 @@
|
|||
package music
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"arimelody.me/arimelody.me/admin"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
)
|
||||
|
||||
// HTTP HANDLER METHODS
|
||||
|
||||
func ServeCatalog() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
releases := []Release{}
|
||||
authorised := r.Context().Value("role") != nil && r.Context().Value("role") == "admin"
|
||||
for _, release := range Releases {
|
||||
if !release.IsReleased() && !authorised {
|
||||
continue
|
||||
}
|
||||
releases = append(releases, release)
|
||||
}
|
||||
|
||||
global.ServeTemplate("music.html", Releases).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
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 := GetRelease(id)
|
||||
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(ServeGateway()).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
lrw := global.LoggingResponseWriter{w, http.StatusOK}
|
||||
|
||||
global.ServeTemplate("music-gateway.html", release).ServeHTTP(&lrw, r)
|
||||
|
||||
if lrw.Code != http.StatusOK {
|
||||
fmt.Printf("Error loading music gateway for %s\n", id)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue