Serve avatars from Habbo imager instead of broken self-hosted proxy.
Local Build and Deploy / deploy (push) Successful in 56s
Local Build and Deploy / deploy (push) Successful in 56s
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
2c68fb7e5a
commit
8cce8837bc
6 files changed
+107
-51
No files matched your search
@@ -17,6 +17,7 @@ import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { getAvatarUrl } from "@/lib/imager";
|
||||
|
||||
// ── Shared types ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -115,11 +116,15 @@ function AvatarPreview({ type, id }: { type?: string; id?: number }) {
|
||||
return <Shirt className="w-8 h-8 text-muted-foreground/60" />;
|
||||
}
|
||||
const figure = buildPreviewFigure(type, id);
|
||||
const headOnly = HEAD_TYPES.has(type) ? "&headonly=1" : "";
|
||||
return (
|
||||
// biome-ignore lint/performance/noImgElement: dynamic avatar imaging, not a Next asset
|
||||
<img
|
||||
src={`/api/imaging/avatar?figure=${encodeURIComponent(figure)}&size=l&direction=2&head_direction=3${headOnly}`}
|
||||
src={getAvatarUrl(figure, {
|
||||
size: "l",
|
||||
direction: 2,
|
||||
headDirection: 3,
|
||||
headOnly: HEAD_TYPES.has(type),
|
||||
})}
|
||||
alt={`${type}-${id}`}
|
||||
className="max-h-full object-contain image-pixelated"
|
||||
onError={() => setFailed(true)}
|
||||
|
||||
@@ -91,13 +91,15 @@ export const SETTINGS_GROUPS: SettingsGroup[] = [
|
||||
fields: [
|
||||
{
|
||||
key: "habbo_imaging_url",
|
||||
label: "Habbo imaging upstream",
|
||||
label: "Habbo imager URL",
|
||||
type: "url",
|
||||
defaultValue: "https://www.habbo.com/habbo-imaging/avatarimage",
|
||||
description:
|
||||
"Public Habbo avatar endpoint used in the browser. Do not point this at /api/imaging/avatar.",
|
||||
},
|
||||
{
|
||||
key: "imaging_use_habbo_fallback",
|
||||
label: "Use Habbo.com as avatar fallback",
|
||||
label: "Use Habbo.com as avatar fallback (legacy proxy)",
|
||||
type: "boolean",
|
||||
defaultValue: "1",
|
||||
},
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
||||
import {
|
||||
type AvatarRenderResult,
|
||||
renderAvatar,
|
||||
} from "@/lib/services/imager/avatar-renderer";
|
||||
import { HABBO_IMAGER_URL, resolveImagerBase } from "@/lib/imager";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
|
||||
/**
|
||||
* GET /api/imaging/avatar?figure=hr-115-42.hd-195-19&size=l&direction=2&...
|
||||
* GET /api/imaging/avatar?figure=…&size=l&direction=2&…
|
||||
*
|
||||
* Self-hosted avatar imager with disk + memory cache and Habbo upstream fallback.
|
||||
* Redirects to Habbo's public imager. Server-side proxying fails in production
|
||||
* (Habbo blocks datacenter IPs), so the browser loads the image from Habbo.
|
||||
*/
|
||||
|
||||
const FIGURE_RE = /^[a-z]{2}-\d+/i;
|
||||
@@ -16,15 +14,6 @@ const FIGURE_MAX_LEN = 512;
|
||||
const FIGURE_MAX_PARTS = 24;
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const ip = await clientIp();
|
||||
const limited = await rateLimit(`avatar-imaging:${ip}`, 60, 60_000);
|
||||
if (!limited.ok) {
|
||||
return new NextResponse(null, {
|
||||
status: 429,
|
||||
headers: { "Retry-After": String(limited.retryAfter) },
|
||||
});
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
const figure = (
|
||||
@@ -68,35 +57,33 @@ export async function GET(request: NextRequest) {
|
||||
const gesture = searchParams.get("gesture") ?? undefined;
|
||||
const action = searchParams.get("action") ?? undefined;
|
||||
|
||||
const result: AvatarRenderResult = await renderAvatar({
|
||||
const params = new URLSearchParams({
|
||||
figure,
|
||||
direction: String(direction),
|
||||
head_direction: String(headDirection),
|
||||
size,
|
||||
direction,
|
||||
headDirection,
|
||||
headOnly,
|
||||
gesture,
|
||||
action,
|
||||
});
|
||||
if (headOnly) params.set("headonly", "1");
|
||||
if (gesture) params.set("gesture", gesture);
|
||||
if (action) params.set("action", action);
|
||||
|
||||
const ifNoneMatch = request.headers.get("if-none-match");
|
||||
if (ifNoneMatch && ifNoneMatch === result.etag) {
|
||||
return new NextResponse(null, {
|
||||
status: 304,
|
||||
headers: {
|
||||
ETag: result.etag,
|
||||
"Cache-Control": "public, max-age=604800, immutable",
|
||||
},
|
||||
});
|
||||
let base = HABBO_IMAGER_URL;
|
||||
try {
|
||||
const configured = await siteSettings.get(
|
||||
"habbo_imaging_url",
|
||||
HABBO_IMAGER_URL,
|
||||
);
|
||||
base = resolveImagerBase(configured);
|
||||
} catch {
|
||||
base = HABBO_IMAGER_URL;
|
||||
}
|
||||
|
||||
return new NextResponse(new Uint8Array(result.buffer), {
|
||||
status: 200,
|
||||
const target = `${base}?${params.toString()}`;
|
||||
return NextResponse.redirect(target, {
|
||||
status: 302,
|
||||
headers: {
|
||||
"Content-Type": "image/png",
|
||||
"Cache-Control": "public, max-age=604800, immutable",
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
ETag: result.etag,
|
||||
"X-Imager-Source": result.source,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user