API: delete temporary folders after upoading files

This commit is contained in:
Sam 2018-05-29 19:06:33 +02:00
parent d5b79e3c57
commit 373d20f0b7
2 changed files with 21 additions and 19 deletions

View File

@ -1,7 +1,8 @@
import json import json
import os 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 mpwo_api import appLog, db
from sqlalchemy import exc from sqlalchemy import exc
@ -144,10 +145,18 @@ 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']
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: try:
new_activities = process_files( new_activities = process_files(
auth_user_id, activity_data, activity_file auth_user_id, activity_data, activity_file, folders
) )
if len(new_activities) > 0: if len(new_activities) > 0:
response_object = { response_object = {
@ -166,7 +175,6 @@ def post_activity(auth_user_id):
} }
} }
code = 400 code = 400
return jsonify(response_object), code
except ActivityException as e: except ActivityException as e:
db.session.rollback() db.session.rollback()
if e.e: if e.e:
@ -176,6 +184,9 @@ def post_activity(auth_user_id):
'message': e.message, 'message': e.message,
} }
code = 500 if e.status == 'error' else 400 code = 500 if e.status == 'error' else 400
shutil.rmtree(folders['extract_dir'], ignore_errors=True)
shutil.rmtree(folders['tmp_dir'], ignore_errors=True)
return jsonify(response_object), code return jsonify(response_object), code

View File

@ -222,12 +222,7 @@ def get_chart_data(gpx_file):
return chart_data return chart_data
def get_file_path(auth_user_id, filename): def get_file_path(auth_user_id, dir_path, filename):
dir_path = os.path.join(
current_app.config['UPLOAD_FOLDER'],
'activities',
str(auth_user_id),
'tmp')
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)
@ -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): def process_zip_archive(auth_user_id, activity_data, zip_path, extract_dir):
extract_dir = os.path.join(
current_app.config['UPLOAD_FOLDER'],
'activities',
str(auth_user_id),
'extract')
with zipfile.ZipFile(zip_path, "r") as zip_ref: with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(extract_dir) zip_ref.extractall(extract_dir)
@ -306,10 +296,10 @@ def process_zip_archive(auth_user_id, activity_data, zip_path):
return new_activities 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) 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, filename) file_path = get_file_path(auth_user_id, folders['tmp_dir'], filename)
try: try:
activity_file.save(file_path) 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 auth_user_id, activity_data, file_path, filename
)] )]
else: 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'])