Skip to content

Commit

Permalink
Merge f1fc530 into 66987ff
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Oct 15, 2019
2 parents 66987ff + f1fc530 commit cc7c0e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
10 changes: 6 additions & 4 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@
},
"post": {
"consumes": [
"multipart/form-data"
"application/octet-stream"
],
"description": "This resource is expecting a workflow UUID and a file to place in the workspace.",
"operationId": "upload_file",
Expand All @@ -1045,10 +1045,12 @@
},
{
"description": "Required. File to add to the workspace.",
"in": "formData",
"name": "file_content",
"in": "body",
"name": "file",
"required": true,
"type": "file"
"schema": {
"type": "string"
}
},
{
"description": "Required. File name.",
Expand Down
24 changes: 17 additions & 7 deletions reana_workflow_controller/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import os
import pprint
import shutil
import subprocess
import traceback
from datetime import datetime
Expand All @@ -27,6 +28,7 @@
from reana_db.database import Session
from reana_db.models import Job, User, Workflow, WorkflowStatus
from reana_db.utils import _get_workflow_with_uuid_or_name
from werkzeug.datastructures import FileStorage
from werkzeug.exceptions import NotFound

from reana_workflow_controller.config import (DEFAULT_NAME_FOR_WORKFLOWS,
Expand Down Expand Up @@ -359,7 +361,7 @@ def upload_file(workflow_id_or_name):
workspace.
operationId: upload_file
consumes:
- multipart/form-data
- application/octet-stream
produces:
- application/json
parameters:
Expand All @@ -373,11 +375,12 @@ def upload_file(workflow_id_or_name):
description: Required. Workflow UUID or name.
required: true
type: string
- name: file_content
in: formData
- name: file
in: body
description: Required. File to add to the workspace.
required: true
type: file
schema:
type: string
- name: file_name
in: query
description: Required. File name.
Expand Down Expand Up @@ -419,8 +422,13 @@ def upload_file(workflow_id_or_name):
Request failed. Internal controller error.
"""
try:
if not ('application/octet-stream' in
request.headers.get('Content-Type')):
return jsonify(
{"message": f'Wrong Content-Type '
f'{request.headers.get("Content-Type")} '
f'use application/octet-stream'}), 400
user_uuid = request.args['user']
file_ = request.files['file_content']
full_file_name = request.args['file_name']
if not full_file_name:
raise ValueError('The file transferred needs to have name.')
Expand All @@ -444,8 +452,10 @@ def upload_file(workflow_id_or_name):
"/".join(dirs))
if not os.path.exists(absolute_workspace_path):
os.makedirs(absolute_workspace_path)
absolute_file_path = os.path.join(absolute_workspace_path, filename)
with open(absolute_file_path, 'wb') as fdest:
shutil.copyfileobj(request.stream, fdest)

file_.save(os.path.join(absolute_workspace_path, filename))
return jsonify(
{'message': '{} has been successfully uploaded.'.format(
full_file_name)}), 200
Expand All @@ -456,7 +466,7 @@ def upload_file(workflow_id_or_name):
'Please set your REANA_WORKON environment'
'variable appropriately.'.
format(workflow_id_or_name)}), 404
except (KeyError, ValueError) as e:
except (KeyError, ) as e:
return jsonify({"message": str(e)}), 400
except Exception as e:
return jsonify({"message": str(e)}), 500
Expand Down
10 changes: 4 additions & 6 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,8 @@ def test_upload_file(app, session, default_user,
workflow_id_or_name=workflow_uuid),
query_string={"user": default_user.id_,
"file_name": file_name},
content_type='multipart/form-data',
data={'file_content': (io.BytesIO(file_binary_content),
file_name)})
content_type='application/octet-stream',
input_stream=io.BytesIO(file_binary_content))
assert res.status_code == 200
# remove workspace directory from path
workflow_workspace = workflow.get_workspace()
Expand Down Expand Up @@ -664,9 +663,8 @@ def test_upload_file_unknown_workflow(app, default_user):
workflow_id_or_name=random_workflow_uuid),
query_string={"user": default_user.id_,
"file_name": file_name},
content_type='multipart/form-data',
data={'file_content': (io.BytesIO(file_binary_content),
file_name)})
content_type='application/octet-stream',
input_stream=io.BytesIO(file_binary_content))
assert res.status_code == 404


Expand Down

0 comments on commit cc7c0e9

Please sign in to comment.