From 4a119a0fc92643ba09312323818e6986a3bca625 Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Tue, 19 Jan 2021 13:46:28 +0100 Subject: [PATCH] feat: implement download for public and private track data independently (public CSV is obsver1 for now) --- src/routes/api/tracks.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/routes/api/tracks.js b/src/routes/api/tracks.js index ddf0ec2..ba33f86 100644 --- a/src/routes/api/tracks.js +++ b/src/routes/api/tracks.js @@ -6,7 +6,7 @@ const Comment = mongoose.model('Comment'); const User = mongoose.model('User'); const busboy = require('connect-busboy'); const auth = require('../auth'); -const { normalizeUserAgent } = require('../../logic/tracks'); +const { normalizeUserAgent, buildObsver1 } = require('../../logic/tracks'); const wrapRoute = require('../../_helpers/wrapRoute'); function preloadByParam(target, getValueFromParam) { @@ -364,11 +364,26 @@ router.get( '/:track/download', auth.optional, wrapRoute(async (req, res) => { - if (!req.track.isVisibleTo(req.user)) { + if (req.track.isVisibleToPrivate(req.user)) { + return res.download(req.track.fullOriginalFilePath) + } else if (req.track.isVisibleTo(req.user)) { + await req.track.populate('publicTrackData').execPopulate() + + if (!req.track.publicTrackData) { + return res.sendStatus(403); + } + + const body = buildObsver1(req.track.publicTrackData.points) + const fileName = req.track.slug + '_public.csv' + + res.set({ + 'Content-Disposition': 'attachment; filename=' + JSON.stringify(fileName), + 'Content-Type': 'text/csv', + }); + return res.end(body) + } else { return res.sendStatus(403); } - - return res.download(req.track.fullOriginalFilePath) }), );