From 373d20f0b7edae19260190fb1d71ce191672d1f8 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 29 May 2018 19:06:33 +0200 Subject: [PATCH] API: delete temporary folders after upoading files --- mpwo_api/mpwo_api/activities/activities.py | 19 +++++++++++++++---- mpwo_api/mpwo_api/activities/utils.py | 21 ++++++--------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index f112e070..a8b7b0e5 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -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']) diff --git a/mpwo_api/mpwo_api/activities/utils.py b/mpwo_api/mpwo_api/activities/utils.py index a5a52e21..6e3cf3ab 100644 --- a/mpwo_api/mpwo_api/activities/utils.py +++ b/mpwo_api/mpwo_api/activities/utils.py @@ -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'])