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 (
"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)

View File

@ -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

View File

@ -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")