# HG changeset patch # User Marcin Kuzminski # Date 1302458089 -7200 # Node ID ed5270522724c260ade4ce50ab9b7ad7123f61f0 # Parent 2c558e2264b45f9a91c6ccf7a615cf3879491470 changed raw action to show images instead of saying about binary file added mimetype to binary file text diff -r 2c558e2264b4 -r ed5270522724 rhodecode/controllers/files.py --- a/rhodecode/controllers/files.py Sun Apr 10 00:01:04 2011 +0200 +++ b/rhodecode/controllers/files.py Sun Apr 10 19:54:49 2011 +0200 @@ -25,6 +25,7 @@ import os import logging +import mimetypes import rhodecode.lib.helpers as h from pylons import request, response, session, tmpl_context as c, url @@ -157,7 +158,43 @@ cs = self.__get_cs_or_redirect(revision, repo_name) file_node = self.__get_filenode_or_redirect(repo_name, cs, f_path) - response.content_type = 'text/plain' + raw_mimetype_mapping = { + # map original mimetype to a mimetype used for "show as raw" + # you can also provide a content-disposition to override the + # default "attachment" disposition. + # orig_type: (new_type, new_dispo) + + # show images inline: + 'image/x-icon': ('image/x-icon', 'inline'), + 'image/png': ('image/png', 'inline'), + 'image/gif': ('image/gif', 'inline'), + 'image/jpeg': ('image/jpeg', 'inline'), + 'image/svg+xml': ('image/svg+xml', 'inline'), + } + + mimetype = file_node.mimetype + try: + mimetype, dispo = raw_mimetype_mapping[mimetype] + except KeyError: + # we don't know anything special about this, handle it safely + if file_node.is_binary: + # do same as download raw for binary files + mimetype, dispo = 'application/octet-stream', 'attachment' + else: + # do not just use the original mimetype, but force text/plain, + # otherwise it would serve text/html and that might be unsafe. + # Note: underlying vcs library fakes text/plain mimetype if the + # mimetype can not be determined and it thinks it is not binary. + # This might lead to erroneous text display in some cases, but + # helps in other cases, like with text files without extension. + mimetype, dispo = 'text/plain', 'inline' + + if dispo == 'attachment': + dispo = 'attachment; filename=%s' % \ + f_path.split(os.sep)[-1].encode('utf8', 'replace') + + response.content_disposition = dispo + response.content_type = mimetype return file_node.content def annotate(self, repo_name, revision, f_path): diff -r 2c558e2264b4 -r ed5270522724 rhodecode/templates/files/files_annotate.html --- a/rhodecode/templates/files/files_annotate.html Sun Apr 10 00:01:04 2011 +0200 +++ b/rhodecode/templates/files/files_annotate.html Sun Apr 10 19:54:49 2011 +0200 @@ -64,7 +64,7 @@
%if c.file.is_binary: - ${_('Binary file')} + ${_('Binary file (%s)') % c.file.mimetype} %else: % if c.file.size < c.cut_off_limit: ${h.pygmentize_annotation(c.repo_name,c.file,linenos=True,anchorlinenos=True,lineanchors='S',cssclass="code-highlight")} diff -r 2c558e2264b4 -r ed5270522724 rhodecode/templates/files/files_source.html --- a/rhodecode/templates/files/files_source.html Sun Apr 10 00:01:04 2011 +0200 +++ b/rhodecode/templates/files/files_source.html Sun Apr 10 19:54:49 2011 +0200 @@ -37,7 +37,7 @@
%if c.files_list.is_binary: - ${_('Binary file')} + ${_('Binary file (%s)') % c.files_list.mimetype} %else: % if c.files_list.size < c.cut_off_limit: ${h.pygmentize(c.files_list,linenos=True,anchorlinenos=True,lineanchors='L',cssclass="code-highlight")}