Skip to content

Commit

Permalink
Set MIME type for PNG and SVG images.
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Oct 16, 2015
1 parent 39d6314 commit e50f7ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/frontend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ automatically handle downloads of these files. Note that if the backend
generates additional files in the job directory which should not be downloaded, the :meth:`~saliwebfrontend.allow_file_download` method can be overridden to
control which files the end user is allowed to download. Note also that if
some files are not text files, the :meth:`~saliwebfrontend.get_file_mime_type`
method should also be overridden to specify the MIME type.
method may also need to be overridden to specify the MIME type.

The example below assumes the backend generates a log file ``log`` and a
single output file, ``output.pdb``, and allows the user to download these files
Expand Down
7 changes: 4 additions & 3 deletions doc/modules/frontend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ the web frontend.
:meth:`~saliwebfrontend.download_results_file`) this
method is called to get the correct
`MIME type <http://en.wikipedia.org/wiki/Internet_media_type>`_
for the file. By default, it always returns 'text/plain'. You may need
to override this, for example, if some of your results files are tar
files ('application/x-tar') or PNG images ('image/png').
for the file. By default, it handles PNG and SVG images, and for
all other files returns 'text/plain'. You may need to override this,
for example, if some of your results files are tar
files ('application/x-tar') or other types of image.

.. method:: get_submit_parameter_help()

Expand Down
11 changes: 10 additions & 1 deletion perl/saliweb/frontend.pm
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ use CGI;
use Error qw(:try);
use MIME::Lite;
use Fcntl ':flock';
use File::Basename;

our $web_server = 'modbase.compbio.ucsf.edu';

Expand Down Expand Up @@ -1547,7 +1548,15 @@ sub allow_file_download {
}

sub get_file_mime_type {
return 'text/plain';
my ($self, $file) = @_;
my ($dir, $name, $ext) = fileparse($file, qr/\.[^.]*/);
if ($ext eq '.png') {
return 'image/png';
} elsif ($ext eq '.svg') {
return 'image/svg+xml';
} else {
return 'text/plain';
}
}

sub _download_real_file {
Expand Down
5 changes: 5 additions & 0 deletions test/frontend/results_page.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
is(saliweb::frontend::get_file_mime_type($cls, $file), 'text/plain',
"check get_file_mime_type: $file");
}
is(saliweb::frontend::get_file_mime_type($cls, "/abspath/foo.png"),
'image/png', "check get_file_mime_type: /abspath/foo.png");

is(saliweb::frontend::get_file_mime_type($cls, "/abspath/foo.svg"),
'image/svg+xml', "check get_file_mime_type: /abspath/foo.svg");
}

# Test download_real_file
Expand Down

0 comments on commit e50f7ca

Please sign in to comment.