2026-02-14 00:59:57 +01:00
|
|
|
/**
|
|
|
|
|
* Image proxy route — serves Immich assets to the browser
|
|
|
|
|
* without exposing the Immich server or API key.
|
|
|
|
|
*
|
|
|
|
|
* Usage: GET /api/image/:token?size=thumbnail|preview|original
|
2026-02-14 10:30:27 +01:00
|
|
|
* GET /api/image/:token?w=640&q=75 (for next/image loader)
|
2026-02-14 00:59:57 +01:00
|
|
|
* The :token is an encoded asset ID (not a raw UUID).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { immich, ImageSize } from '@/lib/immich';
|
|
|
|
|
import { decodeAssetId } from '@/lib/tokens';
|
2026-02-14 10:30:27 +01:00
|
|
|
import { getConfig } from '@/lib/config';
|
|
|
|
|
import { checkRateLimit } from '@/lib/rate-limit';
|
2026-02-14 00:59:57 +01:00
|
|
|
|
|
|
|
|
const VALID_SIZES: ImageSize[] = ['thumbnail', 'preview', 'original'];
|
|
|
|
|
|
2026-02-14 10:30:27 +01:00
|
|
|
/**
|
|
|
|
|
* Map a requested pixel width to the best Immich size tier.
|
|
|
|
|
*/
|
|
|
|
|
function widthToSize(w: number): ImageSize {
|
2026-02-14 11:47:26 +01:00
|
|
|
if (w <= 250) return 'thumbnail';
|
|
|
|
|
if (w <= 1440) return 'preview';
|
|
|
|
|
return 'original';
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
|
|
|
// ── Rate limiting ──────────────────────────────────
|
2026-03-11 23:36:40 +01:00
|
|
|
// Security: Prioritize x-real-ip (set by trusted reverse proxies like nginx/Traefik)
|
|
|
|
|
// over x-forwarded-for which can be spoofed by clients.
|
|
|
|
|
// Note: request.ip was removed in Next.js 15+.
|
2026-03-11 23:32:32 +01:00
|
|
|
const ip =
|
|
|
|
|
request.headers.get('x-real-ip') ??
|
|
|
|
|
request.headers.get('x-forwarded-for')?.split(',')[0].trim() ??
|
|
|
|
|
'unknown';
|
2026-02-14 11:47:26 +01:00
|
|
|
const { success, remaining, resetAt } = checkRateLimit(ip, getConfig().rateLimitRpm);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (!success) {
|
|
|
|
|
const retryAfter = Math.ceil((resetAt - Date.now()) / 1000);
|
2026-03-07 09:42:33 +01:00
|
|
|
console.warn(`[Image API] ⚠️ Rate limit exceeded for IP: ${ip}. Retry after ${retryAfter}s`);
|
|
|
|
|
return NextResponse.json({ error: 'Too many requests' }, { status: 429 });
|
2026-02-14 11:47:26 +01:00
|
|
|
}
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
const { id: token } = await params;
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
// Decode the opaque token back to an Immich asset ID
|
|
|
|
|
const assetId = decodeAssetId(token);
|
|
|
|
|
if (!assetId) {
|
2026-03-07 09:42:33 +01:00
|
|
|
console.error(`[Image API] ❌ Invalid token: ${token.substring(0, 10)}...`);
|
2026-02-14 11:47:26 +01:00
|
|
|
return NextResponse.json({ error: 'Invalid token' }, { status: 400 });
|
|
|
|
|
}
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
// Determine size: prefer explicit ?size=, then infer from ?w=
|
|
|
|
|
const sizeParam = request.nextUrl.searchParams.get('size');
|
|
|
|
|
const widthParam = request.nextUrl.searchParams.get('w');
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
let size: ImageSize = 'preview';
|
|
|
|
|
if (sizeParam && VALID_SIZES.includes(sizeParam as ImageSize)) {
|
|
|
|
|
size = sizeParam as ImageSize;
|
|
|
|
|
} else if (widthParam) {
|
|
|
|
|
const w = parseInt(widthParam, 10);
|
|
|
|
|
if (!isNaN(w) && w > 0) {
|
|
|
|
|
size = widthToSize(w);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
2026-02-14 11:47:26 +01:00
|
|
|
}
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
const result = await immich.streamAsset(assetId, size);
|
|
|
|
|
if (!result) {
|
2026-03-07 09:42:33 +01:00
|
|
|
console.error(`[Image API] ❌ Asset not found in Immich: ${assetId} (Size: ${size})`);
|
2026-02-14 11:47:26 +01:00
|
|
|
return NextResponse.json({ error: 'Asset not found' }, { status: 404 });
|
|
|
|
|
}
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
const headers: Record<string, string> = {
|
2026-03-07 09:46:09 +01:00
|
|
|
'Content-Type': result.contentType.includes('application/octet-stream')
|
|
|
|
|
? 'image/jpeg'
|
|
|
|
|
: result.contentType,
|
2026-02-14 11:47:26 +01:00
|
|
|
// Images are immutable once uploaded to Immich — cache aggressively
|
|
|
|
|
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
|
|
|
'X-RateLimit-Remaining': String(remaining),
|
|
|
|
|
};
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (result.contentLength) {
|
|
|
|
|
headers['Content-Length'] = result.contentLength;
|
|
|
|
|
}
|
2026-02-14 00:59:57 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
return new NextResponse(result.stream, { headers });
|
2026-02-14 00:59:57 +01:00
|
|
|
}
|