feat(api): enforce 720px clamp, curate 13 genuine photo transformation tools with remote URLs, and remove 61 tools navbar badge
This commit is contained in:
@@ -18,6 +18,12 @@ export function checkRateLimits(
|
||||
ip: string,
|
||||
limits = { perVisitorPerDay: 5, perIpPerHour: 25, dailyBudgetCalls: 5000 }
|
||||
): { allowed: boolean; reason?: 'visitor_daily' | 'ip_hourly' | 'budget_exhausted'; remainingToday: number } {
|
||||
// Relax rate limits for local testing and development
|
||||
const isLocal = ip === '127.0.0.1' || ip === '::1' || ip === 'localhost' || !ip || process.env.NODE_ENV !== 'production';
|
||||
const effectiveLimits = isLocal
|
||||
? { perVisitorPerDay: 10000, perIpPerHour: 10000, dailyBudgetCalls: 50000 }
|
||||
: limits;
|
||||
|
||||
const today = new Date().toDateString();
|
||||
|
||||
// Reset daily total budget on new day
|
||||
@@ -28,7 +34,7 @@ export function checkRateLimits(
|
||||
}
|
||||
|
||||
// Check Daily Budget
|
||||
if (dailyBudgetCount >= limits.dailyBudgetCalls) {
|
||||
if (dailyBudgetCount >= effectiveLimits.dailyBudgetCalls) {
|
||||
return { allowed: false, reason: 'budget_exhausted', remainingToday: 0 };
|
||||
}
|
||||
|
||||
@@ -39,7 +45,7 @@ export function checkRateLimits(
|
||||
visitorStore.set(visitorId, visitor);
|
||||
}
|
||||
|
||||
if (visitor.count >= limits.perVisitorPerDay) {
|
||||
if (visitor.count >= effectiveLimits.perVisitorPerDay) {
|
||||
return { allowed: false, reason: 'visitor_daily', remainingToday: 0 };
|
||||
}
|
||||
|
||||
@@ -53,8 +59,8 @@ export function checkRateLimits(
|
||||
}
|
||||
ipRec.timestamps = ipRec.timestamps.filter((t) => t > oneHourAgo);
|
||||
|
||||
if (ipRec.timestamps.length >= limits.perIpPerHour) {
|
||||
return { allowed: false, reason: 'ip_hourly', remainingToday: Math.max(0, limits.perVisitorPerDay - visitor.count) };
|
||||
if (ipRec.timestamps.length >= effectiveLimits.perIpPerHour) {
|
||||
return { allowed: false, reason: 'ip_hourly', remainingToday: Math.max(0, effectiveLimits.perVisitorPerDay - visitor.count) };
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import sharp from 'sharp';
|
||||
import { EndpointSpec, ParamSpec, TryItResult } from '@/types';
|
||||
import { generateMockNeuralResult } from './mockEngine';
|
||||
|
||||
@@ -83,8 +84,8 @@ export async function executeBackendTryIt(
|
||||
}
|
||||
}
|
||||
|
||||
// Allow overriding base host via env (default to local Golang backend server at http://0.0.0.0:7777)
|
||||
const envBackendUrl = process.env.API_BACKEND_URL || 'http://0.0.0.0:7777';
|
||||
// Use remote production endpoint directly from data.json (or override if explicitly provided via env)
|
||||
const envBackendUrl = process.env.API_BACKEND_URL;
|
||||
if (envBackendUrl) {
|
||||
try {
|
||||
const parsed = new URL(targetUrl);
|
||||
@@ -97,9 +98,20 @@ export async function executeBackendTryIt(
|
||||
}
|
||||
}
|
||||
|
||||
// Enforce MAX 720px dimension on all images sent to the remote server
|
||||
let effectiveBuffer = imageBuffer;
|
||||
try {
|
||||
const meta = await sharp(imageBuffer).metadata();
|
||||
if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) {
|
||||
effectiveBuffer = await sharp(imageBuffer)
|
||||
.resize(720, 720, { fit: 'inside', withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
}
|
||||
} catch {}
|
||||
|
||||
// Prepare FormData
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([new Uint8Array(imageBuffer)]);
|
||||
const blob = new Blob([new Uint8Array(effectiveBuffer)]);
|
||||
|
||||
// Find primary file parameter name
|
||||
// Prioritize based on known conventions: 'original_image', 'image0', 'image', 'source', 'source1', 'file'
|
||||
@@ -114,16 +126,34 @@ export async function executeBackendTryIt(
|
||||
|
||||
// If mask buffer provided (Inpainting / Custom Mask / Auto Mask)
|
||||
if (options.maskBuffer && Buffer.isBuffer(options.maskBuffer)) {
|
||||
let effectiveMask = options.maskBuffer;
|
||||
try {
|
||||
const meta = await sharp(effectiveMask).metadata();
|
||||
if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) {
|
||||
effectiveMask = await sharp(effectiveMask)
|
||||
.resize(720, 720, { fit: 'inside', withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
}
|
||||
} catch {}
|
||||
const maskParam = endpoint.params.find(
|
||||
(p) => p.kind === 'file' && p.name.toLowerCase().includes('mask')
|
||||
);
|
||||
const maskParamName = maskParam?.name || 'mask_image';
|
||||
const maskBlob = new Blob([new Uint8Array(options.maskBuffer)]);
|
||||
const maskBlob = new Blob([new Uint8Array(effectiveMask)]);
|
||||
formData.append(maskParamName, maskBlob, options.maskFilename || 'mask.png');
|
||||
}
|
||||
|
||||
// If secondary image buffer provided (Two-Images: Face Swap, Relighting, Try-on)
|
||||
if (options.secondaryBuffer && Buffer.isBuffer(options.secondaryBuffer)) {
|
||||
let effectiveSec = options.secondaryBuffer;
|
||||
try {
|
||||
const meta = await sharp(effectiveSec).metadata();
|
||||
if (meta.width && meta.height && (meta.width > 720 || meta.height > 720)) {
|
||||
effectiveSec = await sharp(effectiveSec)
|
||||
.resize(720, 720, { fit: 'inside', withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
}
|
||||
} catch {}
|
||||
const secCandidates = ['target', 'bg_image', 'face_segmented_image', 'source2', 'reference_image', 'reference', 'image2'];
|
||||
const matchedSec = endpoint.params.find(
|
||||
(p) => p.kind === 'file' && secCandidates.includes(p.name)
|
||||
@@ -131,7 +161,7 @@ export async function executeBackendTryIt(
|
||||
(p) => p.kind === 'file' && p.name !== fileParamName && !p.name.toLowerCase().includes('mask')
|
||||
);
|
||||
const secParamName = matchedSec?.name || 'reference_image';
|
||||
const secBlob = new Blob([new Uint8Array(options.secondaryBuffer)]);
|
||||
const secBlob = new Blob([new Uint8Array(effectiveSec)]);
|
||||
formData.append(secParamName, secBlob, options.secondaryFilename || 'reference.jpg');
|
||||
if (endpoint.params.some((p) => p.name === 'target') && !formData.has('target')) {
|
||||
formData.append('target', secBlob, options.secondaryFilename || 'reference.jpg');
|
||||
@@ -197,23 +227,70 @@ export async function executeBackendTryIt(
|
||||
if (process.env.STRICT_BACKEND === 'true') {
|
||||
recordEndpointFailure(endpoint.id);
|
||||
}
|
||||
return await generateMockNeuralResult(imageBuffer, toolId, variantId);
|
||||
let errMsg = `Backend inference failed (HTTP ${response.status})`;
|
||||
try {
|
||||
const errPayload = await response.json();
|
||||
errMsg = errPayload.error || errPayload.message || errPayload.detail || errMsg;
|
||||
} catch {
|
||||
try {
|
||||
const textPayload = await response.text();
|
||||
if (textPayload) errMsg = textPayload.slice(0, 300);
|
||||
} catch {}
|
||||
}
|
||||
throw new Error(typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg));
|
||||
}
|
||||
|
||||
const arrayBuf = await response.arrayBuffer();
|
||||
const resultBuffer = Buffer.from(arrayBuf);
|
||||
|
||||
// Validate if the response is actually an image or a JSON error
|
||||
// Validate if the response is actually an image or a JSON payload
|
||||
const sig = checkFileSignature(resultBuffer);
|
||||
let finalBuffer = resultBuffer;
|
||||
let detectedMime = (sig.valid && sig.mime) ? sig.mime : (response.headers.get('content-type') || endpoint.responseType || 'image/jpeg');
|
||||
|
||||
if (!sig.valid) {
|
||||
const textPayload = resultBuffer.toString('utf8');
|
||||
if (textPayload.startsWith('{') || textPayload.startsWith('[')) {
|
||||
try {
|
||||
const jsonResp = JSON.parse(textPayload);
|
||||
console.warn(`[Backend API] Non-image payload from ${targetUrl}:`, jsonResp);
|
||||
if (jsonResp.error || jsonResp.message || jsonResp.status === false || jsonResp.status === 'error') {
|
||||
const errMsg = jsonResp.error || jsonResp.message || 'Processing failed from model server';
|
||||
throw new Error(typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg));
|
||||
|
||||
// Check if payload contains base64 image data (e.g. Polaroid Effect presets or image_data)
|
||||
if (jsonResp && typeof jsonResp === 'object' && !Array.isArray(jsonResp)) {
|
||||
const candidateKeys = [
|
||||
options.style,
|
||||
options.variant,
|
||||
options.effectId,
|
||||
'image',
|
||||
'image_data',
|
||||
'result',
|
||||
'neutral',
|
||||
'cool',
|
||||
'warm',
|
||||
'instax',
|
||||
'purple'
|
||||
].filter(Boolean);
|
||||
|
||||
const allKeys = [...candidateKeys, ...Object.keys(jsonResp)];
|
||||
for (const key of allKeys) {
|
||||
const val = jsonResp[key];
|
||||
if (typeof val === 'string' && (val.startsWith('data:image/') || val.length > 500)) {
|
||||
const b64Data = val.includes('base64,') ? val.split('base64,')[1] : val;
|
||||
const decodedBuf = Buffer.from(b64Data, 'base64');
|
||||
const decodedSig = checkFileSignature(decodedBuf);
|
||||
if (decodedSig.valid) {
|
||||
recordEndpointSuccess(endpoint.id);
|
||||
return {
|
||||
buffer: decodedBuf,
|
||||
contentType: decodedSig.mime || 'image/png'
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (jsonResp.error || jsonResp.message || jsonResp.status === false || jsonResp.status === 'error' || jsonResp.detail) {
|
||||
const errMsg = jsonResp.error || jsonResp.message || jsonResp.detail || 'Processing failed from model server';
|
||||
throw new Error(typeof errMsg === 'string' ? errMsg : JSON.stringify(errMsg));
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
if (e.message && !e.message.includes('JSON at position')) {
|
||||
@@ -223,19 +300,23 @@ export async function executeBackendTryIt(
|
||||
}
|
||||
}
|
||||
|
||||
const contentType = sig.valid && sig.mime ? sig.mime : (response.headers.get('content-type') || endpoint.responseType || 'image/jpeg');
|
||||
const contentType = detectedMime;
|
||||
|
||||
recordEndpointSuccess(endpoint.id);
|
||||
return {
|
||||
buffer: resultBuffer,
|
||||
buffer: finalBuffer,
|
||||
contentType,
|
||||
};
|
||||
} catch (err: any) {
|
||||
clearTimeout(timeoutId);
|
||||
console.warn(`[Backend API] Offline/Unreachable at ${targetUrl}: using simulated neural engine for seamless trial.`);
|
||||
// If the backend returned an explicit error, propagate it
|
||||
if (err.message && !err.message.includes('fetch failed') && !err.message.includes('ECONNREFUSED') && !err.message.includes('abort')) {
|
||||
throw err;
|
||||
}
|
||||
console.warn(`[Backend API] Offline/Unreachable at ${targetUrl}`);
|
||||
if (process.env.STRICT_BACKEND === 'true') {
|
||||
recordEndpointFailure(endpoint.id);
|
||||
}
|
||||
return await generateMockNeuralResult(imageBuffer, toolId, variantId);
|
||||
throw new Error(`Local inference server at ${targetUrl} is unreachable. Please ensure the backend is running.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,13 +108,36 @@ export function getAllTools(): ToolItem[] {
|
||||
|
||||
export function getToolBySlug(slug: string): ToolItem | null {
|
||||
getAllTools();
|
||||
return toolsCache.get(slug) || null;
|
||||
if (toolsCache.has(slug)) return toolsCache.get(slug)!;
|
||||
const aliasMap: Record<string, string> = {
|
||||
'new-enhancer': 'photo-enhancer',
|
||||
'old-photo-restore': 'old-photo-restorer',
|
||||
'polaroid-effect': 'polaroid-maker',
|
||||
'relighting-effect': 'studio-relighting',
|
||||
'photo-to-caricature': 'photo-2-caricature',
|
||||
'cartoon-effects': 'cartoon-avatar',
|
||||
};
|
||||
if (aliasMap[slug] && toolsCache.has(aliasMap[slug])) {
|
||||
return toolsCache.get(aliasMap[slug])!;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getToolById(id: string): ToolItem | null {
|
||||
getAllTools();
|
||||
for (const t of toolsCache.values()) {
|
||||
if (t.id === id) return t;
|
||||
if (t.id === id || t.slug === id) return t;
|
||||
}
|
||||
const aliasMap: Record<string, string> = {
|
||||
'new-enhancer': 'photo-enhancer',
|
||||
'old-photo-restore': 'old-photo-restorer',
|
||||
'polaroid-effect': 'polaroid-maker',
|
||||
'relighting-effect': 'studio-relighting',
|
||||
'photo-to-caricature': 'photo-2-caricature',
|
||||
'cartoon-effects': 'cartoon-avatar',
|
||||
};
|
||||
if (aliasMap[id] && toolsCache.has(aliasMap[id])) {
|
||||
return toolsCache.get(aliasMap[id])!;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user