Improved caching of avatars

This commit is contained in:
Eduard Urbach 2017-06-18 13:50:53 +02:00
parent bf1d87c33e
commit 137db5997f
3 changed files with 30 additions and 37 deletions

View File

@ -3,20 +3,16 @@ package main
import ( import (
"errors" "errors"
"net/http" "net/http"
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
) )
func init() { func init() {
// Favicon // Web manifest
app.Get("/favicon.ico", func(ctx *aero.Context) string { app.Get("/manifest.json", func(ctx *aero.Context) string {
return ctx.TryWebP("images/brand/64", ".png") return ctx.JSON(app.Config.Manifest)
})
// Favicon
app.Get("/images/brand/:size", func(ctx *aero.Context) string {
return ctx.TryWebP("images/brand/"+ctx.Get("size"), ".png")
}) })
// Scripts // Scripts
@ -24,19 +20,26 @@ func init() {
return ctx.File("temp/scripts.js") return ctx.File("temp/scripts.js")
}) })
// Web manifest // Favicon
app.Get("/manifest.json", func(ctx *aero.Context) string { app.Get("/favicon.ico", func(ctx *aero.Context) string {
return ctx.JSON(app.Config.Manifest) 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 // SVG icons
app.Get("/icons/:file", func(ctx *aero.Context) string { 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 // Cover image
app.Get("/images/cover/:file", func(ctx *aero.Context) string { 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 // Login buttons
@ -45,52 +48,42 @@ func init() {
}) })
// Avatars // Avatars
app.Get("/user/:nick/avatar", func(ctx *aero.Context) string { app.Get("/images/avatars/large/:file", func(ctx *aero.Context) string {
nick := ctx.Get("nick") file := strings.TrimSuffix(ctx.Get("file"), ".webp")
user, err := arn.GetUserByNick(nick)
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
if ctx.CanUseWebP() { 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( original := arn.FindFileWithExtension(
user.ID, file,
"images/avatars/large/original/", "images/avatars/large/original/",
arn.OriginalImageExtensions, arn.OriginalImageExtensions,
) )
if original == "" { 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) return ctx.File(original)
}) })
// Avatars // Avatars
app.Get("/user/:nick/avatar/small", func(ctx *aero.Context) string { app.Get("/images/avatars/small/:file", func(ctx *aero.Context) string {
nick := ctx.Get("nick") file := strings.TrimSuffix(ctx.Get("file"), ".webp")
user, err := arn.GetUserByNick(nick)
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
if ctx.CanUseWebP() { 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( original := arn.FindFileWithExtension(
user.ID, file,
"images/avatars/small/original/", "images/avatars/small/original/",
arn.OriginalImageExtensions, arn.OriginalImageExtensions,
) )
if original == "" { 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) return ctx.File(original)

View File

@ -3,8 +3,8 @@ component Avatar(user *arn.User)
AvatarNoLink(user) AvatarNoLink(user)
component AvatarNoLink(user *arn.User) component AvatarNoLink(user *arn.User)
if user.Avatar != "" if user.HasAvatar()
img.user-image(src=user.Avatar + "/small", alt=user.Nick) img.user-image(src=user.SmallAvatar(), alt=user.Nick)
else else
SVGAvatar SVGAvatar

View File

@ -1,6 +1,6 @@
component ProfileImage(user *arn.User) component ProfileImage(user *arn.User)
if user.Avatar != "" if user.HasAvatar()
img.profile-image(src=user.Avatar, alt="Profile image") img.profile-image(src=user.LargeAvatar(), alt="Profile image")
else else
svg.profile-image(viewBox="0 0 50 50", alt="Profile image") svg.profile-image(viewBox="0 0 50 50", alt="Profile image")
circle.head(cx="25", cy="19", r="10") circle.head(cx="25", cy="19", r="10")