2026-02-15 23:07:04 +01:00
|
|
|
/**
|
|
|
|
|
* Map data API — returns location-level markers for the map view.
|
|
|
|
|
* Each marker represents a unique city/country with averaged GPS coords.
|
|
|
|
|
* Cover asset IDs are encrypted for security.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
|
import { cookies } from 'next/headers';
|
2026-02-15 23:07:04 +01:00
|
|
|
import { getConfig } from '@/lib/config';
|
|
|
|
|
import { imageUrl } from '@/lib/urls';
|
2026-03-07 08:18:23 +01:00
|
|
|
import { isAuthenticated } from '@/lib/auth';
|
2026-03-08 18:55:45 +01:00
|
|
|
import { getMapData } from '@/lib/mapService';
|
2026-03-13 11:41:56 +01:00
|
|
|
import { checkRateLimit } from '@/lib/rate-limit';
|
2026-02-15 23:07:04 +01:00
|
|
|
|
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
|
2026-03-13 11:41:56 +01:00
|
|
|
export async function GET(request: NextRequest) {
|
|
|
|
|
// Rate limit: 30 requests/minute per IP
|
|
|
|
|
const ip =
|
|
|
|
|
request.headers.get('x-forwarded-for')?.split(',')[0].trim() ??
|
|
|
|
|
request.headers.get('x-real-ip') ??
|
|
|
|
|
'127.0.0.1';
|
|
|
|
|
const rl = checkRateLimit(ip, 30);
|
|
|
|
|
if (!rl.success) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'Too many requests' },
|
|
|
|
|
{
|
|
|
|
|
status: 429,
|
|
|
|
|
headers: {
|
|
|
|
|
'Retry-After': String(Math.ceil((rl.resetAt - Date.now()) / 1000)),
|
|
|
|
|
'X-RateLimit-Limit': '30',
|
|
|
|
|
'X-RateLimit-Remaining': '0',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
const config = getConfig();
|
2026-02-15 23:07:04 +01:00
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
if (!config.map) {
|
|
|
|
|
return NextResponse.json({ error: 'Map is not enabled' }, { status: 404 });
|
|
|
|
|
}
|
2026-02-15 23:07:04 +01:00
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
const cookieStore = await cookies();
|
|
|
|
|
const getCookie = (name: string) => cookieStore.get(name)?.value;
|
2026-02-15 23:07:04 +01:00
|
|
|
|
2026-03-08 18:55:45 +01:00
|
|
|
const locations = await getMapData();
|
2026-03-07 08:18:23 +01:00
|
|
|
|
|
|
|
|
// Filter locations and albums based on auth
|
|
|
|
|
const publicLocations = locations
|
|
|
|
|
.map((loc) => {
|
|
|
|
|
// Only keep albums the user is allowed to see
|
|
|
|
|
const allowedAlbums = loc.albums.filter((a) => {
|
|
|
|
|
if (!a.subpageSlug) return true; // Standalone albums are public
|
|
|
|
|
return isAuthenticated(a.subpageSlug, getCookie);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (allowedAlbums.length === 0) return null;
|
|
|
|
|
|
|
|
|
|
return {
|
2026-02-15 23:07:04 +01:00
|
|
|
city: loc.city,
|
|
|
|
|
country: loc.country,
|
|
|
|
|
lat: loc.lat,
|
|
|
|
|
lng: loc.lng,
|
2026-03-07 08:18:23 +01:00
|
|
|
photoCount: loc.photoCount, // Note: This count might be slightly off if some photos are in hidden albums
|
2026-02-15 23:07:04 +01:00
|
|
|
coverUrl: imageUrl(loc.coverAssetId, 'thumbnail'),
|
2026-03-07 08:18:23 +01:00
|
|
|
albums: allowedAlbums.map((a) => ({
|
|
|
|
|
name: a.name,
|
|
|
|
|
url: a.subpageSlug ? `/${a.subpageSlug}/${a.slug}` : `/${a.slug}`,
|
2026-02-15 23:07:04 +01:00
|
|
|
})),
|
2026-03-07 08:18:23 +01:00
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.filter((loc) => loc !== null);
|
2026-02-15 23:07:04 +01:00
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
return NextResponse.json(publicLocations, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Cache-Control': `private, no-cache, no-store, must-revalidate`, // Don't cache personalized map data publicly
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-02-15 23:07:04 +01:00
|
|
|
}
|