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
+25
-13
@@ -1,22 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getAvatarUrl, HABBO_IMAGER_URL, resolveImagerBase } from "./imager";
|
||||
import {
|
||||
getAvatarUrl,
|
||||
DEFAULT_IMAGER_URL,
|
||||
resolveImagerBase,
|
||||
resolveUpstreamBase,
|
||||
} 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("defaults to the site's own /imaging endpoint", () => {
|
||||
expect(resolveImagerBase()).toBe(DEFAULT_IMAGER_URL);
|
||||
expect(resolveImagerBase("")).toBe(DEFAULT_IMAGER_URL);
|
||||
expect(resolveImagerBase(null)).toBe(DEFAULT_IMAGER_URL);
|
||||
});
|
||||
|
||||
it("rejects the broken self-hosted proxy", () => {
|
||||
expect(resolveImagerBase("/api/imaging/avatar")).toBe(HABBO_IMAGER_URL);
|
||||
expect(resolveImagerBase("/api/imaging/avatar")).toBe(DEFAULT_IMAGER_URL);
|
||||
expect(
|
||||
resolveImagerBase("https://www.epicnabbo.nl/api/imaging/avatar"),
|
||||
).toBe(HABBO_IMAGER_URL);
|
||||
expect(resolveImagerBase("/imaging")).toBe(HABBO_IMAGER_URL);
|
||||
expect(
|
||||
resolveImagerBase("https://www.epicnabbo.nl/imaging"),
|
||||
).toBe(HABBO_IMAGER_URL);
|
||||
).toBe(DEFAULT_IMAGER_URL);
|
||||
});
|
||||
|
||||
it("accepts /imaging as a valid configured URL", () => {
|
||||
expect(resolveImagerBase("/imaging")).toBe("/imaging");
|
||||
});
|
||||
|
||||
it("keeps a custom Habbo-compatible base", () => {
|
||||
@@ -26,14 +31,21 @@ describe("resolveImagerBase", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveUpstreamBase", () => {
|
||||
it("defaults to Habbo's public imager", () => {
|
||||
const url = resolveUpstreamBase();
|
||||
expect(url).toBe("https://www.habbo.com/habbo-imaging/avatarimage");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAvatarUrl", () => {
|
||||
it("builds a Habbo imager URL", () => {
|
||||
it("builds an 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.startsWith(`${DEFAULT_IMAGER_URL}?`)).toBe(true);
|
||||
expect(url).toContain("figure=hr-115-42.hd-180-1");
|
||||
expect(url).toContain("size=l");
|
||||
expect(url).toContain("headonly=1");
|
||||
|
||||
+22
-13
@@ -1,38 +1,47 @@
|
||||
/**
|
||||
* Habbo avatar imager helpers.
|
||||
* 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.
|
||||
* The public-facing imager URL resolves to the site's own `/imaging` endpoint
|
||||
* by default. That endpoint proxies the image from the configured upstream
|
||||
* (defaults to Habbo's public imager).
|
||||
*/
|
||||
|
||||
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";
|
||||
/** Default public imager endpoint (relative — served by the site itself). */
|
||||
export const DEFAULT_IMAGER_URL = "/imaging";
|
||||
|
||||
/**
|
||||
* Resolve a usable imager base URL.
|
||||
* Rejects the broken self-hosted proxy if it was stored in settings.
|
||||
* Resolve the public-facing imager base URL.
|
||||
* Rejects the broken self-hosted proxy route if 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") && !fromEnv.includes("/imaging")) {
|
||||
if (fromEnv && !fromEnv.includes("/api/imaging/avatar")) {
|
||||
return fromEnv.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
const v = (configured ?? "").trim();
|
||||
if (!v || v.includes("/api/imaging/avatar") || v.includes("/imaging")) {
|
||||
return HABBO_IMAGER_URL;
|
||||
if (!v || v.includes("/api/imaging/avatar")) {
|
||||
return DEFAULT_IMAGER_URL;
|
||||
}
|
||||
return v.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an avatar image URL (Habbo imager by default).
|
||||
* Resolve the upstream URL used by the proxy endpoint.
|
||||
* Falls back to Habbo's public imager when nothing else is configured.
|
||||
*/
|
||||
export function resolveUpstreamBase(): string {
|
||||
const fromEnv = process.env.IMAGING_UPSTREAM_URL?.trim();
|
||||
if (fromEnv) return fromEnv.replace(/\/+$/, "");
|
||||
return "https://www.habbo.com/habbo-imaging/avatarimage";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an avatar image URL (site's own `/imaging` endpoint by default).
|
||||
*
|
||||
* @example
|
||||
* getAvatarUrl('hr-893-45.hd-180-1.ch-210-66.lg-270-82')
|
||||
|
||||
@@ -3,7 +3,7 @@ import { redis } from "@/lib/redis";
|
||||
|
||||
const DEFAULTS: Record<string, string> = {
|
||||
hotel_name: "Atom",
|
||||
habbo_imaging_url: "https://www.habbo.com/habbo-imaging/avatarimage",
|
||||
habbo_imaging_url: "/imaging",
|
||||
logo_url: "",
|
||||
nitro_client_url: "",
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user