Files
dougal-software/lib/www/server/api/middleware/event/get/geojson.js
D. Berge b0f4559b83 Allow direct downloading of sequence reports.
If the `download` or `d` query parameter is supplied (even
without any value), the response will include a
`Content-Disposition: attachment` header. A filename will
also be suggested.
2021-05-15 17:10:28 +02:00

33 lines
850 B
JavaScript

const { event } = require('../../../../lib/db');
const geojson = async function (req, res, next) {
try {
const query = req.query;
query.sequence = req.params.sequence;
const events = await event.list(req.params.project, query);
const response = {
type: "FeatureCollection",
features: events.filter(event => event.geometry).map(event => {
const feature = {
type: "Feature",
geometry: event.geometry,
properties: event
};
delete feature.properties.geometry;
return feature;
})
};
if ("download" in query || "d" in query) {
const filename = `${req.params.project}-seq${query.sequence.padStart(3, "0")}.geojson`;
res.set("Content-Disposition", `attachment; filename="${filename}"`);
}
res.status(200).send(response);
next();
} catch (err) {
next(err);
}
}
module.exports = geojson;