refactor upload endpoint
This commit is contained in:
parent
affb7d837a
commit
6a587d03cf
1 changed files with 29 additions and 24 deletions
|
@ -100,34 +100,39 @@ class EncryptedFileUpload(resource.Resource):
|
||||||
it into a temporary dir, and responds with a JSON string containing
|
it into a temporary dir, and responds with a JSON string containing
|
||||||
the path of the newly created file.
|
the path of the newly created file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, api):
|
def __init__(self, api):
|
||||||
self._api = api
|
self._api = api
|
||||||
|
|
||||||
def render_POST(self, request):
|
def render_POST(self, request):
|
||||||
origfilename = request.args['file_filename'][0]
|
origfilename = request.args['file_filename'][0]
|
||||||
uploaded_file = request.args['file'][0] # Temp file created by request
|
# Temp file created by request
|
||||||
|
uploaded_file = request.args['file'][0]
|
||||||
|
newpath = move_to_temp_dir_and_restore_filename(uploaded_file, origfilename)
|
||||||
|
self._api.uploaded_temp_files.append(newpath)
|
||||||
|
return json.dumps(newpath)
|
||||||
|
|
||||||
# Move to a new temporary dir and restore the original file name
|
|
||||||
|
def move_to_temp_dir_and_restore_filename(uploaded_file, origfilename):
|
||||||
newdirpath = tempfile.mkdtemp()
|
newdirpath = tempfile.mkdtemp()
|
||||||
newpath = os.path.join(newdirpath, origfilename)
|
newpath = os.path.join(newdirpath, origfilename)
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
shutil.copy(uploaded_file.name, newpath)
|
# TODO: comment on why shutil.move doesn't work?
|
||||||
# TODO Still need to remove the file
|
move_win(uploaded_file.name, newpath)
|
||||||
|
else:
|
||||||
|
shutil.move(uploaded_file.name, newpath)
|
||||||
|
return newpath
|
||||||
|
|
||||||
|
|
||||||
|
def move_win(from_path, to_path):
|
||||||
|
shutil.copy(from_path, to_path)
|
||||||
|
# TODO Still need to remove the file
|
||||||
# TODO deal with pylint error in cleaner fashion than this
|
# TODO deal with pylint error in cleaner fashion than this
|
||||||
try:
|
try:
|
||||||
from exceptions import WindowsError as win_except
|
from exceptions import WindowsError as win_except
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
log.error("This shouldn't happen")
|
log.error("This shouldn't happen")
|
||||||
win_except = Exception
|
win_except = Exception
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.remove(uploaded_file.name)
|
os.remove(from_path)
|
||||||
except win_except as e:
|
except win_except as e:
|
||||||
pass
|
pass
|
||||||
else:
|
|
||||||
shutil.move(uploaded_file.name, newpath)
|
|
||||||
self._api.uploaded_temp_files.append(newpath)
|
|
||||||
|
|
||||||
return json.dumps(newpath)
|
|
||||||
|
|
Loading…
Reference in a new issue