From b2be7c38240f35f6c5b9a45a2774d33ebcc91f71 Mon Sep 17 00:00:00 2001 From: timifasubaa <30888507+timifasubaa@users.noreply.github.com> Date: Wed, 18 Apr 2018 15:01:40 -0700 Subject: [PATCH] ensure directory exists before saving csv file (#4829) --- superset/utils.py | 9 +++++++++ superset/views/core.py | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/superset/utils.py b/superset/utils.py index d465bb086c147..2d06c48f2200a 100644 --- a/superset/utils.py +++ b/superset/utils.py @@ -12,6 +12,7 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import formatdate +import errno import functools import json import logging @@ -827,3 +828,11 @@ def is_adhoc_metric(metric): def get_metric_names(metrics): return [metric['label'] if is_adhoc_metric(metric) else metric for metric in metrics] + + +def ensure_path_exists(path): + try: + os.makedirs(path) + except OSError as exc: + if not (os.path.isdir(path) and exc.errno == errno.EEXIST): + raise diff --git a/superset/views/core.py b/superset/views/core.py index 19885dcee32d5..f66b70ef55a44 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -347,15 +347,17 @@ def form_post(self, form): csv_file = form.csv_file.data form.csv_file.data.filename = secure_filename(form.csv_file.data.filename) csv_filename = form.csv_file.data.filename + path = os.path.join(config['UPLOAD_FOLDER'], csv_filename) try: - csv_file.save(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + utils.ensure_path_exists(config['UPLOAD_FOLDER']) + csv_file.save(path) table = SqlaTable(table_name=form.name.data) table.database = form.data.get('con') table.database_id = table.database.id table.database.db_engine_spec.create_table_from_csv(form, table) except Exception as e: try: - os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + os.remove(path) except OSError: pass message = 'Table name {} already exists. Please pick another'.format( @@ -365,7 +367,7 @@ def form_post(self, form): 'danger') return redirect('/csvtodatabaseview/form') - os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + os.remove(path) # Go back to welcome page / splash screen db_name = table.database.database_name message = _('CSV file "{0}" uploaded to table "{1}" in '