Support uploading file from octet-stream

Change-Id: I11ad29059aea4aa5eb0ead906e04dd31f6b36bfb
This commit is contained in:
Ekaterina Fedorova 2013-11-07 14:04:57 +04:00
parent c98656a328
commit 0e234c1dd4
2 changed files with 31 additions and 14 deletions

View File

@ -74,24 +74,41 @@ def get_locations(data_type, result_path):
return jsonify({data_type: locations}) return jsonify({data_type: locations})
def save_file(request, data_type, path=None): def save_file(request, data_type, path=None, filename=None):
if path: if path:
result_path = compose_path(data_type, path) path_to_folder = compose_path(data_type, path)
#subfolder should already exists #subfolder should already exists
if not os.path.exists(result_path): if not os.path.exists(path_to_folder):
abort(404) abort(404)
else: else:
result_path = compose_path(data_type) path_to_folder = compose_path(data_type)
if request.content_type == 'application/octet-stream':
data = request.environ['wsgi.input'].read()
if not data:
return make_response('No file to upload', 400)
if not filename:
return make_response("'filename' should be in request arguments",
400)
with tempfile.NamedTemporaryFile(delete=False) as uploaded_file:
uploaded_file.write(data)
path_to_file = os.path.join(path_to_folder, filename)
if os.path.exists(path_to_file):
abort(403)
shutil.move(uploaded_file.name, path_to_file)
else:
file_to_upload = request.files.get('file') file_to_upload = request.files.get('file')
if file_to_upload: if file_to_upload:
filename = secure_filename(file_to_upload.filename) filename = secure_filename(file_to_upload.filename)
if os.path.exists(os.path.join(result_path, filename)): path_to_file = os.path.join(path_to_folder, filename)
if os.path.exists(path_to_file):
abort(403) abort(403)
file_to_upload.save(os.path.join(result_path, filename)) file_to_upload.save(path_to_file)
else:
return make_response('No file to upload', 400)
update_cache(data_type) update_cache(data_type)
return jsonify(result='success') return jsonify(result='success')
else:
abort(400)
def compose_path(data_type, path=None): def compose_path(data_type, path=None):
@ -184,7 +201,6 @@ def save_archive(request):
data = request.environ['wsgi.input'].read() data = request.environ['wsgi.input'].read()
if not data: if not data:
return err_resp return err_resp
with tempfile.NamedTemporaryFile(delete=False) as uploaded_file: with tempfile.NamedTemporaryFile(delete=False) as uploaded_file:
uploaded_file.write(data) uploaded_file.write(data)
path_to_archive = uploaded_file.name path_to_archive = uploaded_file.name

View File

@ -73,8 +73,9 @@ def get_data_type_locations(data_type):
@v1_api.route('/admin/<data_type>', methods=['POST']) @v1_api.route('/admin/<data_type>', methods=['POST'])
def upload_file(data_type): def upload_file(data_type):
api.check_data_type(data_type) api.check_data_type(data_type)
filename = request.args.get('filename')
try: try:
return api.save_file(request, data_type) return api.save_file(request, data_type, path=None, filename=filename)
except: except:
abort(403) abort(403)