diff --git a/muranorepository/api/utils.py b/muranorepository/api/utils.py index aa9329e..a114619 100644 --- a/muranorepository/api/utils.py +++ b/muranorepository/api/utils.py @@ -74,24 +74,41 @@ def get_locations(data_type, result_path): 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: - result_path = compose_path(data_type, path) + path_to_folder = compose_path(data_type, path) #subfolder should already exists - if not os.path.exists(result_path): + if not os.path.exists(path_to_folder): abort(404) else: - result_path = compose_path(data_type) - file_to_upload = request.files.get('file') - if file_to_upload: - filename = secure_filename(file_to_upload.filename) - if os.path.exists(os.path.join(result_path, filename)): + 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) - file_to_upload.save(os.path.join(result_path, filename)) - update_cache(data_type) - return jsonify(result='success') + shutil.move(uploaded_file.name, path_to_file) else: - abort(400) + file_to_upload = request.files.get('file') + if file_to_upload: + filename = secure_filename(file_to_upload.filename) + path_to_file = os.path.join(path_to_folder, filename) + if os.path.exists(path_to_file): + abort(403) + file_to_upload.save(path_to_file) + else: + return make_response('No file to upload', 400) + update_cache(data_type) + return jsonify(result='success') def compose_path(data_type, path=None): @@ -184,7 +201,6 @@ def save_archive(request): data = request.environ['wsgi.input'].read() if not data: return err_resp - with tempfile.NamedTemporaryFile(delete=False) as uploaded_file: uploaded_file.write(data) path_to_archive = uploaded_file.name diff --git a/muranorepository/api/v1.py b/muranorepository/api/v1.py index 6b48e62..9d36bfe 100644 --- a/muranorepository/api/v1.py +++ b/muranorepository/api/v1.py @@ -73,8 +73,9 @@ def get_data_type_locations(data_type): @v1_api.route('/admin/', methods=['POST']) def upload_file(data_type): api.check_data_type(data_type) + filename = request.args.get('filename') try: - return api.save_file(request, data_type) + return api.save_file(request, data_type, path=None, filename=filename) except: abort(403)