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