Skip to content

Commit afe4f07

Browse files
committed
[importer_direct_upload] file storage location is changed to tempfile.NamedTemporaryFile
1 parent 8d53fa8 commit afe4f07

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

desktop/core/src/desktop/settings.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@
115115
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
116116
# trailing slash.
117117
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
118-
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
119-
MEDIA_URL = '/media/'
118+
MEDIA_URL = ''
120119

121120

122121
############################################################

desktop/libs/indexer/src/indexer/api3.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import logging
2626
import urllib.error
2727
import sys
28+
import tempfile
2829
import uuid
2930

3031
from django.urls import reverse
3132
from django.views.decorators.http import require_POST
32-
from django.core.files.storage import FileSystemStorage
3333

3434
LOG = logging.getLogger(__name__)
3535

@@ -44,7 +44,6 @@
4444
from desktop.lib.i18n import smart_unicode
4545
from desktop.lib.python_util import check_encoding
4646
from desktop.models import Document2
47-
from desktop.settings import BASE_DIR
4847
from kafka.kafka_api import get_topics, get_topic_data
4948
from notebook.connectors.base import get_api, Notebook
5049
from notebook.decorators import api_error_handler
@@ -231,7 +230,7 @@ def guess_field_types(request):
231230
if file_format['inputFormat'] == 'localfile':
232231
path = urllib_unquote(file_format['path'])
233232

234-
with open(BASE_DIR + path, 'r') as local_file:
233+
with open(path, 'r') as local_file:
235234

236235
reader = csv.reader(local_file)
237236
csv_data = list(reader)
@@ -742,11 +741,11 @@ def save_pipeline(request):
742741
def upload_local_file(request):
743742

744743
upload_file = request.FILES['inputfile']
745-
fs = FileSystemStorage()
746744
username = request.user.username
747-
filename = "%s_%s.%s" % (username, uuid.uuid4(), 'csv')
748-
name = fs.save(filename, upload_file)
749-
750-
local_file_url = fs.url(name)
745+
filename = "%s_%s" % (username, uuid.uuid4())
746+
temp_file = tempfile.NamedTemporaryFile(prefix=filename, suffix='.csv', delete=False)
747+
temp_file.write(upload_file.read())
748+
local_file_url = temp_file.name
749+
temp_file.close()
751750

752751
return JsonResponse({'local_file_url': local_file_url})

desktop/libs/indexer/src/indexer/indexers/sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def create_table_from_local_file(self, source, destination, start_time=-1):
352352
path = urllib_unquote(source['path'])
353353

354354
if path: # data insertion
355-
with open(BASE_DIR + path, 'r') as local_file:
355+
with open(path, 'r') as local_file:
356356
reader = csv.reader(local_file)
357357
_csv_rows = []
358358

desktop/libs/indexer/src/indexer/indexers/sql_tests.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nose.tools import assert_equal, assert_true
2424

2525
from desktop.lib.django_test_util import make_logged_in_client
26+
from desktop.settings import BASE_DIR
2627
from useradmin.models import User
2728

2829
from azure.conf import ABFS_CLUSTERS
@@ -841,7 +842,7 @@ def test_create_table_from_local_mysql():
841842
with patch('indexer.indexers.sql.get_interpreter') as get_interpreter:
842843
get_interpreter.return_value = {'Name': 'MySQL', 'dialect': 'mysql'}
843844
source = {
844-
'path': '/apps/beeswax/data/tables/us_population.csv',
845+
'path': BASE_DIR + '/apps/beeswax/data/tables/us_population.csv',
845846
'sourceType': 'mysql',
846847
'format': {'hasHeader': False}
847848
}
@@ -875,7 +876,7 @@ def test_create_table_from_local_phoenix():
875876
with patch('indexer.indexers.sql.get_interpreter') as get_interpreter:
876877
get_interpreter.return_value = {'Name': 'Phoenix', 'dialect': 'phoenix'}
877878
source = {
878-
'path': '/apps/beeswax/data/tables/us_population.csv',
879+
'path': BASE_DIR + '/apps/beeswax/data/tables/us_population.csv',
879880
'sourceType': 'phoenix',
880881
'format': {'hasHeader': False}
881882
}
@@ -926,7 +927,7 @@ def test_create_table_from_local_impala():
926927
with patch('indexer.indexers.sql.get_interpreter') as get_interpreter:
927928
get_interpreter.return_value = {'Name': 'Impala', 'dialect': 'impala'}
928929
source = {
929-
'path': '/apps/beeswax/data/tables/flights.csv',
930+
'path': BASE_DIR + '/apps/beeswax/data/tables/flights.csv',
930931
'sourceType': 'impala',
931932
'format': {'hasHeader': True}
932933
}

0 commit comments

Comments
 (0)