Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rest: add preview flag to download file #302

Merged
merged 2 commits into from Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/openapi.json
Expand Up @@ -1218,6 +1218,13 @@
"name": "file_name",
"required": true,
"type": "string"
},
{
"description": "Optional flag to return a previewable response of the file (corresponding mime-type).",
"in": "query",
"name": "preview",
"required": false,
"type": "boolean"
}
],
"produces": [
Expand Down
18 changes: 17 additions & 1 deletion reana_workflow_controller/rest/workflows_workspace.py
Expand Up @@ -8,6 +8,8 @@

"""REANA Workflow Controller workspaces REST API."""

import json
import mimetypes
import os

from flask import Blueprint, current_app, jsonify, request, send_from_directory
Expand Down Expand Up @@ -181,6 +183,13 @@ def download_file(workflow_id_or_name, file_name): # noqa
description: Required. Name (or path) of the file to be downloaded.
required: true
type: string
- name: preview
in: query
description: >-
Optional flag to return a previewable response of the file
(corresponding mime-type).
required: false
type: boolean
responses:
200:
description: >-
Expand Down Expand Up @@ -220,9 +229,16 @@ def download_file(workflow_id_or_name, file_name): # noqa
absolute_workflow_workspace_path = os.path.join(
current_app.config['SHARED_VOLUME_PATH'],
workflow.workspace_path)

preview = json.loads(request.args.get('preview', 'false').lower())
response_mime_type = 'multipart/form-data'
file_mime_type = mimetypes.guess_type(file_name)[0]
# Only display image files as preview
if preview and file_mime_type and file_mime_type.startswith('image'):
response_mime_type = file_mime_type
return send_from_directory(absolute_workflow_workspace_path,
file_name,
mimetype='multipart/form-data',
mimetype=response_mime_type,
as_attachment=True), 200

except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -52,7 +52,7 @@
'jsonpickle>=0.9.6',
'marshmallow>2.13.0,<=2.20.1',
'packaging>=18.0',
'reana-commons[kubernetes]>=0.7.0.dev20200211,<0.8.0',
'reana-commons[kubernetes]>=0.7.0.dev20200217,<0.8.0',
'reana-db>=0.7.0.dev20200206,<0.8.0',
'requests==2.20.0',
'sqlalchemy-utils>=0.31.0',
Expand Down