store files w/ relative path instead of absolute path

This commit is contained in:
Sam 2018-07-04 13:10:47 +02:00
parent 88382d72d5
commit ea545b6a16
5 changed files with 29 additions and 14 deletions

View File

@ -12,8 +12,8 @@ from ..users.utils import (
) )
from .models import Activity from .models import Activity
from .utils import ( from .utils import (
ActivityException, create_activity, edit_activity, get_chart_data, ActivityException, create_activity, edit_activity, get_absolute_file_path,
get_datetime_with_tz, process_files get_chart_data, get_datetime_with_tz, process_files
) )
from .utils_format import convert_in_duration 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 return jsonify(response_object), 400
try: try:
absolute_gpx_filepath = get_absolute_file_path(activity.gpx)
if data_type == 'chart': if data_type == 'chart':
content = get_chart_data(activity.gpx) content = get_chart_data(absolute_gpx_filepath)
else: # data_type == 'gpx' 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() content = f.read()
except Exception as e: except Exception as e:
appLog.error(e) appLog.error(e)
@ -198,7 +199,8 @@ def get_map(map_id):
} }
return jsonify(response_object), 404 return jsonify(response_object), 404
else: 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: except Exception as e:
appLog.error(e) appLog.error(e)
response_object = { response_object = {

View File

@ -8,6 +8,7 @@ from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm.session import object_session from sqlalchemy.orm.session import object_session
from sqlalchemy.types import JSON, Enum 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 from .utils_format import convert_in_duration, convert_value_to_integer
record_types = [ record_types = [
@ -298,9 +299,9 @@ def on_activity_delete(mapper, connection, old_record):
@listens_for(db.Session, 'after_flush', once=True) @listens_for(db.Session, 'after_flush', once=True)
def receive_after_flush(session, context): def receive_after_flush(session, context):
if old_record.map: if old_record.map:
os.remove(old_record.map) os.remove(get_absolute_file_path(old_record.map))
if old_record.gpx: if old_record.gpx:
os.remove(old_record.gpx) os.remove(get_absolute_file_path(old_record.gpx))
class ActivitySegment(db.Model): class ActivitySegment(db.Model):

View File

@ -14,6 +14,7 @@ from werkzeug.utils import secure_filename
from ..users.models import User from ..users.models import User
from .models import Activity, ActivitySegment, Sport from .models import Activity, ActivitySegment, Sport
from .utils_files import get_absolute_file_path
from .utils_weather import get_weather from .utils_weather import get_weather
@ -274,7 +275,7 @@ def get_chart_data(gpx_file):
return chart_data 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): if not os.path.exists(dir_path):
os.makedirs(dir_path) os.makedirs(dir_path)
file_path = os.path.join(dir_path, filename) file_path = os.path.join(dir_path, filename)
@ -290,8 +291,7 @@ def get_new_file_path(
prefix=f'{activity_date}_{sport}_', prefix=f'{activity_date}_{sport}_',
suffix=extension suffix=extension
) )
dir_path = os.path.join( dir_path = os.path.join('activities', str(auth_user_id))
current_app.config['UPLOAD_FOLDER'], 'activities', str(auth_user_id))
if not os.path.exists(dir_path): if not os.path.exists(dir_path):
os.makedirs(dir_path) os.makedirs(dir_path)
file_path = os.path.join(dir_path, file_path = os.path.join(dir_path,
@ -313,7 +313,8 @@ def get_map_hash(map_filepath):
(maps are sensitive data) (maps are sensitive data)
""" """
md5 = hashlib.md5() 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''): for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
md5.update(chunk) md5.update(chunk)
return md5.hexdigest() return md5.hexdigest()
@ -329,7 +330,8 @@ def process_one_gpx_file(params, filename):
old_filename=filename, old_filename=filename,
sport=params['sport_label'] 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 gpx_data['filename'] = new_filepath
map_filepath = get_new_file_path( map_filepath = get_new_file_path(
@ -338,7 +340,8 @@ def process_one_gpx_file(params, filename):
extension='.png', extension='.png',
sport=params['sport_label'] 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: except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e:
raise ActivityException('error', 'Error during gpx file parsing.', e) raise ActivityException('error', 'Error during gpx file parsing.', e)
except Exception as 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): def process_files(auth_user_id, activity_data, activity_file, folders):
filename = secure_filename(activity_file.filename) filename = secure_filename(activity_file.filename)
extension = f".{filename.rsplit('.', 1)[1].lower()}" 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() sport = Sport.query.filter_by(id=activity_data.get('sport_id')).first()
if not sport: if not sport:
raise ActivityException( raise ActivityException(

View File

@ -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)

View File

@ -3,6 +3,7 @@ import os
from io import BytesIO from io import BytesIO
from fittrackee_api.activities.models import Activity from fittrackee_api.activities.models import Activity
from fittrackee_api.activities.utils import get_absolute_file_path
def get_gpx_filepath(activity_id): 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_gpx_filepath(1)
gpx_filepath = get_absolute_file_path(gpx_filepath)
os.remove(gpx_filepath) os.remove(gpx_filepath)
response = client.delete( response = client.delete(