API minor change and fix

This commit is contained in:
Sam 2018-05-29 11:52:48 +02:00
parent c1af957e50
commit 3db20eced7
2 changed files with 7 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import os
from flask import Blueprint, jsonify, request from flask import Blueprint, jsonify, request
from mpwo_api import appLog, db from mpwo_api import appLog, db
from sqlalchemy import exc from sqlalchemy import exc
from werkzeug.utils import secure_filename
from ..users.utils import authenticate, verify_extension from ..users.utils import authenticate, verify_extension
from .models import Activity, Sport from .models import Activity, Sport
@ -144,7 +145,8 @@ def post_activity(auth_user_id):
return jsonify(response_object), 400 return jsonify(response_object), 400
activity_file = request.files['file'] activity_file = request.files['file']
file_path = get_file_path(auth_user_id, activity_file) filename = secure_filename(activity_file.filename)
file_path = get_file_path(auth_user_id, filename)
try: try:
activity_file.save(file_path) activity_file.save(file_path)
@ -161,7 +163,7 @@ def post_activity(auth_user_id):
new_filepath = get_new_file_path( new_filepath = get_new_file_path(
auth_user_id=auth_user_id, auth_user_id=auth_user_id,
activity_date=gpx_data['start'], activity_date=gpx_data['start'],
activity_file=activity_file, old_filename=filename,
sport=sport.label sport=sport.label
) )
os.rename(file_path, new_filepath) os.rename(file_path, new_filepath)

View File

@ -5,7 +5,6 @@ from datetime import datetime, timedelta
import gpxpy.gpx import gpxpy.gpx
from flask import current_app from flask import current_app
from mpwo_api import appLog from mpwo_api import appLog
from werkzeug.utils import secure_filename
from .models import Activity, ActivitySegment, Sport from .models import Activity, ActivitySegment, Sport
@ -218,8 +217,7 @@ def get_chart_data(gpx_file):
return chart_data return chart_data
def get_file_path(auth_user_id, activity_file): def get_file_path(auth_user_id, filename):
filename = secure_filename(activity_file.filename)
dir_path = os.path.join( dir_path = os.path.join(
current_app.config['UPLOAD_FOLDER'], current_app.config['UPLOAD_FOLDER'],
'activities', 'activities',
@ -231,11 +229,10 @@ def get_file_path(auth_user_id, activity_file):
return file_path return file_path
def get_new_file_path(auth_user_id, activity_date, activity_file, sport): def get_new_file_path(auth_user_id, activity_date, old_filename, sport):
old_filename = secure_filename(activity_file.filename)
extension = f".{old_filename.rsplit('.', 1)[1].lower()}" extension = f".{old_filename.rsplit('.', 1)[1].lower()}"
_, new_filename = tempfile.mkstemp( _, new_filename = tempfile.mkstemp(
prefix=f'{activity_date}_sport_', prefix=f'{activity_date}_{sport}_',
suffix=extension suffix=extension
) )
dir_path = os.path.join( dir_path = os.path.join(