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-04-24 22:12:42 +02:00
|
|
|
import { checkRateLimit, getClientIp } from '@/lib/rate-limit';
|
2026-02-15 23:07:04 +01:00
|
|
|
|
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
|
2026-07-20 18:57:57 +02:00
|
|
|
/** Map data is cached client-side, so a low limit is plenty. */
|
|
|
|
|
const MAP_RPM = 120;
|
|
|
|
|
|
2026-03-13 11:41:56 +01:00
|
|
|
export async function GET(request: NextRequest) {
|
2026-04-24 22:12:42 +02:00
|
|
|
const ip = getClientIp(request);
|
2026-07-20 18:57:57 +02:00
|
|
|
const rl = checkRateLimit(`map:${ip}`, MAP_RPM);
|
2026-03-13 11:41:56 +01:00
|
|
|
if (!rl.success) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'Too many requests' },
|
|
|
|
|
{
|
|
|
|
|
status: 429,
|
|
|
|
|
headers: {
|
|
|
|
|
'Retry-After': String(Math.ceil((rl.resetAt - Date.now()) / 1000)),
|
2026-03-13 12:55:07 +01:00
|
|
|
'X-RateLimit-Limit': '120',
|
2026-03-13 11:41:56 +01:00
|
|
|
'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
|
|
|
|
2026-07-20 18:57:57 +02:00
|
|
|
// Filter locations and albums based on auth.
|
|
|
|
|
// An album is visible only if BOTH its subpage gate (if any) and its own
|
|
|
|
|
// album-level password gate are satisfied. Standalone albums are NOT
|
|
|
|
|
// implicitly public — they can carry their own password.
|
|
|
|
|
const subpageAuthCache = new Map<string, boolean>();
|
|
|
|
|
const albumAuthCache = new Map<string, boolean>();
|
|
|
|
|
|
|
|
|
|
const isSubpageAllowed = (slug: string): boolean => {
|
|
|
|
|
const cached = subpageAuthCache.get(slug);
|
|
|
|
|
if (cached !== undefined) return cached;
|
|
|
|
|
const result = isAuthenticated(slug, getCookie);
|
|
|
|
|
subpageAuthCache.set(slug, result);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isAlbumAllowed = (albumId: string): boolean => {
|
|
|
|
|
const cached = albumAuthCache.get(albumId);
|
|
|
|
|
if (cached !== undefined) return cached;
|
|
|
|
|
const result = isAuthenticated(albumId, getCookie, 'album');
|
|
|
|
|
albumAuthCache.set(albumId, result);
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
const publicLocations = locations
|
|
|
|
|
.map((loc) => {
|
2026-07-20 18:57:57 +02:00
|
|
|
const allowedAlbums = loc.albums.filter(
|
|
|
|
|
(a) => (!a.subpageSlug || isSubpageAllowed(a.subpageSlug)) && isAlbumAllowed(a.id),
|
|
|
|
|
);
|
2026-03-07 08:18:23 +01:00
|
|
|
|
|
|
|
|
if (allowedAlbums.length === 0) return null;
|
|
|
|
|
|
2026-07-20 18:57:57 +02:00
|
|
|
// Aggregate over visible albums only, so neither the marker position,
|
|
|
|
|
// the photo count, nor the cover asset reveals a protected album.
|
|
|
|
|
const photoCount = allowedAlbums.reduce((sum, a) => sum + a.photoCount, 0);
|
|
|
|
|
const latSum = allowedAlbums.reduce((sum, a) => sum + a.latSum, 0);
|
|
|
|
|
const lngSum = allowedAlbums.reduce((sum, a) => sum + a.lngSum, 0);
|
|
|
|
|
|
2026-03-07 08:18:23 +01:00
|
|
|
return {
|
2026-02-15 23:07:04 +01:00
|
|
|
city: loc.city,
|
|
|
|
|
country: loc.country,
|
2026-07-20 18:57:57 +02:00
|
|
|
lat: latSum / photoCount,
|
|
|
|
|
lng: lngSum / photoCount,
|
|
|
|
|
photoCount,
|
|
|
|
|
coverUrl: imageUrl(allowedAlbums[0].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: {
|
2026-03-13 12:55:07 +01:00
|
|
|
// Private 5-minute browser cache: reduces re-fetches during a session
|
|
|
|
|
// while still preventing shared/CDN caching of personalised data.
|
|
|
|
|
'Cache-Control': 'private, max-age=300, must-revalidate',
|
2026-03-07 08:18:23 +01:00
|
|
|
},
|
|
|
|
|
});
|
2026-02-15 23:07:04 +01:00
|
|
|
}
|