fix(journey): handle large photo selections within the API limits

Allow larger JSON bodies on the draft-creation route, split oversized photo groups, and use the original geotagged times to assign photos between groups.
This commit is contained in:
Maurice
2026-11-30 17:06:03 +01:00
parent 21b8da53cf
commit f848446131
4 changed files with 64 additions and 9 deletions
+9 -1
View File
@@ -207,11 +207,16 @@ export async function buildApp(): Promise<INestApplication> {
* kilobytes, so those two routes are measured against TOUR_WRITE_MAX_BYTES.
* The planner's unsaved draft carries the same points and fixed legs (its
* contract caps it at TOUR_DRAFT_MAX_CHARS), so its write takes that ceiling
* too. Everything else keeps the tighter limit.
* too.
*
* Creating drafts from gallery photos sends the chosen groups together, with
* every photo ID plus titles and place names. Give this one route space for
* large galleries while keeping the tighter limit on ordinary writes.
*/
const bookBody = express.json({ limit: '8mb', verify: rawBodyKeeper });
const listFileBody = express.json({ limit: MAX_COLLECTION_FILE_BYTES * 2, verify: rawBodyKeeper });
const tourBody = express.json({ limit: TOUR_WRITE_MAX_BYTES, verify: rawBodyKeeper });
const photoDraftBody = express.json({ limit: '4mb', verify: rawBodyKeeper });
const json = express.json({ limit: '100kb', verify: rawBodyKeeper });
const urlencoded = express.urlencoded({ limit: '100kb', extended: true, verify: rawBodyKeeper });
const isMcp = (req: Request) => req.path === '/mcp' || req.path === '/mcp/';
@@ -227,6 +232,9 @@ export async function buildApp(): Promise<INestApplication> {
if (isBookWrite(req)) return bookBody(req, res, next);
if (isListFile(req)) return listFileBody(req, res, next);
if (isTourWrite(req)) return tourBody(req, res, next);
if (req.method === 'POST' && /^\/api\/journeys\/\d+\/entries\/from-photos\/?$/i.test(req.path)) {
return photoDraftBody(req, res, next);
}
return isMcp(req) ? next() : json(req, res, next);
});
instance.use(function urlencodedParser(req: Request, res: Response, next: NextFunction) {
@@ -1,6 +1,6 @@
import { haversineKm } from '../common/geo';
import { localParts, resolveTimeZone } from '../common/timezoneService';
import { getIntlLanguage, SUPPORTED_LANGUAGE_CODES } from '@trek/shared';
import { getIntlLanguage, JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP, SUPPORTED_LANGUAGE_CODES } from '@trek/shared';
/**
* The pure half of making journal entries from gallery photos (#1920): when
@@ -196,15 +196,20 @@ function clusterDay(day: LocalCapture[], thresholds: ClusterThresholds): LocalCa
return clusters;
}
walk(located, true);
// Fix the cutoffs before adding photos: an unlocated photo cannot move a
// location group's boundary for the next one. Both lists are in time order.
const boundaries = clusters.slice(1).map((next, index) => {
const previous = clusters[index] as LocalCapture[];
return ((previous.at(-1) as LocalCapture).instant + (next[0] as LocalCapture).instant) / 2;
});
let index = 0;
for (const photo of unlocated) {
let best: { distance: number; cluster: LocalCapture[] } | null = null;
for (const cluster of clusters) {
const first = (cluster[0] as LocalCapture).instant;
const last = (cluster[cluster.length - 1] as LocalCapture).instant;
const distance = photo.instant < first ? first - photo.instant : Math.max(0, photo.instant - last);
if (!best || distance < best.distance) best = { distance, cluster };
let boundary = boundaries[index];
while (boundary !== undefined && photo.instant > boundary) {
index++;
boundary = boundaries[index];
}
(best as { cluster: LocalCapture[] }).cluster.push(photo);
clusters[index]?.push(photo);
}
for (const cluster of clusters) cluster.sort(byInstant);
return clusters;
@@ -220,6 +225,13 @@ export function clusterCaptures(captures: readonly LocalCapture[], thresholds: C
}
return [...days.values()]
.flatMap((day) => clusterDay(day, thresholds))
.flatMap((cluster) => {
const groups: LocalCapture[][] = [];
for (let start = 0; start < cluster.length; start += JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP) {
groups.push(cluster.slice(start, start + JOURNEY_FROM_PHOTOS_MAX_PHOTOS_PER_GROUP));
}
return groups;
})
.sort((a, b) => byInstant(a[0] as LocalCapture, b[0] as LocalCapture));
}