From 137db5997f00d72b9a5842fa9372e8bd26c99f2a Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 18 Jun 2017 13:50:53 +0200 Subject: [PATCH] Improved caching of avatars --- assets.go | 59 ++++++++++++++++++---------------------- mixins/Avatar.pixy | 4 +-- mixins/ProfileImage.pixy | 4 +-- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/assets.go b/assets.go index a8398dcd..1b8ca125 100644 --- a/assets.go +++ b/assets.go @@ -3,20 +3,16 @@ package main import ( "errors" "net/http" + "strings" "github.com/aerogo/aero" "github.com/animenotifier/arn" ) func init() { - // Favicon - app.Get("/favicon.ico", func(ctx *aero.Context) string { - return ctx.TryWebP("images/brand/64", ".png") - }) - - // Favicon - app.Get("/images/brand/:size", func(ctx *aero.Context) string { - return ctx.TryWebP("images/brand/"+ctx.Get("size"), ".png") + // Web manifest + app.Get("/manifest.json", func(ctx *aero.Context) string { + return ctx.JSON(app.Config.Manifest) }) // Scripts @@ -24,19 +20,26 @@ func init() { return ctx.File("temp/scripts.js") }) - // Web manifest - app.Get("/manifest.json", func(ctx *aero.Context) string { - return ctx.JSON(app.Config.Manifest) + // Favicon + app.Get("/favicon.ico", func(ctx *aero.Context) string { + return ctx.TryWebP("images/brand/64", ".png") + }) + + // Brand icons + app.Get("/images/brand/:file", func(ctx *aero.Context) string { + file := strings.TrimSuffix(ctx.Get("file"), ".webp") + return ctx.TryWebP("images/brand/"+file, ".png") }) // SVG icons app.Get("/icons/:file", func(ctx *aero.Context) string { - return ctx.File("images/icons/svg/" + ctx.Get("file") + ".svg") + return ctx.File("images/icons/svg/" + ctx.Get("file")) }) // Cover image app.Get("/images/cover/:file", func(ctx *aero.Context) string { - return ctx.TryWebP("images/cover/"+ctx.Get("file"), ".jpg") + file := strings.TrimSuffix(ctx.Get("file"), ".webp") + return ctx.TryWebP("images/cover/"+file, ".jpg") }) // Login buttons @@ -45,52 +48,42 @@ func init() { }) // Avatars - app.Get("/user/:nick/avatar", func(ctx *aero.Context) string { - nick := ctx.Get("nick") - user, err := arn.GetUserByNick(nick) - - if err != nil { - return ctx.Error(http.StatusNotFound, "User not found", err) - } + app.Get("/images/avatars/large/:file", func(ctx *aero.Context) string { + file := strings.TrimSuffix(ctx.Get("file"), ".webp") if ctx.CanUseWebP() { - return ctx.File("images/avatars/large/webp/" + user.ID + ".webp") + return ctx.File("images/avatars/large/webp/" + file + ".webp") } original := arn.FindFileWithExtension( - user.ID, + file, "images/avatars/large/original/", arn.OriginalImageExtensions, ) if original == "" { - return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found for user: "+user.ID)) + return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found: "+file)) } return ctx.File(original) }) // Avatars - app.Get("/user/:nick/avatar/small", func(ctx *aero.Context) string { - nick := ctx.Get("nick") - user, err := arn.GetUserByNick(nick) - - if err != nil { - return ctx.Error(http.StatusNotFound, "User not found", err) - } + app.Get("/images/avatars/small/:file", func(ctx *aero.Context) string { + file := strings.TrimSuffix(ctx.Get("file"), ".webp") if ctx.CanUseWebP() { - return ctx.File("images/avatars/small/webp/" + user.ID + ".webp") + return ctx.File("images/avatars/small/webp/" + file + ".webp") } original := arn.FindFileWithExtension( - user.ID, + file, "images/avatars/small/original/", arn.OriginalImageExtensions, ) if original == "" { - return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found for user: "+user.ID)) + return ctx.Error(http.StatusNotFound, "Avatar not found", errors.New("Image not found: "+file)) } return ctx.File(original) diff --git a/mixins/Avatar.pixy b/mixins/Avatar.pixy index 845f9f5e..c95f96f4 100644 --- a/mixins/Avatar.pixy +++ b/mixins/Avatar.pixy @@ -3,8 +3,8 @@ component Avatar(user *arn.User) AvatarNoLink(user) component AvatarNoLink(user *arn.User) - if user.Avatar != "" - img.user-image(src=user.Avatar + "/small", alt=user.Nick) + if user.HasAvatar() + img.user-image(src=user.SmallAvatar(), alt=user.Nick) else SVGAvatar diff --git a/mixins/ProfileImage.pixy b/mixins/ProfileImage.pixy index eb6bae6e..dc06f415 100644 --- a/mixins/ProfileImage.pixy +++ b/mixins/ProfileImage.pixy @@ -1,6 +1,6 @@ component ProfileImage(user *arn.User) - if user.Avatar != "" - img.profile-image(src=user.Avatar, alt="Profile image") + if user.HasAvatar() + img.profile-image(src=user.LargeAvatar(), alt="Profile image") else svg.profile-image(viewBox="0 0 50 50", alt="Profile image") circle.head(cx="25", cy="19", r="10")