API: delete temporary folders after upoading files
This commit is contained in:
parent
d5b79e3c57
commit
373d20f0b7
@ -1,7 +1,8 @@
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint, current_app, jsonify, request
|
||||
from mpwo_api import appLog, db
|
||||
from sqlalchemy import exc
|
||||
|
||||
@ -144,10 +145,18 @@ def post_activity(auth_user_id):
|
||||
return jsonify(response_object), 400
|
||||
|
||||
activity_file = request.files['file']
|
||||
upload_dir = os.path.join(
|
||||
current_app.config['UPLOAD_FOLDER'],
|
||||
'activities',
|
||||
str(auth_user_id))
|
||||
folders = {
|
||||
'extract_dir': os.path.join(upload_dir, 'extract'),
|
||||
'tmp_dir': os.path.join(upload_dir, 'tmp'),
|
||||
}
|
||||
|
||||
try:
|
||||
new_activities = process_files(
|
||||
auth_user_id, activity_data, activity_file
|
||||
auth_user_id, activity_data, activity_file, folders
|
||||
)
|
||||
if len(new_activities) > 0:
|
||||
response_object = {
|
||||
@ -166,7 +175,6 @@ def post_activity(auth_user_id):
|
||||
}
|
||||
}
|
||||
code = 400
|
||||
return jsonify(response_object), code
|
||||
except ActivityException as e:
|
||||
db.session.rollback()
|
||||
if e.e:
|
||||
@ -176,7 +184,10 @@ def post_activity(auth_user_id):
|
||||
'message': e.message,
|
||||
}
|
||||
code = 500 if e.status == 'error' else 400
|
||||
return jsonify(response_object), code
|
||||
|
||||
shutil.rmtree(folders['extract_dir'], ignore_errors=True)
|
||||
shutil.rmtree(folders['tmp_dir'], ignore_errors=True)
|
||||
return jsonify(response_object), code
|
||||
|
||||
|
||||
@activities_blueprint.route('/activities/no_gpx', methods=['POST'])
|
||||
|
@ -222,12 +222,7 @@ def get_chart_data(gpx_file):
|
||||
return chart_data
|
||||
|
||||
|
||||
def get_file_path(auth_user_id, filename):
|
||||
dir_path = os.path.join(
|
||||
current_app.config['UPLOAD_FOLDER'],
|
||||
'activities',
|
||||
str(auth_user_id),
|
||||
'tmp')
|
||||
def get_file_path(auth_user_id, dir_path, filename):
|
||||
if not os.path.exists(dir_path):
|
||||
os.makedirs(dir_path)
|
||||
file_path = os.path.join(dir_path, filename)
|
||||
@ -284,12 +279,7 @@ def process_one_gpx_file(auth_user_id, activity_data, file_path, filename):
|
||||
)
|
||||
|
||||
|
||||
def process_zip_archive(auth_user_id, activity_data, zip_path):
|
||||
extract_dir = os.path.join(
|
||||
current_app.config['UPLOAD_FOLDER'],
|
||||
'activities',
|
||||
str(auth_user_id),
|
||||
'extract')
|
||||
def process_zip_archive(auth_user_id, activity_data, zip_path, extract_dir):
|
||||
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
||||
zip_ref.extractall(extract_dir)
|
||||
|
||||
@ -306,10 +296,10 @@ def process_zip_archive(auth_user_id, activity_data, zip_path):
|
||||
return new_activities
|
||||
|
||||
|
||||
def process_files(auth_user_id, activity_data, activity_file):
|
||||
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, filename)
|
||||
file_path = get_file_path(auth_user_id, folders['tmp_dir'], filename)
|
||||
|
||||
try:
|
||||
activity_file.save(file_path)
|
||||
@ -321,4 +311,5 @@ def process_files(auth_user_id, activity_data, activity_file):
|
||||
auth_user_id, activity_data, file_path, filename
|
||||
)]
|
||||
else:
|
||||
return process_zip_archive(auth_user_id, activity_data, file_path)
|
||||
return process_zip_archive(
|
||||
auth_user_id, activity_data, file_path, folders['extract_dir'])
|
||||
|
Loading…
Reference in New Issue
Block a user