File size: 17,865 Bytes
5ef6e9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | import { Router } from "express";
import { db, imagesTable, usersTable, configTable, creditTransactionsTable } from "@workspace/db";
import {
GenerateImageBody,
GetImageHistoryQueryParams,
DeleteImageParams,
} from "@workspace/api-zod";
import { desc, eq, count, and, or, isNull, sql } from "drizzle-orm";
import {
getValidBearerToken, refreshAccessToken,
getPoolToken, markAccountUsed, tryRefreshPoolAccount, disablePoolAccount,
} from "./config";
import { optionalJwtAuth } from "./auth";
import { generateGuardId } from "../guardId";
const router = Router();
const GEMINIGEN_BASE = "https://api.geminigen.ai/api";
const STYLE_PROMPTS: Record<string, string> = {
realistic: "photorealistic, high quality, detailed, 8k resolution",
anime: "anime style, manga art style, japanese animation",
artistic: "artistic, fine art, expressive brushwork",
cartoon: "cartoon style, colorful, fun illustration",
sketch: "pencil sketch, hand drawn, black and white drawing",
oil_painting: "oil painting, classical art style, textured canvas",
watercolor: "watercolor painting, soft colors, fluid brushstrokes",
digital_art: "digital art, concept art, highly detailed digital illustration",
};
const ORIENTATION_MAP: Record<string, string> = {
"1:1": "square",
"16:9": "landscape",
"9:16": "portrait",
"4:3": "landscape",
"3:4": "portrait",
"2:3": "portrait",
"3:2": "landscape",
};
const USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/124.0.6367.111 Mobile/15E148 Safari/604.1";
function base64ToBlob(base64: string, mime: string): Blob {
const binary = Buffer.from(base64, "base64");
return new Blob([binary], { type: mime });
}
async function pollForImage(uuid: string, token: string, maxWaitMs = 120000): Promise<string | null> {
const interval = 3000;
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
await new Promise((r) => setTimeout(r, interval));
const resp = await fetch(`${GEMINIGEN_BASE}/history/${uuid}`, {
headers: {
Authorization: `Bearer ${token}`,
"x-guard-id": generateGuardId("/api/history/" + uuid, "get"),
"User-Agent": USER_AGENT,
Accept: "application/json",
},
});
if (!resp.ok) break;
const data = await resp.json() as {
status?: number;
generated_image?: Array<{ image_url?: string }>;
};
if (data.status === 2 || data.status === 3) {
return data.generated_image?.[0]?.image_url || null;
}
if (typeof data.status === "number" && data.status > 3) break;
}
return null;
}
async function callGrokEndpoint(prompt: string, orientation: string, token: string, refImageBase64?: string, refImageMime?: string) {
const form = new FormData();
form.append("prompt", prompt);
form.append("orientation", orientation);
form.append("num_result", "1");
if (refImageBase64 && refImageMime) {
const blob = base64ToBlob(refImageBase64, refImageMime);
form.append("files", blob, "reference.jpg");
}
const resp = await fetch(`${GEMINIGEN_BASE}/imagen/grok`, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "x-guard-id": generateGuardId("/api/imagen/grok", "post"), "User-Agent": USER_AGENT, Accept: "application/json" },
body: form,
});
const body = await resp.json().catch(async () => ({ raw: await resp.text().catch(() => "") }));
return { status: resp.status, body };
}
async function callMetaEndpoint(prompt: string, orientation: string, token: string, refImageBase64?: string, refImageMime?: string) {
const form = new FormData();
form.append("prompt", prompt);
form.append("orientation", orientation);
form.append("num_result", "1");
if (refImageBase64 && refImageMime) {
const blob = base64ToBlob(refImageBase64, refImageMime);
form.append("files", blob, "reference.jpg");
}
const resp = await fetch(`${GEMINIGEN_BASE}/meta_ai/generate`, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "x-guard-id": generateGuardId("/api/meta_ai/generate", "post"), "User-Agent": USER_AGENT, Accept: "application/json" },
body: form,
});
const body = await resp.json().catch(async () => ({ raw: await resp.text().catch(() => "") }));
return { status: resp.status, body };
}
async function callImagenEndpoint(model: string, prompt: string, aspectRatio: string, style: string, token: string, refImageBase64?: string, refImageMime?: string, resolution?: string) {
const form = new FormData();
form.append("prompt", prompt);
form.append("model", model);
form.append("aspect_ratio", aspectRatio);
form.append("output_format", "jpg");
if (resolution) form.append("resolution", resolution);
if (refImageBase64 && refImageMime) {
const blob = base64ToBlob(refImageBase64, refImageMime);
form.append("files", blob, "reference.jpg");
}
const resp = await fetch(`${GEMINIGEN_BASE}/generate_image`, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "x-guard-id": generateGuardId("/api/generate_image", "post"), "User-Agent": USER_AGENT, Accept: "application/json" },
body: form,
});
const body = await resp.json().catch(async () => ({ raw: await resp.text().catch(() => "") }));
return { status: resp.status, body };
}
// โโ Credit helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
async function getConfigVal(key: string): Promise<string | null> {
const rows = await db.select({ value: configTable.value }).from(configTable).where(eq(configTable.key, key)).limit(1);
return rows[0]?.value ?? null;
}
async function checkAndDeductCredits(userId: number, cost: number, description: string): Promise<{ ok: boolean; balance?: number }> {
const enabled = await getConfigVal("enable_credits");
if (enabled !== "true") return { ok: true };
const [user] = await db.select({ credits: usersTable.credits }).from(usersTable).where(eq(usersTable.id, userId)).limit(1);
if (!user) return { ok: false };
if (user.credits < cost) return { ok: false, balance: user.credits };
const [updated] = await db
.update(usersTable)
.set({ credits: sql`${usersTable.credits} - ${cost}` })
.where(eq(usersTable.id, userId))
.returning({ credits: usersTable.credits });
await db.insert(creditTransactionsTable).values({
userId,
amount: -cost,
type: "spend",
description,
});
return { ok: true, balance: updated.credits };
}
router.post("/generate", optionalJwtAuth, async (req, res) => {
const bodyResult = GenerateImageBody.safeParse(req.body);
if (!bodyResult.success) {
return res.status(400).json({ error: "VALIDATION_ERROR", message: "Invalid request body" });
}
const {
prompt,
style = "realistic",
aspectRatio = "1:1",
model = "grok",
resolution,
referenceImageBase64,
referenceImageMime,
isPrivate = false,
} = bodyResult.data as any;
const userId: number | null = (req as any).jwtUserId ?? null;
// โโ Credits check โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if (userId !== null) {
const costStr = await getConfigVal("image_gen_cost");
const cost = Number(costStr) || 0;
if (cost > 0) {
const creditResult = await checkAndDeductCredits(userId, cost, `ๅ็็ๆ๏ผ${model}๏ผ`);
if (!creditResult.ok) {
return res.status(402).json({
error: "INSUFFICIENT_CREDITS",
message: `้ปๆธไธ่ถณ๏ผๆญคๆไฝ้่ฆ ${cost} ้ป`,
balance: creditResult.balance ?? 0,
});
}
}
}
const stylePrompt = style === "none" ? "" : (STYLE_PROMPTS[style] || "");
const fullPrompt = stylePrompt ? `${prompt}, ${stylePrompt}` : prompt;
const orientation = ORIENTATION_MAP[aspectRatio] || "square";
const isImagenModel = model === "imagen-pro" || model === "imagen-4" || model === "imagen-flash" || model === "nano-banana-pro" || model === "nano-banana-2";
const apiModelId = isImagenModel ? model : model;
let imageUrl = "";
let usedFallback = false;
let fallbackReason = "";
let responseStatus = 0;
let responseBody: unknown = {};
let pollResult: Record<string, unknown> = {};
const startTime = Date.now();
// โโ Pool-aware token selection โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const failedPoolIds: number[] = [];
let currentAccountId: number | null = null;
async function pickToken(): Promise<string | null> {
const poolEntry = await getPoolToken(failedPoolIds);
if (poolEntry) {
currentAccountId = poolEntry.accountId;
return poolEntry.token;
}
currentAccountId = null;
return getValidBearerToken();
}
async function handleTokenExpiry(): Promise<string | null> {
if (currentAccountId !== null) {
const refreshed = await tryRefreshPoolAccount(currentAccountId);
if (refreshed) return refreshed;
failedPoolIds.push(currentAccountId);
const next = await getPoolToken(failedPoolIds);
if (next) {
currentAccountId = next.accountId;
return next.token;
}
}
return refreshAccessToken();
}
let token = await pickToken();
const requestInfo = {
url: isImagenModel
? `${GEMINIGEN_BASE}/generate_image`
: model === "meta"
? `${GEMINIGEN_BASE}/meta_ai/generate`
: `${GEMINIGEN_BASE}/imagen/grok`,
model: isImagenModel ? apiModelId : model,
fields: isImagenModel
? { prompt: fullPrompt, model: apiModelId, aspect_ratio: aspectRatio, output_format: "jpg", ...(resolution ? { resolution } : {}), hasReferenceImage: !!referenceImageBase64 }
: { prompt: fullPrompt, orientation, num_result: "1", hasReferenceImage: !!referenceImageBase64 },
};
try {
if (!token) throw new Error("ๆช่จญๅฎ API Token๏ผ่ซๅฐ่จญๅฎ้ ้ข่ผธๅ
ฅ token");
let result: { status: number; body: unknown };
if (model === "grok") {
result = await callGrokEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
} else if (model === "meta") {
result = await callMetaEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
} else {
result = await callImagenEndpoint(apiModelId, fullPrompt, aspectRatio, style, token, referenceImageBase64, referenceImageMime, resolution);
}
if (result.status === 401) {
const newToken = await handleTokenExpiry();
if (!newToken) throw new Error("Token ๅทฒ้ๆไธ็กๆณ่ชๅๅทๆฐ");
token = newToken;
if (model === "grok") result = await callGrokEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
else if (model === "meta") result = await callMetaEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
else result = await callImagenEndpoint(apiModelId, fullPrompt, aspectRatio, style, token, referenceImageBase64, referenceImageMime, resolution);
}
responseStatus = result.status;
responseBody = result.body;
const data = result.body as { uuid?: string; base64_images?: string; generated_image?: Array<{ image_url?: string }>; detail?: { error_code?: string; error_message?: string } };
const errMsg = (data?.detail?.error_message || "").toLowerCase();
const isTokenExpired = result.status === 401 || result.status === 403
|| data?.detail?.error_code === "TOKEN_EXPIRED"
|| errMsg.includes("expired") || errMsg.includes("token");
if (isTokenExpired) {
const newToken = await handleTokenExpiry();
if (!newToken) throw new Error("Token ๅทฒ้ๆไธ็กๆณ่ชๅๅทๆฐ๏ผ่ซ้ๆฐๅๅพ");
token = newToken;
let retryResult: { status: number; body: unknown };
if (model === "grok") retryResult = await callGrokEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
else if (model === "meta") retryResult = await callMetaEndpoint(fullPrompt, orientation, token, referenceImageBase64, referenceImageMime);
else retryResult = await callImagenEndpoint(apiModelId, fullPrompt, aspectRatio, style, token, referenceImageBase64, referenceImageMime, resolution);
responseStatus = retryResult.status;
responseBody = retryResult.body;
Object.assign(data, retryResult.body);
}
if (!result.status.toString().startsWith("2") && !isTokenExpired) {
const msg = (data as any)?.detail?.error_message || (data as any)?.detail?.error_code || `HTTP ${result.status}`;
throw new Error(`API ้ฏ่ชค๏ผ${msg}`);
}
const finalData = responseBody as typeof data;
if ((finalData as any)?.detail?.error_code && (finalData as any)?.detail?.error_code !== "TOKEN_EXPIRED") {
const msg = (finalData as any)?.detail?.error_message || (finalData as any)?.detail?.error_code;
throw new Error(`API ้ฏ่ชค๏ผ${msg}`);
}
if (data.base64_images) {
imageUrl = `data:image/png;base64,${data.base64_images}`;
pollResult = { type: "immediate_base64" };
} else if (data.generated_image?.[0]?.image_url) {
imageUrl = data.generated_image[0].image_url;
pollResult = { type: "immediate_url" };
} else if (data.uuid) {
pollResult.uuid = data.uuid;
const polledUrl = await pollForImage(data.uuid, token);
if (!polledUrl) throw new Error("ๅ็็ๆ้พๆๆๆช่ฟๅ็ตๆ");
imageUrl = polledUrl;
pollResult.imageUrl = imageUrl;
pollResult.status = "completed";
} else {
throw new Error("API ๆช่ฟๅๅ็ๆไปปๅ UUID");
}
} catch (err: unknown) {
const errMsg = err instanceof Error ? err.message : String(err);
req.log.warn({ err }, "Image generation failed, using fallback");
usedFallback = true;
fallbackReason = errMsg;
const fallbackSizes: Record<string, string> = {
"1:1": "1024/1024", "16:9": "1344/768", "9:16": "768/1344",
"4:3": "1152/896", "3:4": "896/1152", "2:3": "768/1152", "3:2": "1152/768",
};
const seed = Math.floor(Math.random() * 1000000);
imageUrl = `https://picsum.photos/seed/${seed}/${fallbackSizes[aspectRatio] || "1024/1024"}`;
}
const durationMs = Date.now() - startTime;
const isTokenError = usedFallback && (
fallbackReason?.includes("Token ๅทฒ้ๆ") ||
fallbackReason?.includes("็กๆณ่ชๅๅทๆฐ") ||
fallbackReason?.includes("ๆช่จญๅฎ API Token") ||
fallbackReason?.includes("REFRESH_TOKEN_EXPIRED")
);
// Mark the pool account as used (after successful generation)
if (currentAccountId !== null) {
markAccountUsed(currentAccountId).catch(() => {});
}
const [inserted] = await db
.insert(imagesTable)
.values({ imageUrl, prompt, style, aspectRatio, model, isPrivate: !!isPrivate, userId })
.returning();
res.json({
id: inserted.id,
imageUrl: inserted.imageUrl,
prompt: inserted.prompt,
style: inserted.style,
aspectRatio: inserted.aspectRatio,
model: inserted.model,
createdAt: inserted.createdAt.toISOString(),
...(usedFallback ? { error: fallbackReason, tokenExpired: isTokenError } : {}),
apiDebug: {
requestUrl: requestInfo.url,
requestMethod: "POST",
requestContentType: "multipart/form-data",
requestHeaders: {
Authorization: token ? "Bearer ****" : "(็ก Token)",
"User-Agent": USER_AGENT,
Accept: "application/json",
},
requestBody: requestInfo.fields,
responseStatus,
responseBody,
pollResult,
durationMs,
usedFallback,
...(fallbackReason ? { fallbackReason } : {}),
},
});
});
router.get("/history", optionalJwtAuth, async (req, res) => {
const paramsResult = GetImageHistoryQueryParams.safeParse({
limit: req.query.limit ? Number(req.query.limit) : 20,
offset: req.query.offset ? Number(req.query.offset) : 0,
});
const { limit = 20, offset = 0 } = paramsResult.success ? paramsResult.data : {};
const currentUserId: number | null = (req as any).jwtUserId ?? null;
// Visibility filter:
// - Public images are always visible
// - Private images are only visible to their owner
const visibilityFilter = currentUserId
? or(eq(imagesTable.isPrivate, false), and(eq(imagesTable.isPrivate, true), eq(imagesTable.userId, currentUserId)))
: eq(imagesTable.isPrivate, false);
const [images, [{ value: total }]] = await Promise.all([
db.select().from(imagesTable).where(visibilityFilter).orderBy(desc(imagesTable.createdAt)).limit(limit).offset(offset),
db.select({ value: count() }).from(imagesTable).where(visibilityFilter),
]);
res.json({
images: images.map((img) => ({
id: img.id,
imageUrl: img.imageUrl,
prompt: img.prompt,
style: img.style,
aspectRatio: img.aspectRatio,
model: img.model,
isPrivate: img.isPrivate,
userId: img.userId,
createdAt: img.createdAt.toISOString(),
})),
total: Number(total),
});
});
router.delete("/:id", async (req, res) => {
const paramsResult = DeleteImageParams.safeParse({ id: Number(req.params.id) });
if (!paramsResult.success) {
return res.status(400).json({ error: "INVALID_ID", message: "Invalid image ID" });
}
const deleted = await db.delete(imagesTable).where(eq(imagesTable.id, paramsResult.data.id)).returning();
if (deleted.length === 0) {
return res.status(404).json({ error: "NOT_FOUND", message: "Image not found" });
}
res.json({ success: true, message: "Image deleted successfully" });
});
export default router;
|