mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
feat(collections): export and import a list as GPX
A list could leave the instance as a TREK file, which only another TREK reads. Export now asks for a format, and GPX is the second one: the places as waypoints, for OsmAnd, Organic Maps, a Garmin or gpx.studio. Import takes a GPX in the same dialog as a list file. Asked for in discussion #2301. The GPX is written from the very list file the JSON export returns, so what may leave the instance is still decided in one place. Name, description and address, notes, website and category go into GPX's own elements, where every app shows them. Labels, status, phone and the rest travel in a TREK extension namespace, so a GPX made by TREK comes back as the same list. A place without coordinates cannot be a waypoint; it is left out and the export says how many were. A GPX is read on the server, sent as text, with the XML library the trip importer already uses. A document with a DOCTYPE is refused before parsing, which closes both external entities and entity expansion. Size is capped like the list file, and the element count is capped before a tree is built. The reader only turns the GPX into a list file: the browser previews it and imports it through the existing route, so one contract, one transaction and one skipped counter cover both formats. Waypoints and named route points become places; tracks and unnamed route points are counted in the preview and not imported. The two list-file routes get a body ceiling of twice the file limit. The global 100 kB cap refused most real favourites files, and it already refused list files well under the megabyte the contract allows.
This commit is contained in:
+13
-2
@@ -18,6 +18,7 @@ import { validateRouteGuards } from './nest/common/validate-route-guards';
|
||||
import { validateManagedRoutes } from './nest/common/validate-managed-routes';
|
||||
import { TrekWsAdapter } from './nest/realtime/trek-ws.adapter';
|
||||
import { StorageService } from './nest/storage/storage.service';
|
||||
import { MAX_COLLECTION_FILE_BYTES } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* Builds the unified TREK NestJS application that serves the ENTIRE surface — the
|
||||
@@ -142,18 +143,28 @@ export async function buildApp(): Promise<INestApplication> {
|
||||
* but a save that quietly fails and an editor that says "not saved" without
|
||||
* saying why.
|
||||
*
|
||||
* So the book route, and only the book route, is measured against the size a
|
||||
* book can actually be. Everything else keeps the tighter limit.
|
||||
* So the book route is measured against the size a book can actually be.
|
||||
*
|
||||
* A list file is the same story at a smaller scale. Its contract allows a
|
||||
* megabyte and the import posts it whole, yet a list of a few hundred places
|
||||
* with notes is past a hundred kilobytes already, and so is the favourites
|
||||
* GPX of anybody who uses OsmAnd (#2301). The two routes that carry one are
|
||||
* measured against that megabyte, doubled for the JSON escaping of a
|
||||
* document full of quotes. Everything else keeps the tighter limit.
|
||||
*/
|
||||
const bookBody = express.json({ limit: '8mb', verify: rawBodyKeeper });
|
||||
const listFileBody = express.json({ limit: MAX_COLLECTION_FILE_BYTES * 2, 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/';
|
||||
const isBookWrite = (req: Request) =>
|
||||
req.method === 'PUT' && /^\/api\/journeys\/\d+\/book$/.test(req.path);
|
||||
const listFilePaths = new Set(['/api/addons/collections/import', '/api/addons/collections/gpx/read']);
|
||||
const isListFile = (req: Request) => req.method === 'POST' && listFilePaths.has(req.path);
|
||||
|
||||
instance.use(function jsonParser(req: Request, res: Response, next: NextFunction) {
|
||||
if (isBookWrite(req)) return bookBody(req, res, next);
|
||||
if (isListFile(req)) return listFileBody(req, res, next);
|
||||
return isMcp(req) ? next() : json(req, res, next);
|
||||
});
|
||||
instance.use(function urlencodedParser(req: Request, res: Response, next: NextFunction) {
|
||||
|
||||
Reference in New Issue
Block a user