[API] add endpoint to download gpx file

This commit is contained in:
Sam
2021-11-29 16:54:24 +01:00
parent 76dc9f1912
commit e50c3d3799
2 changed files with 153 additions and 1 deletions

View File

@ -5,7 +5,14 @@ from datetime import timedelta
from typing import Any, Dict, List, Optional, Tuple, Union
import requests
from flask import Blueprint, Response, current_app, request, send_file
from flask import (
Blueprint,
Response,
current_app,
request,
send_file,
send_from_directory,
)
from sqlalchemy import exc
from werkzeug.exceptions import RequestEntityTooLarge
@ -703,6 +710,65 @@ def get_segment_chart_data(
)
@workouts_blueprint.route(
'/workouts/<string:workout_short_id>/gpx/download', methods=['GET']
)
@authenticate
def download_workout_gpx(
auth_user_id: int, workout_short_id: str
) -> Union[HttpResponse, Response]:
"""
Download gpx file
**Example request**:
.. sourcecode:: http
GET /api/workouts/kjxavSTUrJvoAh2wvCeGEF/gpx/download HTTP/1.1
**Example response**:
.. sourcecode:: http
HTTP/1.1 200 OK
Content-Type: application/gpx+xml
:param integer auth_user_id: authenticate user id (from JSON Web Token)
:param string workout_short_id: workout short id
:statuscode 200: success
:statuscode 401:
- provide a valid auth token
- signature expired, please log in again
- invalid token, please log in again
:statuscode 404:
- workout not found
- no gpx file for workout
"""
workout_uuid = decode_short_id(workout_short_id)
workout = Workout.query.filter_by(
uuid=workout_uuid, user_id=auth_user_id
).first()
if not workout:
return DataNotFoundErrorResponse(
data_type='workout',
message=f'workout not found (id: {workout_short_id})',
)
if workout.gpx is None:
return DataNotFoundErrorResponse(
data_type='gpx',
message=f'no gpx file for workout (id: {workout_short_id})',
)
return send_from_directory(
current_app.config['UPLOAD_FOLDER'],
workout.gpx,
mimetype='application/gpx+xml',
as_attachment=True,
)
@workouts_blueprint.route('/workouts/map/<map_id>', methods=['GET'])
def get_map(map_id: int) -> Any:
"""