Serve avatars from site's own /imaging endpoint instead of habbo.com
Local Build and Deploy / deploy (push) Successful in 1m7s
Local Build and Deploy / deploy (push) Successful in 1m7s
- Change default public imager URL from habbo.com to /imaging - /imaging and /api/imaging/avatar now proxy from upstream (habbo.com) instead of redirecting - Add resolveUpstreamBase() to separate public URL from upstream URL - Update admin settings default and description
This commit is contained in:
1 parent
71f154cf52
commit
19faa5615c
6 files changed
+128
-79
No files matched your search
@@ -1,17 +1,10 @@
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { HABBO_IMAGER_URL, resolveImagerBase } from "@/lib/imager";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
|
||||
/**
|
||||
* GET /api/imaging/avatar?figure=…&size=l&direction=2&…
|
||||
*
|
||||
* Redirects to Habbo's public imager. Server-side proxying fails in production
|
||||
* (Habbo blocks datacenter IPs), so the browser loads the image from Habbo.
|
||||
*/
|
||||
import { resolveUpstreamBase } from "@/lib/imager";
|
||||
|
||||
const FIGURE_RE = /^[a-z]{2}-\d+/i;
|
||||
const FIGURE_MAX_LEN = 512;
|
||||
const FIGURE_MAX_PARTS = 24;
|
||||
const UPSTREAM_TIMEOUT_MS = 10_000;
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
@@ -62,30 +55,50 @@ export async function GET(request: NextRequest) {
|
||||
direction: String(direction),
|
||||
head_direction: String(headDirection),
|
||||
size,
|
||||
img_format: "png",
|
||||
});
|
||||
if (headOnly) params.set("headonly", "1");
|
||||
if (gesture) params.set("gesture", gesture);
|
||||
if (action) params.set("action", action);
|
||||
|
||||
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;
|
||||
}
|
||||
const imgFormat = searchParams.get("img_format");
|
||||
if (imgFormat) params.set("img_format", imgFormat);
|
||||
|
||||
const target = `${base}?${params.toString()}`;
|
||||
return NextResponse.redirect(target, {
|
||||
status: 302,
|
||||
headers: {
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
});
|
||||
const upstream = resolveUpstreamBase();
|
||||
const upstreamUrl = `${upstream}?${params.toString()}`;
|
||||
|
||||
try {
|
||||
const res = await fetch(upstreamUrl, {
|
||||
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
return NextResponse.json(
|
||||
{ error: "Upstream imager returned an error" },
|
||||
{ status: res.status },
|
||||
);
|
||||
}
|
||||
|
||||
const buffer = await res.arrayBuffer();
|
||||
const contentType = res.headers.get("content-type") || "image/png";
|
||||
|
||||
return new NextResponse(buffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to fetch avatar from upstream" },
|
||||
{ status: 502 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function OPTIONS() {
|
||||
|
||||
Reference in new issue
Block a user