From 70fa1a41c4fafbde533bc1a60d9258e9c2d48cdd Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Fri, 25 Feb 2022 11:53:28 +0100 Subject: [PATCH] Provide GPX download from track page --- api/obs/api/routes/tracks.py | 18 ++++++++++++++++++ frontend/src/pages/TrackPage/TrackActions.tsx | 7 ++++--- frontend/src/pages/TrackPage/index.tsx | 6 +++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/api/obs/api/routes/tracks.py b/api/obs/api/routes/tracks.py index d2cdbef..3a3a2e2 100644 --- a/api/obs/api/routes/tracks.py +++ b/api/obs/api/routes/tracks.py @@ -214,6 +214,24 @@ async def download_original_file(req, slug: str): ) +@api.get("/tracks//download/track.gpx") +async def download_track_gpx(req, slug: str): + track = await _load_track(req, slug) + + if not track.is_visible_to(req.ctx.user): + raise Forbidden() + + file_path = join(req.app.config.PROCESSING_OUTPUT_DIR, track.file_path, "track.gpx") + if not exists(file_path) or not isfile(file_path): + raise NotFound() + + return await file_stream( + file_path, + mime_type="application/gpx+xml", + filename=f"{slug}.gpx", + ) + + @api.put("/tracks/") @require_auth async def put_track(req, slug: str): diff --git a/frontend/src/pages/TrackPage/TrackActions.tsx b/frontend/src/pages/TrackPage/TrackActions.tsx index 865f812..19a8da5 100644 --- a/frontend/src/pages/TrackPage/TrackActions.tsx +++ b/frontend/src/pages/TrackPage/TrackActions.tsx @@ -2,11 +2,11 @@ import React from 'react' import {Link} from 'react-router-dom' import {Icon, Popup, Button, Dropdown} from 'semantic-ui-react' -export default function TrackActions({slug, isAuthor, onDownloadOriginal}) { +export default function TrackActions({slug, isAuthor, onDownload}) { return ( <> {isAuthor ? ( - + } - trigger={} + trigger={ onDownload('original.csv')} />} /> + onDownload('track.gpx')} /> ) : ( diff --git a/frontend/src/pages/TrackPage/index.tsx b/frontend/src/pages/TrackPage/index.tsx index 4656c0e..fd56ba8 100644 --- a/frontend/src/pages/TrackPage/index.tsx +++ b/frontend/src/pages/TrackPage/index.tsx @@ -145,8 +145,8 @@ const TrackPage = connect((state) => ({login: state.login}))(function TrackPage( [slug, reloadComments] ) - const onDownloadOriginal = React.useCallback(() => { - api.downloadFile(`/tracks/${slug}/download/original.csv`) + const onDownload= React.useCallback((filename) => { + api.downloadFile(`/tracks/${slug}/download/${filename}`) }, [slug]) const isAuthor = login?.username === data?.track?.author?.username @@ -191,7 +191,7 @@ const TrackPage = connect((state) => ({login: state.login}))(function TrackPage( <>
{track.title || 'Unnamed track'}
- + )}