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" });