From f25c8ef4b959bf980ea5757a9e239e04d8a8bff2 Mon Sep 17 00:00:00 2001 From: soraefir Date: Thu, 24 Aug 2023 10:11:05 +0200 Subject: [PATCH] Added GPX download route --- router/api.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/router/api.js b/router/api.js index 1bb5aec..c57408c 100644 --- a/router/api.js +++ b/router/api.js @@ -38,6 +38,36 @@ module.exports = (fastify, opts, done) => { return reply; }); + fastify.get("/gpx/:id", async (req, reply) => { + if (req.params.id == undefined) + return reply.code(400).send({ error: "No ID query parameter" }); + + fastify.level.db.get(req.params.id, (err, val) => { + if (err) { + console.warn(err); + reply.code(500).send(); + } else { + let file = '' + const data = JSON.parse(val); + const gen_wpt = (name,desc,latlon,icon="Flag") => `0${name}-${desc}${icon}` + data.main.array.forEach(a => { + file+= gen_wpt(a.hotel.name, a.hotel.notes, a.hotel.latlon, icon="Hotel"); + a.places.restaurants.array.forEach(b => { + file+= gen_wpt(b.name, b.notes, b.latlon, icon="Restaurant"); + }); + a.places.activities.array.forEach(b => { + file+= gen_wpt(b.name, b.notes, b.latlon, icon="Tree"); + }); + }); + file+=""; + reply.header('Content-Type', 'application/gpx+xml'); + reply.header('Content-Disposition', `attachment; filename=${req.params.id}.gpx`); + reply.send(file); + } + }); + return reply; + }); + fastify.get("/:id", async (req, reply) => { if (req.params.id == undefined) return reply.code(400).send({ error: "No ID query parameter" });