Skip to content

Commit

Permalink
ensure directory exists before saving csv file (#4829)
Browse files Browse the repository at this point in the history
  • Loading branch information
timifasubaa authored and john-bodley committed Apr 18, 2018
1 parent eac97ce commit a14dc26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions superset/utils.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 5 additions & 3 deletions superset/views/core.py
Expand Up @@ -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(
Expand All @@ -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 '
Expand Down

0 comments on commit a14dc26

Please sign in to comment.