From d12edc93ae06f62868ad3938f5c7250bb3d8d889 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Mon, 16 May 2022 13:08:44 +0100 Subject: [PATCH] Remove image caches --- module/discord/main.go | 64 ++++-------------------------------------- 1 file changed, 6 insertions(+), 58 deletions(-) diff --git a/module/discord/main.go b/module/discord/main.go index e9bde01..1e3d091 100644 --- a/module/discord/main.go +++ b/module/discord/main.go @@ -1,7 +1,6 @@ package discord import ( - "bytes" "code.mrmelon54.xyz/sean/melon-tools/utils" "context" "embed" @@ -12,7 +11,6 @@ import ( "github.com/gorilla/mux" "golang.org/x/oauth2" "html/template" - "io" "net/http" "os" ) @@ -60,12 +58,6 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri } router.HandleFunc("/", m.getClient(m.homepage)) 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/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.PathPrefix("/assets/icon/{name}.svg").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) b, err := iconFiles.ReadFile("pages/assets/icon/" + vars["name"] + ".svg") @@ -118,7 +110,10 @@ func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils. guildIcons := make([]template.HTMLAttr, len(myGuilds)) for i, j := range myGuilds { - guildIcons[i] = template.HTMLAttr(fmt.Sprintf("src=\"/discord/guild/icon/%s/%s\"", j.ID, j.Icon)) + var a discordgo.Guild + a.ID = j.ID + a.Icon = j.Icon + guildIcons[i] = template.HTMLAttr(fmt.Sprintf("src=\"%s\"", a.IconURL())) } err = tmp.Execute(rw, struct { @@ -132,8 +127,8 @@ func (m *Module) homepage(rw http.ResponseWriter, _ *http.Request, state *utils. }{ User: myUser, UserAccent: fmt.Sprintf("#%06x", myUser.AccentColor), - 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)), + Avatar: template.HTMLAttr(fmt.Sprintf("src=\"%s\"", myUser.AvatarURL("256"))), + Banner: template.HTMLAttr(fmt.Sprintf("src=\"%s\"", myUser.BannerURL("256"))), Guilds: myGuilds, GuildIcons: guildIcons, Connections: myConns, @@ -183,53 +178,6 @@ 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) } -func (m *Module) bucketFetchImage(rw http.ResponseWriter, discordClient *discordgo.Session, endpoint, emptyEndpoint, contentType string) { - body, err := discordClient.RequestWithBucketID("GET", endpoint, nil, emptyEndpoint) - if err != nil { - return - } - - rw.Header().Set("Content-Type", contentType) - 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) { - vars := mux.Vars(req) - endpoint := discordgo.EndpointUserAvatar - m.bucketFetchImage(rw, discordClient, endpoint(vars["userId"], vars["avatarId"]), endpoint("", ""), "image/png") -} - -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 { switch a.Type { case "github":