feat: add smart map view with location-level clustering

- /map page with Leaflet (CartoDB dark tiles)
- Photos clustered by city/country (not per-photo)
- /api/map endpoint with encrypted cover asset IDs
- Configurable via gallery.yaml (map: true/false)
- Conditional nav link in header
- CSP updated for Leaflet CDN and tile servers
- Theme-aware popups with cover thumbnails and album links
This commit is contained in:
ralksta
2026-02-15 23:07:04 +01:00
parent 424f7558c6
commit 62a7b178d6
13 changed files with 555 additions and 3 deletions

View File

@@ -123,6 +123,7 @@ interface GalleryYaml {
hero?: string | string[];
albums?: string[];
exifOnHover?: boolean;
map?: boolean;
theme?:
| string
| {
@@ -236,6 +237,8 @@ export interface AppConfig {
theme: ThemeConfig;
/** Footer social links config. */
footer: FooterConfig | null;
/** Show the /map page. Default: false. */
map: boolean;
cacheTtl: number;
rateLimitRpm: number;
}
@@ -392,6 +395,7 @@ export function getConfig(): AppConfig {
website: gallery.footer.website,
}
: null,
map: gallery.map === true,
cacheTtl: env.CACHE_TTL * 1000,
rateLimitRpm: env.RATE_LIMIT_RPM,
};

View File

@@ -46,6 +46,8 @@ export interface ImmichExifInfo {
iso: number | null;
exifImageWidth: number | null;
exifImageHeight: number | null;
latitude: number | null;
longitude: number | null;
city: string | null;
state: string | null;
country: string | null;
@@ -53,6 +55,17 @@ export interface ImmichExifInfo {
description: string | null;
}
/** A clustered map marker — one per unique city/country. */
export interface MapLocation {
city: string;
country: string;
lat: number;
lng: number;
photoCount: number;
coverAssetId: string;
albums: { name: string; slug: string; subpageSlug?: string }[];
}
export type ImageSize = 'thumbnail' | 'preview' | 'original';
/** Enriched subpage with album metadata (for rendering cards). */
@@ -312,6 +325,76 @@ class ImmichClient {
return asset;
}
/**
* Aggregate all geotagged photos into location-level markers.
* Returns one entry per unique city+country with averaged lat/lng.
*/
async getMapData(): Promise<MapLocation[]> {
const cacheKey = 'map-data';
const cached = cache.get<MapLocation[]>(cacheKey);
if (cached) return cached;
const albums = await this.getAlbums();
// Build a lookup: album ID → { name, slug, subpageSlug? }
const albumMeta = new Map<string, { name: string; slug: string; subpageSlug?: string }>();
for (const a of albums) {
// Check if this album belongs to a subpage
const sp = this.config.subpages.find((s) => s.albumIds.includes(a.id));
albumMeta.set(a.id, { name: a.albumName, slug: a.slug, subpageSlug: sp?.slug });
}
// Fetch full album data (with assets) for each
const fullAlbums = await Promise.all(albums.map((a) => this.getAlbum(a.id)));
// Bucket assets by city+country
const buckets = new Map<
string,
{ lats: number[]; lngs: number[]; count: number; coverAssetId: string; albumIds: Set<string> }
>();
for (const album of fullAlbums) {
if (!album) continue;
for (const asset of album.assets) {
const exif = asset.exifInfo;
if (!exif?.latitude || !exif?.longitude || !exif?.city || !exif?.country) continue;
const key = `${exif.city}|${exif.country}`;
let bucket = buckets.get(key);
if (!bucket) {
bucket = { lats: [], lngs: [], count: 0, coverAssetId: asset.id, albumIds: new Set() };
buckets.set(key, bucket);
}
bucket.lats.push(exif.latitude);
bucket.lngs.push(exif.longitude);
bucket.count++;
bucket.albumIds.add(album.id);
}
}
// Convert buckets to MapLocation[]
const locations: MapLocation[] = [];
for (const [key, bucket] of buckets) {
const [city, country] = key.split('|');
const lat = bucket.lats.reduce((a, b) => a + b, 0) / bucket.lats.length;
const lng = bucket.lngs.reduce((a, b) => a + b, 0) / bucket.lngs.length;
locations.push({
city,
country,
lat,
lng,
photoCount: bucket.count,
coverAssetId: bucket.coverAssetId,
albums: [...bucket.albumIds]
.map((id) => albumMeta.get(id))
.filter((m): m is NonNullable<typeof m> => !!m),
});
}
cache.set(cacheKey, locations, this.config.cacheTtl);
return locations;
}
/**
* Check if the Immich server is reachable.
*/