Skip to content

Commit

Permalink
py3: Send bytestring in various datatatypes' display_data methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Aug 2, 2018
1 parent 27a30b7 commit 33c0b58
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/binary.py
Expand Up @@ -67,7 +67,7 @@ def display_data(self, trans, dataset, preview=False, filename=None, to_ext=None
fname = ''.join(c in FILENAME_VALID_CHARS and c or '_' for c in dataset.name)[0:150]
trans.response.set_content_type("application/octet-stream") # force octet-stream so Safari doesn't append mime extensions to filename
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % (dataset.hid, fname, to_ext)
return open(dataset.file_name)
return open(dataset.file_name, mode='rb')


class Ab1(Binary):
Expand Down
15 changes: 8 additions & 7 deletions lib/galaxy/datatypes/data.py
Expand Up @@ -21,6 +21,7 @@
compression_utils,
FILENAME_VALID_CHARS,
inflector,
smart_str,
unicodify
)
from galaxy.util.bunch import Bunch
Expand Down Expand Up @@ -313,7 +314,7 @@ def _serve_raw(self, trans, dataset, to_ext, **kwd):
trans.response.set_content_type("application/octet-stream") # force octet-stream so Safari doesn't append mime extensions to filename
filename = self._download_filename(dataset, to_ext, hdca=kwd.get("hdca", None), element_identifier=kwd.get("element_identifier", None))
trans.response.headers["Content-Disposition"] = 'attachment; filename="%s"' % filename
return open(dataset.file_name)
return open(dataset.file_name, mode='rb')

def to_archive(self, trans, dataset, name=""):
"""
Expand Down Expand Up @@ -355,13 +356,13 @@ def display_data(self, trans, data, preview=False, filename=None, to_ext=None, *
# content from being rendered in the browser
trans.response.headers['X-Content-Type-Options'] = 'nosniff'
if isinstance(data, six.string_types):
return data
return smart_str(data)
if filename and filename != "index":
# For files in extra_files_path
file_path = trans.app.object_store.get_filename(data.dataset, extra_dir='dataset_%s_files' % data.dataset.id, alt_name=filename)
if os.path.exists(file_path):
if os.path.isdir(file_path):
with tempfile.NamedTemporaryFile(delete=False) as tmp_fh:
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_fh:
tmp_file_name = tmp_fh.name
dir_items = sorted(os.listdir(file_path))
base_path, item_name = os.path.split(file_path)
Expand Down Expand Up @@ -404,7 +405,7 @@ def display_data(self, trans, data, preview=False, filename=None, to_ext=None, *
filename = self._download_filename(data, to_ext, hdca=kwd.get("hdca", None), element_identifier=kwd.get("element_identifier", None))
trans.response.set_content_type("application/octet-stream") # force octet-stream so Safari doesn't append mime extensions to filename
trans.response.headers["Content-Disposition"] = 'attachment; filename="%s"' % filename
return open(data.file_name)
return open(data.file_name, 'rb')
if not os.path.exists(data.file_name):
raise webob.exc.HTTPNotFound("File Not Found (%s)." % data.file_name)
max_peek_size = 1000000 # 1 MB
Expand All @@ -426,14 +427,14 @@ def _yield_user_file_content(self, trans, from_dataset, filename):
# Check to see if this dataset's parent job is whitelisted
# We cannot currently trust imported datasets for rendering.
if not from_dataset.creating_job.imported and from_dataset.creating_job.tool_id in trans.app.config.sanitize_whitelist:
return open(filename)
return open(filename, mode='rb')

# This is returning to the browser, it needs to be encoded.
# TODO Ideally this happens a layer higher, but this is a bad
# issue affecting many tools
return sanitize_html(open(filename).read()).encode('utf-8')
return sanitize_html(open(filename, 'rb').read()).encode('utf-8')

return open(filename)
return open(filename, mode='rb')

def _download_filename(self, dataset, to_ext, hdca=None, element_identifier=None):
def escape(raw_identifier):
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/molecules.py
Expand Up @@ -429,7 +429,7 @@ def display_data(self, trans, data, preview=False, filename=None,
This allows us to format the data shown in the central pane via the "eye" icon.
"""
return "This is a OpenBabel Fastsearch format. You can speed up your similarity and substructure search with it."
return b"This is a OpenBabel Fastsearch format. You can speed up your similarity and substructure search with it."

def get_mime(self):
"""Returns the mime type of the datatype (pretend it is text for peek)"""
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/neo4j.py
Expand Up @@ -92,7 +92,7 @@ def display_data(self, trans, data, preview=False, filename=None,
trans.response.headers["Content-Disposition"] = 'attachment; filename="Galaxy%s-[%s].%s"' % \
(data.hid,
download_zip, "zip")
return open(download_zip)
return open(download_zip, mode='rb')


class Neo4jDB(Neo4j, Data):
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/datatypes/tabular.py
Expand Up @@ -94,11 +94,11 @@ def display_data(self, trans, dataset, preview=False, filename=None, to_ext=None
max_peek_size = 1000000 # 1 MB
if os.stat(dataset.file_name).st_size < max_peek_size:
self._clean_and_set_mime_type(trans, dataset.get_mime())
return open(dataset.file_name)
return open(dataset.file_name, mode='rb')
else:
trans.response.set_content_type("text/html")
return trans.stream_template_mako("/dataset/large_file.mako",
truncated_data=open(dataset.file_name).read(max_peek_size),
truncated_data=open(dataset.file_name, mode='r').read(max_peek_size),
data=dataset)
else:
column_names = 'null'
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/text.py
Expand Up @@ -153,7 +153,7 @@ def _display_data_trusted(self, trans, dataset, preview=False, filename=None, to
except subprocess.CalledProcessError:
ofilename = dataset.file_name
log.exception('Command "%s" failed. Could not convert the Jupyter Notebook to HTML, defaulting to plain text.', ' '.join(map(shlex_quote, cmd)))
return open(ofilename)
return open(ofilename, mode='rb')

def set_meta(self, dataset, **kwd):
"""
Expand Down

0 comments on commit 33c0b58

Please sign in to comment.