Add support for banner image

This commit is contained in:
Melon 2022-05-16 12:57:46 +01:00
parent d42ef41dc0
commit f77e0bd08f
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
2 changed files with 118 additions and 101 deletions

View File

@ -12,8 +12,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"html/template" "html/template"
"image" "io"
"image/png"
"net/http" "net/http"
"os" "os"
) )
@ -61,7 +60,11 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri
} }
router.HandleFunc("/", m.getClient(m.homepage)) router.HandleFunc("/", m.getClient(m.homepage))
router.HandleFunc("/login", m.sessionWrapper(m.loginPage)) router.HandleFunc("/login", m.sessionWrapper(m.loginPage))
router.HandleFunc("/user/avatar/{userId}/a_{avatarId}", m.getClient(m.userAvatarAnim))
router.HandleFunc("/user/avatar/{userId}/{avatarId}", m.getClient(m.userAvatar)) router.HandleFunc("/user/avatar/{userId}/{avatarId}", m.getClient(m.userAvatar))
router.HandleFunc("/user/banner/{userId}/a_{bannerId}", m.getClient(m.userAvatarAnim))
router.HandleFunc("/user/banner/{userId}/{bannerId}", m.getClient(m.userAvatar))
router.HandleFunc("/guild/icon/{guildId}/a_{iconId}", m.getClient(m.guildIconAnim))
router.HandleFunc("/guild/icon/{guildId}/{iconId}", m.getClient(m.guildIcon)) router.HandleFunc("/guild/icon/{guildId}/{iconId}", m.getClient(m.guildIcon))
router.PathPrefix("/assets/icon/{name}.svg").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { router.PathPrefix("/assets/icon/{name}.svg").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
@ -86,7 +89,7 @@ func (m *Module) getClient(cb func(http.ResponseWriter, *http.Request, *utils.St
}) })
} }
func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils.State, discordClient *discordgo.Session) {
myUser, err := discordClient.User("@me") myUser, err := discordClient.User("@me")
if err != nil { if err != nil {
state.Del(KeyOauthClient) state.Del(KeyOauthClient)
@ -122,6 +125,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
User *discordgo.User User *discordgo.User
UserAccent string UserAccent string
Avatar template.HTMLAttr Avatar template.HTMLAttr
Banner template.HTMLAttr
Guilds []*discordgo.UserGuild Guilds []*discordgo.UserGuild
GuildIcons []template.HTMLAttr GuildIcons []template.HTMLAttr
Connections []*discordgo.UserConnection Connections []*discordgo.UserConnection
@ -129,6 +133,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
User: myUser, User: myUser,
UserAccent: fmt.Sprintf("#%06x", myUser.AccentColor), UserAccent: fmt.Sprintf("#%06x", myUser.AccentColor),
Avatar: template.HTMLAttr(fmt.Sprintf("src=\"/discord/user/avatar/%s/%s\"", myUser.ID, myUser.Avatar)), Avatar: template.HTMLAttr(fmt.Sprintf("src=\"/discord/user/avatar/%s/%s\"", myUser.ID, myUser.Avatar)),
Banner: template.HTMLAttr(fmt.Sprintf("src=\"/discord/user/banner/%s/%s\"", myUser.ID, myUser.Banner)),
Guilds: myGuilds, Guilds: myGuilds,
GuildIcons: guildIcons, GuildIcons: guildIcons,
Connections: myConns, Connections: myConns,
@ -178,40 +183,51 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti
http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect) http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect)
} }
func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { func (m *Module) bucketFetchImage(rw http.ResponseWriter, discordClient *discordgo.Session, endpoint, emptyEndpoint, contentType string) {
vars := mux.Vars(req) body, err := discordClient.RequestWithBucketID("GET", endpoint, nil, emptyEndpoint)
body, err := discordClient.RequestWithBucketID("GET", discordgo.EndpointUserAvatar(vars["userId"], vars["avatarId"]), nil, discordgo.EndpointUserAvatar("", ""))
if err != nil { if err != nil {
return return
} }
rw.Header().Set("Content-Type", "image/png") rw.Header().Set("Content-Type", contentType)
rw.WriteHeader(200) rw.WriteHeader(http.StatusOK)
_, _ = io.Copy(rw, bytes.NewReader(body))
myAvatar, _, err := image.Decode(bytes.NewReader(body))
err = png.Encode(rw, myAvatar)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
} }
func (m *Module) guildIcon(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) { func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
vars := mux.Vars(req) vars := mux.Vars(req)
body, err := discordClient.RequestWithBucketID("GET", discordgo.EndpointGuildIcon(vars["guildId"], vars["iconId"]), nil, discordgo.EndpointGuildIcon(vars["guildId"], "")) endpoint := discordgo.EndpointUserAvatar
if err != nil { m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["avatarId"]), endpoint("", ""), "image/png")
return }
}
rw.Header().Set("Content-Type", "image/png") func (m *Module) userAvatarAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
rw.WriteHeader(http.StatusOK) vars := mux.Vars(req)
endpoint := discordgo.EndpointUserAvatarAnimated
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["avatarId"]), endpoint("", ""), "image/gif")
}
myAvatar, _, err := image.Decode(bytes.NewReader(body)) func (m *Module) userBanner(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
err = png.Encode(rw, myAvatar) vars := mux.Vars(req)
if err != nil { endpoint := discordgo.EndpointUserBanner
http.Error(rw, err.Error(), http.StatusInternalServerError) m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["bannerId"]), endpoint("", ""), "image/png")
return }
}
func (m *Module) userBannerAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
vars := mux.Vars(req)
endpoint := discordgo.EndpointUserBannerAnimated
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["bannerId"]), endpoint("", ""), "image/gif")
}
func (m *Module) guildIcon(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
vars := mux.Vars(req)
endpoint := discordgo.EndpointGuildIcon
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["guildId"]), endpoint("", ""), "image/png")
}
func (m *Module) guildIconAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
vars := mux.Vars(req)
endpoint := discordgo.EndpointGuildIconAnimated
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["guildId"]), endpoint("", ""), "image/gif")
} }
func connectedLinkFunc(a *discordgo.UserConnection) string { func connectedLinkFunc(a *discordgo.UserConnection) string {

View File

@ -43,84 +43,85 @@
<div class="page-content"> <div class="page-content">
<div> <div>
{{with .User}} {{with .User}}
<p>My User: {{.Username}}#{{.Discriminator}}</p> <p>My User: {{.Username}}#{{.Discriminator}}</p>
<ul> <ul>
<li>ID: {{.ID}}</li> <li>ID: {{.ID}}</li>
<li>Email: {{.Email}}</li> <li>Email: {{.Email}}</li>
<li>Username: {{.Username}}</li> <li>Username: {{.Username}}</li>
<li>Avatar: <img {{$.Avatar}}/></li> <li>Avatar: <img {{$.Avatar}} alt="User Avatar"/></li>
<li>Locale: {{.Locale}}</li> <li>Locale: {{.Locale}}</li>
<li>Discriminator: {{.Discriminator}}</li> <li>Discriminator: {{.Discriminator}}</li>
<li>Verified: {{.Verified}}</li> <li>Verified: {{.Verified}}</li>
<li>MFA: {{.MFAEnabled}}</li> <li>MFA: {{.MFAEnabled}}</li>
<li>Banner: {{.Banner}}</li> <li>Banner: <img {{$.Banner}} alt="User Banner"/></li>
<li>Accent Color: {{$.UserAccent}} <span class="colour-block" style="background-color: {{$.UserAccent}}">&nbsp;</span> <li>Accent Color: {{$.UserAccent}} <span class="colour-block"
</li> style="background-color: {{$.UserAccent}}">&nbsp;</span>
<li>Bot: {{.Bot}}</li> </li>
<li>Premium: {{.PremiumType}}</li> <li>Bot: {{.Bot}}</li>
<li>System: {{.System}}</li> <li>Premium: {{.PremiumType}}</li>
</ul> <li>System: {{.System}}</li>
</ul>
{{end}} {{end}}
</div> </div>
<div> <div>
{{with .User.PublicFlags}} {{with .User.PublicFlags}}
<p>My Flags:</p> <p>My Flags:</p>
<ul> <ul>
{{if checkFlag . 1}} {{if checkFlag . 1}}
<li>Discord Employee</li> <li>Discord Employee</li>
{{end}} {{end}}
{{if checkFlag . 2}} {{if checkFlag . 2}}
<li>Discord Partner</li> <li>Discord Partner</li>
{{end}} {{end}}
{{if checkFlag . 4}} {{if checkFlag . 4}}
<li>Hype Squad Events</li> <li>Hype Squad Events</li>
{{end}} {{end}}
{{if checkFlag . 8}} {{if checkFlag . 8}}
<li>Bug Hunter Level 1</li> <li>Bug Hunter Level 1</li>
{{end}} {{end}}
{{if checkFlag . 64}} {{if checkFlag . 64}}
<li>House Bravery</li> <li>House Bravery</li>
{{end}} {{end}}
{{if checkFlag . 128}} {{if checkFlag . 128}}
<li>House Brilliance</li> <li>House Brilliance</li>
{{end}} {{end}}
{{if checkFlag . 256}} {{if checkFlag . 256}}
<li>House Balance</li> <li>House Balance</li>
{{end}} {{end}}
{{if checkFlag . 512}} {{if checkFlag . 512}}
<li>Early Supporter</li> <li>Early Supporter</li>
{{end}} {{end}}
{{if checkFlag . 1024}} {{if checkFlag . 1024}}
<li>Team User</li> <li>Team User</li>
{{end}} {{end}}
{{if checkFlag . 4096}} {{if checkFlag . 4096}}
<li>System</li> <li>System</li>
{{end}} {{end}}
{{if checkFlag . 16384}} {{if checkFlag . 16384}}
<li>Bug Hunter Level 2</li> <li>Bug Hunter Level 2</li>
{{end}} {{end}}
{{if checkFlag . 65536}} {{if checkFlag . 65536}}
<li>Verified Bot</li> <li>Verified Bot</li>
{{end}} {{end}}
{{if checkFlag . 131072}} {{if checkFlag . 131072}}
<li>Verified Bot Developer</li> <li>Verified Bot Developer</li>
{{end}} {{end}}
{{if checkFlag . 262144}} {{if checkFlag . 262144}}
<li>Discord Certified Moderator</li> <li>Discord Certified Moderator</li>
{{end}} {{end}}
</ul> </ul>
{{end}} {{end}}
</div> </div>
<div> <div>
<p>Connections ({{len .Connections}}):</p> <p>Connections ({{len .Connections}}):</p>
<ul class="connected-accounts"> <ul class="connected-accounts">
{{range .Connections}} {{range .Connections}}
<li> <li>
<a href="{{connectedLink .}}" target="_blank" data-is-revoked="{{.Revoked}}"> <a href="{{connectedLink .}}" target="_blank" data-is-revoked="{{.Revoked}}">
<img src="/discord/assets/icon/{{.Type}}.svg"/> <img src="/discord/assets/icon/{{.Type}}.svg"/>
<span>{{.Name}}</span> <span>{{.Name}}</span>
</a> </a>
</li> </li>
{{end}} {{end}}
</ul> </ul>
</div> </div>
@ -128,11 +129,11 @@
<p>Guilds ({{len .Guilds}}):</p> <p>Guilds ({{len .Guilds}}):</p>
<div class="page-content guilds"> <div class="page-content guilds">
{{range $k, $v := .Guilds}} {{range $k, $v := .Guilds}}
<div class="guild-item"> <div class="guild-item">
<div><img {{index $.GuildIcons $k}}/></div> <div><img {{index $.GuildIcons $k}}/></div>
<div>{{$v.Name}}</div> <div>{{$v.Name}}</div>
<div>@*{{$v.ID}}</div> <div>@*{{$v.ID}}</div>
</div> </div>
{{end}} {{end}}
</div> </div>
</div> </div>