Compare commits
No commits in common. "f77e0bd08fe8721110691f59af29939d5c00c846" and "f414dfdb77a1fa38cbdce3ebcd451c684569fabc" have entirely different histories.
f77e0bd08f
...
f414dfdb77
17
module/discord/guild-icon.go
Normal file
17
module/discord/guild-icon.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"image"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getGuildIcon(s *discordgo.Session, guild *discordgo.UserGuild) (image.Image, error) {
|
||||||
|
body, err := s.RequestWithBucketID("GET", discordgo.EndpointGuildIcon(guild.ID, guild.Icon), nil, discordgo.EndpointGuildIcon(guild.ID, ""))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
img, _, err := image.Decode(bytes.NewReader(body))
|
||||||
|
return img, err
|
||||||
|
}
|
@ -12,7 +12,8 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"image"
|
||||||
|
"image/png"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -60,11 +61,7 @@ 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)
|
||||||
@ -89,7 +86,7 @@ func (m *Module) getClient(cb func(http.ResponseWriter, *http.Request, *utils.St
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils.State, discordClient *discordgo.Session) {
|
func (m *Module) homepage(rw http.ResponseWriter, req *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)
|
||||||
@ -125,7 +122,6 @@ func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils.
|
|||||||
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
|
||||||
@ -133,7 +129,6 @@ func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils.
|
|||||||
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,
|
||||||
@ -183,51 +178,40 @@ 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) bucketFetchImage(rw http.ResponseWriter, discordClient *discordgo.Session, endpoint, emptyEndpoint, contentType string) {
|
func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, state *utils.State, discordClient *discordgo.Session) {
|
||||||
body, err := discordClient.RequestWithBucketID("GET", endpoint, nil, emptyEndpoint)
|
vars := mux.Vars(req)
|
||||||
|
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", contentType)
|
rw.Header().Set("Content-Type", "image/png")
|
||||||
|
rw.WriteHeader(200)
|
||||||
|
|
||||||
|
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) {
|
||||||
|
vars := mux.Vars(req)
|
||||||
|
body, err := discordClient.RequestWithBucketID("GET", discordgo.EndpointGuildIcon(vars["guildId"], vars["iconId"]), nil, discordgo.EndpointGuildIcon(vars["guildId"], ""))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rw.Header().Set("Content-Type", "image/png")
|
||||||
rw.WriteHeader(http.StatusOK)
|
rw.WriteHeader(http.StatusOK)
|
||||||
_, _ = io.Copy(rw, bytes.NewReader(body))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Module) userAvatar(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
|
myAvatar, _, err := image.Decode(bytes.NewReader(body))
|
||||||
vars := mux.Vars(req)
|
err = png.Encode(rw, myAvatar)
|
||||||
endpoint := discordgo.EndpointUserAvatar
|
if err != nil {
|
||||||
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["avatarId"]), endpoint("", ""), "image/png")
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Module) userAvatarAnim(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
|
|
||||||
vars := mux.Vars(req)
|
|
||||||
endpoint := discordgo.EndpointUserAvatarAnimated
|
|
||||||
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], "a_"+vars["avatarId"]), endpoint("", ""), "image/gif")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Module) userBanner(rw http.ResponseWriter, req *http.Request, _ *utils.State, discordClient *discordgo.Session) {
|
|
||||||
vars := mux.Vars(req)
|
|
||||||
endpoint := discordgo.EndpointUserBanner
|
|
||||||
m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["bannerId"]), endpoint("", ""), "image/png")
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -39,7 +39,7 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Discord | <a href="/">Melon Tools</a></h1>
|
<h1>Discord | Melon Tools</h1>
|
||||||
<div class="page-content">
|
<div class="page-content">
|
||||||
<div>
|
<div>
|
||||||
{{with .User}}
|
{{with .User}}
|
||||||
@ -48,15 +48,13 @@
|
|||||||
<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}} alt="User Avatar"/></li>
|
<li>Avatar: <img {{$.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: <img {{$.Banner}} alt="User Banner"/></li>
|
<li>Banner: {{.Banner}}</li>
|
||||||
<li>Accent Color: {{$.UserAccent}} <span class="colour-block"
|
<li>Accent Color: {{$.UserAccent}} <span class="colour-block" style="background-color: {{$.UserAccent}}"> </span></li>
|
||||||
style="background-color: {{$.UserAccent}}"> </span>
|
|
||||||
</li>
|
|
||||||
<li>Bot: {{.Bot}}</li>
|
<li>Bot: {{.Bot}}</li>
|
||||||
<li>Premium: {{.PremiumType}}</li>
|
<li>Premium: {{.PremiumType}}</li>
|
||||||
<li>System: {{.System}}</li>
|
<li>System: {{.System}}</li>
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Gitea | <a href="/">Melon Tools</a></h1>
|
<h1>Gitea | Melon Tools</h1>
|
||||||
<div class="page-content">
|
<div class="page-content">
|
||||||
<div>
|
<div>
|
||||||
<p>Select repository source:</p>
|
<p>Select repository source:</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user