From ea545b6a16549e83a2cd1335973a37ea7d0d3df9 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 4 Jul 2018 13:10:47 +0200 Subject: [PATCH] store files w/ relative path instead of absolute path --- .../fittrackee_api/activities/activities.py | 12 +++++++----- .../fittrackee_api/activities/models.py | 5 +++-- .../fittrackee_api/activities/utils.py | 17 ++++++++++------- .../fittrackee_api/activities/utils_files.py | 7 +++++++ .../tests/test_activities_api_3_delete.py | 2 ++ 5 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 fittrackee_api/fittrackee_api/activities/utils_files.py diff --git a/fittrackee_api/fittrackee_api/activities/activities.py b/fittrackee_api/fittrackee_api/activities/activities.py index ed1c3623..61f97a25 100644 --- a/fittrackee_api/fittrackee_api/activities/activities.py +++ b/fittrackee_api/fittrackee_api/activities/activities.py @@ -12,8 +12,8 @@ from ..users.utils import ( ) from .models import Activity from .utils import ( - ActivityException, create_activity, edit_activity, get_chart_data, - get_datetime_with_tz, process_files + ActivityException, create_activity, edit_activity, get_absolute_file_path, + get_chart_data, get_datetime_with_tz, process_files ) from .utils_format import convert_in_duration @@ -139,10 +139,11 @@ def get_activity_data(auth_user_id, activity_id, data_type): return jsonify(response_object), 400 try: + absolute_gpx_filepath = get_absolute_file_path(activity.gpx) if data_type == 'chart': - content = get_chart_data(activity.gpx) + content = get_chart_data(absolute_gpx_filepath) else: # data_type == 'gpx' - with open(activity.gpx, encoding='utf-8') as f: + with open(absolute_gpx_filepath, encoding='utf-8') as f: content = f.read() except Exception as e: appLog.error(e) @@ -198,7 +199,8 @@ def get_map(map_id): } return jsonify(response_object), 404 else: - return send_file(activity.map) + absolute_map_filepath = get_absolute_file_path(activity.map) + return send_file(absolute_map_filepath) except Exception as e: appLog.error(e) response_object = { diff --git a/fittrackee_api/fittrackee_api/activities/models.py b/fittrackee_api/fittrackee_api/activities/models.py index a9e3820d..e27e8c83 100644 --- a/fittrackee_api/fittrackee_api/activities/models.py +++ b/fittrackee_api/fittrackee_api/activities/models.py @@ -8,6 +8,7 @@ from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm.session import object_session from sqlalchemy.types import JSON, Enum +from .utils_files import get_absolute_file_path from .utils_format import convert_in_duration, convert_value_to_integer record_types = [ @@ -298,9 +299,9 @@ def on_activity_delete(mapper, connection, old_record): @listens_for(db.Session, 'after_flush', once=True) def receive_after_flush(session, context): if old_record.map: - os.remove(old_record.map) + os.remove(get_absolute_file_path(old_record.map)) if old_record.gpx: - os.remove(old_record.gpx) + os.remove(get_absolute_file_path(old_record.gpx)) class ActivitySegment(db.Model): diff --git a/fittrackee_api/fittrackee_api/activities/utils.py b/fittrackee_api/fittrackee_api/activities/utils.py index 3cc40397..09b4bcc9 100644 --- a/fittrackee_api/fittrackee_api/activities/utils.py +++ b/fittrackee_api/fittrackee_api/activities/utils.py @@ -14,6 +14,7 @@ from werkzeug.utils import secure_filename from ..users.models import User from .models import Activity, ActivitySegment, Sport +from .utils_files import get_absolute_file_path from .utils_weather import get_weather @@ -274,7 +275,7 @@ def get_chart_data(gpx_file): return chart_data -def get_file_path(auth_user_id, dir_path, filename): +def get_file_path(dir_path, filename): if not os.path.exists(dir_path): os.makedirs(dir_path) file_path = os.path.join(dir_path, filename) @@ -290,8 +291,7 @@ def get_new_file_path( prefix=f'{activity_date}_{sport}_', suffix=extension ) - dir_path = os.path.join( - current_app.config['UPLOAD_FOLDER'], 'activities', str(auth_user_id)) + dir_path = os.path.join('activities', str(auth_user_id)) if not os.path.exists(dir_path): os.makedirs(dir_path) file_path = os.path.join(dir_path, @@ -313,7 +313,8 @@ def get_map_hash(map_filepath): (maps are sensitive data) """ md5 = hashlib.md5() - with open(map_filepath, 'rb') as f: + absolute_map_filepath = get_absolute_file_path(map_filepath) + with open(absolute_map_filepath, 'rb') as f: for chunk in iter(lambda: f.read(128 * md5.block_size), b''): md5.update(chunk) return md5.hexdigest() @@ -329,7 +330,8 @@ def process_one_gpx_file(params, filename): old_filename=filename, sport=params['sport_label'] ) - os.rename(params['file_path'], new_filepath) + absolute_gpx_filepath = get_absolute_file_path(new_filepath) + os.rename(params['file_path'], absolute_gpx_filepath) gpx_data['filename'] = new_filepath map_filepath = get_new_file_path( @@ -338,7 +340,8 @@ def process_one_gpx_file(params, filename): extension='.png', sport=params['sport_label'] ) - generate_map(map_filepath, map_data) + absolute_map_filepath = get_absolute_file_path(map_filepath) + generate_map(absolute_map_filepath, map_data) except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e: raise ActivityException('error', 'Error during gpx file parsing.', e) except Exception as e: @@ -394,7 +397,7 @@ def process_zip_archive(common_params, extract_dir): def process_files(auth_user_id, activity_data, activity_file, folders): filename = secure_filename(activity_file.filename) extension = f".{filename.rsplit('.', 1)[1].lower()}" - file_path = get_file_path(auth_user_id, folders['tmp_dir'], filename) + file_path = get_file_path(folders['tmp_dir'], filename) sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first() if not sport: raise ActivityException( diff --git a/fittrackee_api/fittrackee_api/activities/utils_files.py b/fittrackee_api/fittrackee_api/activities/utils_files.py new file mode 100644 index 00000000..e2d176c5 --- /dev/null +++ b/fittrackee_api/fittrackee_api/activities/utils_files.py @@ -0,0 +1,7 @@ +import os + +from flask import current_app + + +def get_absolute_file_path(relative_path): + return os.path.join(current_app.config['UPLOAD_FOLDER'], relative_path) diff --git a/fittrackee_api/fittrackee_api/tests/test_activities_api_3_delete.py b/fittrackee_api/fittrackee_api/tests/test_activities_api_3_delete.py index cbb2d800..aede8ab4 100644 --- a/fittrackee_api/fittrackee_api/tests/test_activities_api_3_delete.py +++ b/fittrackee_api/fittrackee_api/tests/test_activities_api_3_delete.py @@ -3,6 +3,7 @@ import os from io import BytesIO from fittrackee_api.activities.models import Activity +from fittrackee_api.activities.utils import get_absolute_file_path def get_gpx_filepath(activity_id): @@ -193,6 +194,7 @@ def test_delete_an_activity_with_gpx_invalid_file( ) gpx_filepath = get_gpx_filepath(1) + gpx_filepath = get_absolute_file_path(gpx_filepath) os.remove(gpx_filepath) response = client.delete(