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
+5
-2
@@ -1,16 +1,19 @@
|
||||
import { resolveImagerBase } from "@/lib/imager";
|
||||
|
||||
/** Build a Habbo avatar-imager URL for a figure string. */
|
||||
export function avatarImageUrl(
|
||||
base: string,
|
||||
look: string,
|
||||
opts: { size?: "s" | "m" | "l"; headOnly?: boolean; direction?: number } = {},
|
||||
): string {
|
||||
const resolved = resolveImagerBase(base);
|
||||
const params = new URLSearchParams({ figure: look });
|
||||
if (opts.size) params.set("size", opts.size);
|
||||
if (opts.headOnly) params.set("headonly", "1");
|
||||
if (opts.direction !== undefined)
|
||||
params.set("direction", String(opts.direction));
|
||||
const sep = base.includes("?") ? "&" : "?";
|
||||
return `${base}${sep}${params.toString()}`;
|
||||
const sep = resolved.includes("?") ? "&" : "?";
|
||||
return `${resolved}${sep}${params.toString()}`;
|
||||
}
|
||||
|
||||
/** URL-safe slug from a title (lowercase, ascii, hyphenated). */
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getAvatarUrl, HABBO_IMAGER_URL, resolveImagerBase } from "./imager";
|
||||
|
||||
describe("resolveImagerBase", () => {
|
||||
it("defaults to Habbo imager", () => {
|
||||
expect(resolveImagerBase()).toBe(HABBO_IMAGER_URL);
|
||||
expect(resolveImagerBase("")).toBe(HABBO_IMAGER_URL);
|
||||
expect(resolveImagerBase(null)).toBe(HABBO_IMAGER_URL);
|
||||
});
|
||||
|
||||
it("rejects the broken self-hosted proxy", () => {
|
||||
expect(resolveImagerBase("/api/imaging/avatar")).toBe(HABBO_IMAGER_URL);
|
||||
expect(
|
||||
resolveImagerBase("https://www.epicnabbo.nl/api/imaging/avatar"),
|
||||
).toBe(HABBO_IMAGER_URL);
|
||||
});
|
||||
|
||||
it("keeps a custom Habbo-compatible base", () => {
|
||||
expect(
|
||||
resolveImagerBase("https://www.habbo.com/habbo-imaging/avatarimage/"),
|
||||
).toBe("https://www.habbo.com/habbo-imaging/avatarimage");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAvatarUrl", () => {
|
||||
it("builds a Habbo imager URL", () => {
|
||||
const url = getAvatarUrl("hr-115-42.hd-180-1", {
|
||||
size: "l",
|
||||
headOnly: true,
|
||||
direction: 2,
|
||||
});
|
||||
expect(url.startsWith(`${HABBO_IMAGER_URL}?`)).toBe(true);
|
||||
expect(url).toContain("figure=hr-115-42.hd-180-1");
|
||||
expect(url).toContain("size=l");
|
||||
expect(url).toContain("headonly=1");
|
||||
expect(url).toContain("direction=2");
|
||||
});
|
||||
});
|
||||
+29
-8
@@ -1,18 +1,38 @@
|
||||
/**
|
||||
* Centralized Habbo avatar imager utility.
|
||||
* Points to the self-hosted imager API (`/api/imaging/avatar`) which handles
|
||||
* disk + memory caching and (Phase 2) local rendering from .nitro bundles.
|
||||
* Override with NEXT_PUBLIC_IMAGER_URL when needed (e.g. CDN in front).
|
||||
* Habbo avatar imager helpers.
|
||||
*
|
||||
* Images are loaded directly from Habbo's public imager. The old self-hosted
|
||||
* `/api/imaging/avatar` proxy fails in production (Habbo blocks server-side
|
||||
* fetches), so the browser always hits Habbo.com instead.
|
||||
*/
|
||||
|
||||
const IMAGER_URL = process.env.NEXT_PUBLIC_IMAGER_URL || "/api/imaging/avatar";
|
||||
|
||||
export type { AvatarOptions } from "@/types/admin";
|
||||
|
||||
import type { AvatarOptions } from "@/types/admin";
|
||||
|
||||
/** Official Habbo avatar imaging endpoint. */
|
||||
export const HABBO_IMAGER_URL =
|
||||
"https://www.habbo.com/habbo-imaging/avatarimage";
|
||||
|
||||
/**
|
||||
* Build an avatar image URL served by the self-hosted imager.
|
||||
* Resolve a usable imager base URL.
|
||||
* Rejects the broken self-hosted proxy if it was stored in settings.
|
||||
*/
|
||||
export function resolveImagerBase(configured?: string | null): string {
|
||||
const fromEnv = process.env.NEXT_PUBLIC_IMAGER_URL?.trim();
|
||||
if (fromEnv && !fromEnv.includes("/api/imaging/avatar")) {
|
||||
return fromEnv.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
const v = (configured ?? "").trim();
|
||||
if (!v || v.includes("/api/imaging/avatar")) {
|
||||
return HABBO_IMAGER_URL;
|
||||
}
|
||||
return v.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an avatar image URL (Habbo imager by default).
|
||||
*
|
||||
* @example
|
||||
* getAvatarUrl('hr-893-45.hd-180-1.ch-210-66.lg-270-82')
|
||||
@@ -43,5 +63,6 @@ export function getAvatarUrl(
|
||||
if (gesture) params.set("gesture", gesture);
|
||||
if (action) params.set("action", action);
|
||||
|
||||
return `${IMAGER_URL}?${params.toString()}`;
|
||||
const base = resolveImagerBase();
|
||||
return `${base}?${params.toString()}`;
|
||||
}
|
||||
Reference in new issue
Block a user